Skip to content

Commit 8feaba3

Browse files
committed
Added compare, release v0.7.0
1 parent dad9983 commit 8feaba3

5 files changed

Lines changed: 61 additions & 1 deletion

File tree

HISTORY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# History
22

3+
## 2019-05-28, v0.7.0
4+
- Added compare
5+
36
## 2019-05-28, v0.6.0
47
- Implement valueOf()
58
- Added lessThan, lessThanOrEqual, greaterThan, greaterThanOrEqual

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,16 @@ UnitMath will use your custom type's `toString()` method when formatting a custo
636636
unit('3 ft').equals('1 yard') // true
637637
```
638638

639+
- `#compare(other: unit | string)`
640+
641+
Returns a value indicating whether this unit is less than (-1), greater than (1), or equal to (0), another unit.
642+
643+
```js
644+
unit('30 min').compare('1 hour') // -1
645+
unit('60 min').compare('1 hour') // 0
646+
unit('90 min').compare('1 hour') // 1
647+
```
648+
639649
- `#lessThan(other: unit | string)`
640650

641651
Compares this and another unit and returns true if this unit is less than the other.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "unitmath",
3-
"version": "0.6.0",
3+
"version": "0.7.0",
44
"description": "JavaScript library for unit conversion and arithmetic",
55
"main": "index.js",
66
"module": "src/Unit.js",

src/Unit.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,26 @@ let _config = function _config (options) {
436436
return this.equalQuantity(other) && options.type.eq(value1, value2)
437437
}
438438

439+
/**
440+
* Compare this unit to another and return a value indicating whether this unit is less than, greater than, or equal to the other.
441+
* @param {Unit} other
442+
* @return {number} -1 if this unit is less than, 1 if this unit is greater than, and 0 if this unit is equal to the other unit.
443+
*/
444+
compare (other) {
445+
if (!options.type.conv._IS_UNITMATH_DEFAULT_FUNCTION && (options.type.gt._IS_UNITMATH_DEFAULT_FUNCTION || options.type.lt._IS_UNITMATH_DEFAULT_FUNCTION)) {
446+
throw new Error(`When using custom types, compare requires a type.gt and a type.lt function`)
447+
}
448+
other = _convertParamToUnit(other)
449+
let { value1, value2 } = _comparePrepare(this, other, true)
450+
if (options.type.lt(value1, value2)) {
451+
return -1
452+
} else if (options.type.gt(value1, value2)) {
453+
return 1
454+
} else {
455+
return 0
456+
}
457+
}
458+
439459
/**
440460
* Compare this unit to another and return whether this unit is less than the other.
441461
* @param {Unit} other

test/Unit.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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')), /Cannot compare units.*dimensions do not match/)
714+
assert.throws(() => unit(100, 'cm').compare(unit(1, 'kg')), /Cannot compare units.*dimensions do not match/)
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

Comments
 (0)