Skip to content

Commit

Permalink
fix: PartitionedMap and ObjectWrappingMap missing a property
Browse files Browse the repository at this point in the history
  `Symbol.iterator`
  • Loading branch information
josdejong committed Feb 21, 2024
1 parent 085b855 commit a7f84ab
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

- Feat: implement support for trailing commas in matrices (#3154, #2968).
Thanks @dvd101x.
- Fix: `PartitionedMap` and `ObjectWrappingMap` missing a property
`Symbol.iterator`, causing problems when trying `new Map(scope)`. See
https://github.com/josdejong/mathjs/discussions/3156#discussioncomment-8516994
- Docs: describe method `getAllAsMap` in the Parser docs (#3158, #3157).
Thanks @dvd101x.

Expand Down
4 changes: 4 additions & 0 deletions src/utils/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { isObject } from './is.js'
export class ObjectWrappingMap {
constructor (object) {
this.wrappedObject = object

this[Symbol.iterator] = this.entries
}

keys () {
Expand Down Expand Up @@ -80,6 +82,8 @@ export class PartitionedMap {
this.a = a
this.b = b
this.bKeys = bKeys

this[Symbol.iterator] = this.entries
}

get (key) {
Expand Down
14 changes: 14 additions & 0 deletions test/unit-tests/utils/map.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ describe('maps', function () {
const innerObject = toObject(map)
assert.strictEqual(innerObject, obj)

// Create a new Map
const copy = new Map(map)
assert.deepStrictEqual([...copy.keys()], [...map.keys()])

// clear
map.clear()
assert.deepStrictEqual([...map.keys()], [])
Expand Down Expand Up @@ -214,6 +218,16 @@ describe('maps', function () {
assert.deepStrictEqual(it.next(), { done: true, value: undefined })
})

it('copy', function () {
const { a, b, p } = createPartitionedMap(['b'])

Check failure on line 222 in test/unit-tests/utils/map.test.js

View workflow job for this annotation

GitHub Actions / build-and-test

'a' is assigned a value but never used

Check failure on line 222 in test/unit-tests/utils/map.test.js

View workflow job for this annotation

GitHub Actions / build-and-test

'b' is assigned a value but never used

Check failure on line 222 in test/unit-tests/utils/map.test.js

View workflow job for this annotation

GitHub Actions / lint

'a' is assigned a value but never used

Check failure on line 222 in test/unit-tests/utils/map.test.js

View workflow job for this annotation

GitHub Actions / lint

'b' is assigned a value but never used
p
.set('a', 2)
.set('b', 3)

const copy = new Map(p)
assert.deepStrictEqual([...copy.keys()], [...p.keys()])
})

it('size', function () {
const { p } = createPartitionedMap(['b'])
p.set('a', 2)
Expand Down

0 comments on commit a7f84ab

Please sign in to comment.