Skip to content

Commit becae37

Browse files
committed
fix: #3175 cannot delete units using math.Unit.deleteUnit
1 parent a41def8 commit becae37

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

HISTORY.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
- Fix #3172: simplify `"true and true"`.
99
- Fix #3163: `toTex` wrongly returning `Infinity` for large BigNumbers.
1010
- Fix #3162: add license information about CSParse (#3164).
11-
- Fix: expose `math.Unit.ALIASES` (see #3175).
11+
- Fix #3175: cannot delete units using `math.Unit.deleteUnit`.
1212
- Fix: faster startup time of the CLI and REPL by loading the bundle.
1313
- Fix: remove using polyfill.io inside the example
14-
pretty_printing_with_mathjax.html (#3167). Thanks @SukkaW.
14+
`pretty_printing_with_mathjax.html` (#3167). Thanks @SukkaW.
1515

1616

1717
# 2024-02-22, 12.4.0

src/type/unit/Unit.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2767,6 +2767,7 @@ export const createUnitClass = /* #__PURE__ */ factory(name, dependencies, ({
27672767
}
27682768

27692769
// aliases (formerly plurals)
2770+
// note that ALIASES is only used at creation to create more entries in UNITS by copying the aliased units
27702771
const ALIASES = {
27712772
meters: 'meter',
27722773
inches: 'inch',
@@ -3327,15 +3328,18 @@ export const createUnitClass = /* #__PURE__ */ factory(name, dependencies, ({
33273328
alias.name = aliasName
33283329
Unit.UNITS[aliasName] = alias
33293330
}
3330-
// delete the memoization cache, since adding a new unit to the array
3331-
// invalidates all old results
3331+
3332+
// delete the memoization cache because we created a new unit
33323333
delete _findUnit.cache
33333334

33343335
return new Unit(null, name)
33353336
}
33363337

33373338
Unit.deleteUnit = function (name) {
33383339
delete Unit.UNITS[name]
3340+
3341+
// delete the memoization cache because we deleted a unit
3342+
delete _findUnit.cache
33393343
}
33403344

33413345
// expose arrays with prefixes, dimensions, units, systems
@@ -3344,7 +3348,6 @@ export const createUnitClass = /* #__PURE__ */ factory(name, dependencies, ({
33443348
Unit.BASE_UNITS = BASE_UNITS
33453349
Unit.UNIT_SYSTEMS = UNIT_SYSTEMS
33463350
Unit.UNITS = UNITS
3347-
Unit.ALIASES = ALIASES
33483351

33493352
return Unit
33503353
}, { isClass: true })

test/unit-tests/type/unit/Unit.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,6 +1324,29 @@ describe('Unit', function () {
13241324
})
13251325
})
13261326

1327+
describe('deleteUnit', function () {
1328+
it('should delete a unit', function () {
1329+
const math2 = math.create()
1330+
1331+
assert.strictEqual(math2.evaluate('5 b').toString(), '5 b')
1332+
assert.strictEqual(math2.evaluate('5 bytes').toString(), '5 bytes')
1333+
assert.strictEqual(math2.evaluate('5 byte').toString(), '5 byte') // alias of "bytes"
1334+
1335+
math2.Unit.deleteUnit('b')
1336+
math2.Unit.deleteUnit('bytes')
1337+
math2.Unit.deleteUnit('byte')
1338+
1339+
assert.throws(() => math2.evaluate('5 b').toString(), 'foo')
1340+
assert.throws(() => math2.evaluate('5 bytes').toString(), 'foo')
1341+
assert.throws(() => math2.evaluate('5 byte').toString(), 'foo')
1342+
1343+
// should not have changed the original math
1344+
assert.strictEqual(math.evaluate('5 b').toString(), '5 b')
1345+
assert.strictEqual(math.evaluate('5 bytes').toString(), '5 bytes')
1346+
assert.strictEqual(math.evaluate('5 byte').toString(), '5 byte')
1347+
})
1348+
})
1349+
13271350
describe('splitUnit', function () {
13281351
it('should split a unit into parts', function () {
13291352
assert.strictEqual((new Unit(1, 'm')).splitUnit(['ft', 'in']).toString(), '3 ft,3.3700787401574765 in')

0 commit comments

Comments
 (0)