Skip to content

Commit

Permalink
fix: #3175 cannot delete units using math.Unit.deleteUnit
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong committed Mar 13, 2024
1 parent a41def8 commit becae37
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
4 changes: 2 additions & 2 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
- Fix #3172: simplify `"true and true"`.
- Fix #3163: `toTex` wrongly returning `Infinity` for large BigNumbers.
- Fix #3162: add license information about CSParse (#3164).
- Fix: expose `math.Unit.ALIASES` (see #3175).
- Fix #3175: cannot delete units using `math.Unit.deleteUnit`.
- Fix: faster startup time of the CLI and REPL by loading the bundle.
- Fix: remove using polyfill.io inside the example
pretty_printing_with_mathjax.html (#3167). Thanks @SukkaW.
`pretty_printing_with_mathjax.html` (#3167). Thanks @SukkaW.


# 2024-02-22, 12.4.0
Expand Down
9 changes: 6 additions & 3 deletions src/type/unit/Unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2767,6 +2767,7 @@ export const createUnitClass = /* #__PURE__ */ factory(name, dependencies, ({
}

// aliases (formerly plurals)
// note that ALIASES is only used at creation to create more entries in UNITS by copying the aliased units
const ALIASES = {
meters: 'meter',
inches: 'inch',
Expand Down Expand Up @@ -3327,15 +3328,18 @@ export const createUnitClass = /* #__PURE__ */ factory(name, dependencies, ({
alias.name = aliasName
Unit.UNITS[aliasName] = alias
}
// delete the memoization cache, since adding a new unit to the array
// invalidates all old results

// delete the memoization cache because we created a new unit
delete _findUnit.cache

return new Unit(null, name)
}

Unit.deleteUnit = function (name) {
delete Unit.UNITS[name]

// delete the memoization cache because we deleted a unit
delete _findUnit.cache
}

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

return Unit
}, { isClass: true })
23 changes: 23 additions & 0 deletions test/unit-tests/type/unit/Unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1324,6 +1324,29 @@ describe('Unit', function () {
})
})

describe('deleteUnit', function () {
it('should delete a unit', function () {
const math2 = math.create()

assert.strictEqual(math2.evaluate('5 b').toString(), '5 b')
assert.strictEqual(math2.evaluate('5 bytes').toString(), '5 bytes')
assert.strictEqual(math2.evaluate('5 byte').toString(), '5 byte') // alias of "bytes"

math2.Unit.deleteUnit('b')
math2.Unit.deleteUnit('bytes')
math2.Unit.deleteUnit('byte')

assert.throws(() => math2.evaluate('5 b').toString(), 'foo')
assert.throws(() => math2.evaluate('5 bytes').toString(), 'foo')
assert.throws(() => math2.evaluate('5 byte').toString(), 'foo')

// should not have changed the original math
assert.strictEqual(math.evaluate('5 b').toString(), '5 b')
assert.strictEqual(math.evaluate('5 bytes').toString(), '5 bytes')
assert.strictEqual(math.evaluate('5 byte').toString(), '5 byte')
})
})

describe('splitUnit', function () {
it('should split a unit into parts', function () {
assert.strictEqual((new Unit(1, 'm')).splitUnit(['ft', 'in']).toString(), '3 ft,3.3700787401574765 in')
Expand Down

0 comments on commit becae37

Please sign in to comment.