Skip to content

Commit 24a1a8c

Browse files
authored
[Quantity] Fix .unit returns null if there is valid unit (#449)
Fixes #445. Signed-off-by: Florian Hotze <dev@florianhotze.com>
1 parent 0f712ff commit 24a1a8c

5 files changed

Lines changed: 12 additions & 9 deletions

File tree

src/quantity.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,17 @@ class Quantity {
124124
}
125125

126126
/**
127-
* Unit of this Quantity, e.g. `Metre`, or `null` if not available
127+
* Unit name of this Quantity, e.g. `Metre`, `kWh`, or `null` if not available
128128
* @type {string|null}
129129
*/
130130
get unit () {
131-
const unit = this.rawQtyType.getUnit().getName();
131+
const rawUnit = this.rawQtyType.getUnit();
132+
const unit = rawUnit.getName() ?? rawUnit;
132133
return (unit === null) ? null : unit.toString();
133134
}
134135

135136
/**
136-
* Unit symbol of this Quantity, e.g. `m`, or `null` if not available
137+
* Unit symbol of this Quantity, e.g. `m`, `kWh`, or `null` if not available
137138
* @type {string|null}
138139
*/
139140
get symbol () {

test/javax-measure.mock.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// javax.measure.Unit (https://unitsofmeasurement.github.io/unit-api/site/apidocs/javax/measure/Unit.html)
22
class Unit {
33
getName = jest.fn()
4+
toString = jest.fn()
45
}
56

67
module.exports = { Unit };

test/quantity.spec.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@ describe('quantity.js', () => {
3939
});
4040

4141
it('unit delegates & returns', () => {
42-
unitSpy.getName.mockImplementation(() => 'Metres');
42+
unitSpy.getName.mockImplementation(() => 'Metre');
4343
let unit = getQuantity('5 m').unit;
44-
expect(unit).toBe('Metres');
44+
expect(unit).toBe('Metre');
4545

4646
unitSpy.getName.mockImplementation(() => null);
47+
unitSpy.toString.mockImplementation(() => 'Metre');
4748
unit = getQuantity('5 m').unit;
48-
expect(unit).toBe(null);
49+
expect(unit).toBe('Metre');
4950

5051
expect(unitSpy.getName).toHaveBeenCalledTimes(2);
5152
expect(quantityTypeSpy.getUnit).toHaveBeenCalledTimes(2);

types/quantity.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,12 @@ export class Quantity {
9494
*/
9595
get dimension(): string;
9696
/**
97-
* Unit of this Quantity, e.g. `Metre`, or `null` if not available
97+
* Unit name of this Quantity, e.g. `Metre`, `kWh`, or `null` if not available
9898
* @type {string|null}
9999
*/
100100
get unit(): string;
101101
/**
102-
* Unit symbol of this Quantity, e.g. `m`, or `null` if not available
102+
* Unit symbol of this Quantity, e.g. `m`, `kWh`, or `null` if not available
103103
* @type {string|null}
104104
*/
105105
get symbol(): string;

types/quantity.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)