From 6daf13a0fd67fcb3cdd2757aa7b4beed53d046eb Mon Sep 17 00:00:00 2001 From: Carl Osterwisch Date: Fri, 22 Dec 2023 20:17:57 -0600 Subject: [PATCH 1/5] Add test for conversion to unitless quantity --- test/unit-tests/type/unit/Unit.test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/unit-tests/type/unit/Unit.test.js b/test/unit-tests/type/unit/Unit.test.js index c8f475d1ff..6793491a8b 100644 --- a/test/unit-tests/type/unit/Unit.test.js +++ b/test/unit-tests/type/unit/Unit.test.js @@ -393,6 +393,7 @@ describe('Unit', function () { it('should convert a unitless quantity', function () { const u = Unit.parse('5', { allowNoUnits: true }) assert.strictEqual(u.toNumeric(), 5) + assert.strictEqual(u.toNumeric('mm/m'), 5000) }) it('should convert a binary prefixes (1)', function () { From f8537db61b8684e2e20f522e60115ae6e3b907a4 Mon Sep 17 00:00:00 2001 From: Carl Osterwisch Date: Fri, 22 Dec 2023 20:19:18 -0600 Subject: [PATCH 2/5] Avoid access to missing array index --- src/type/unit/Unit.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/type/unit/Unit.js b/src/type/unit/Unit.js index 9379460203..7a88b78305 100644 --- a/src/type/unit/Unit.js +++ b/src/type/unit/Unit.js @@ -828,6 +828,7 @@ export const createUnitClass = /* #__PURE__ */ factory(name, dependencies, ({ } if (this.value === null || this._isDerived() || + this.units.length === 0 || this.units[0].unit.offset === other.units[0].unit.offset) { other.value = clone(value) } else { From a9f686b9149e6e238003e536a87c4e418bb8e90f Mon Sep 17 00:00:00 2001 From: Carl Osterwisch Date: Wed, 27 Dec 2023 13:18:45 -0500 Subject: [PATCH 3/5] Also check that other.units is not empty --- src/type/unit/Unit.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/type/unit/Unit.js b/src/type/unit/Unit.js index 7a88b78305..05bc89626c 100644 --- a/src/type/unit/Unit.js +++ b/src/type/unit/Unit.js @@ -828,7 +828,7 @@ export const createUnitClass = /* #__PURE__ */ factory(name, dependencies, ({ } if (this.value === null || this._isDerived() || - this.units.length === 0 || + this.units.length === 0 || other.units.length === 0 || this.units[0].unit.offset === other.units[0].unit.offset) { other.value = clone(value) } else { From a8017ef993fbae1e852f82aa3bb43df7b0652a8e Mon Sep 17 00:00:00 2001 From: Carl Osterwisch Date: Wed, 27 Dec 2023 13:19:34 -0500 Subject: [PATCH 4/5] Add test for abs of dimensionless unit --- test/unit-tests/function/arithmetic/abs.test.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/unit-tests/function/arithmetic/abs.test.js b/test/unit-tests/function/arithmetic/abs.test.js index a737ba37e9..f1392927e9 100644 --- a/test/unit-tests/function/arithmetic/abs.test.js +++ b/test/unit-tests/function/arithmetic/abs.test.js @@ -84,6 +84,9 @@ describe('abs', function () { u = abs(unit(complex(-4, 3), 'in')) assert.strictEqual(u.toString(), '5 in') + + u = abs(unit(-10)) // dimensionless unit + assert.strictEqual(u.toString(), '10') }) it('should throw an error in case of invalid number of arguments', function () { From cda62c6303cd0aebc6d37db5e2020c525c95d6af Mon Sep 17 00:00:00 2001 From: Carl Osterwisch Date: Wed, 27 Dec 2023 13:24:56 -0500 Subject: [PATCH 5/5] Fix: avoid access to missing units array index --- src/type/unit/Unit.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/type/unit/Unit.js b/src/type/unit/Unit.js index 05bc89626c..e923113720 100644 --- a/src/type/unit/Unit.js +++ b/src/type/unit/Unit.js @@ -780,7 +780,7 @@ export const createUnitClass = /* #__PURE__ */ factory(name, dependencies, ({ Unit.prototype.abs = function () { const ret = this.clone() if (ret.value !== null) { - if (ret._isDerived() || ret.units[0].unit.offset === 0) { + if (ret._isDerived() || ret.units.length === 0 || ret.units[0].unit.offset === 0) { ret.value = abs(ret.value) } else { // To give the correct, but unexpected, results for units with an offset.