@@ -696,6 +696,30 @@ describe('unitmath', () => {
696696 } )
697697 } )
698698
699+ describe ( 'compare' , ( ) => {
700+ it ( 'should compare two units' , ( ) => {
701+ assert . strictEqual ( unit ( '30 min' ) . compare ( '1 hour' ) , - 1 )
702+ assert . strictEqual ( unit ( '60 min' ) . compare ( '1 hour' ) , 0 )
703+ assert . strictEqual ( unit ( '90 min' ) . compare ( '1 hour' ) , 1 )
704+ } )
705+
706+ it ( 'should work with valueless units' , ( ) => {
707+ assert . strictEqual ( unit ( 'kg/hr' ) . compare ( 'kg/min' ) , - 1 )
708+ assert . strictEqual ( unit ( 'kg/hr' ) . compare ( 'kg/hr' ) , 0 )
709+ assert . strictEqual ( unit ( 'kg/hr' ) . compare ( 'g/hr' ) , 1 )
710+ } )
711+
712+ it ( 'should throw if dimensions do not match' , ( ) => {
713+ assert . throws ( ( ) => unit ( 100 , 'N' ) . compare ( unit ( 100 , 'kg m / s' ) ) , / C a n n o t c o m p a r e u n i t s .* d i m e n s i o n s d o n o t m a t c h / )
714+ assert . throws ( ( ) => unit ( 100 , 'cm' ) . compare ( unit ( 1 , 'kg' ) ) , / C a n n o t c o m p a r e u n i t s .* d i m e n s i o n s d o n o t m a t c h / )
715+ } )
716+
717+ it ( 'should convert parameter to a unit' , ( ) => {
718+ assert . strictEqual ( unit ( 100 , 'cm' ) . compare ( '2 m' ) , - 1 )
719+ assert . strictEqual ( unit ( '3 kg / kg' ) . compare ( 2 ) , 1 )
720+ } )
721+ } )
722+
699723 describe ( 'lessThan' , ( ) => {
700724 it ( 'should test whether one unit is less than another' , ( ) => {
701725 assert . strictEqual ( unit ( 100 , 'cm' ) . lessThan ( unit ( 1 , 'm' ) ) , false )
@@ -2024,6 +2048,9 @@ describe('unitmath', () => {
20242048 assert ( unitDec ( '5 km' ) . lessThanOrEqual ( '500000 cm' ) )
20252049 assert ( unitDec ( '5 N' ) . greaterThan ( '5 dyne' ) )
20262050 assert ( unitDec ( '10 kg' ) . greaterThanOrEqual ( '1 kg' ) )
2051+ assert . strictEqual ( unitDec ( '60 min' ) . compare ( '2 hour' ) , - 1 )
2052+ assert . strictEqual ( unitDec ( '60 min' ) . compare ( '1 hour' ) , 0 )
2053+ assert . strictEqual ( unitDec ( '60 min' ) . compare ( '0.5 hour' ) , 1 )
20272054 } )
20282055
20292056 it ( 'should do setValue' , ( ) => {
0 commit comments