Skip to content

Commit

Permalink
Published version 4.9.4
Browse files Browse the repository at this point in the history
  • Loading branch information
pimterry authored and mweststrate committed Mar 30, 2019
1 parent 01f6e92 commit 2b09e99
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 16 deletions.
2 changes: 0 additions & 2 deletions CHANGELOG.md
@@ -1,5 +1,3 @@
# For latest changes, see CHANGELOG on master

Changelog continues [here](https://github.com/mobxjs/mobx/blob/master/CHANGELOG.md)

---
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "mobx",
"version": "4.9.3",
"version": "4.9.4",
"description": "Simple, scalable state management.",
"main": "lib/mobx.js",
"umd:main": "lib/mobx.umd.js",
Expand Down
22 changes: 17 additions & 5 deletions src/types/observablemap.ts
Expand Up @@ -91,7 +91,7 @@ export class ObservableMap<K = any, V = any>
// eslint-disable-next-line
[Symbol.iterator]: () => IterableIterator<[K, V]>; // only used for typings!
// eslint-disable-next-line
[Symbol.toStringTag]: 'Map' // only used for typings!
[Symbol.toStringTag]: "Map" // only used for typings!

constructor(
initialData?: IObservableMapInitialValues<K, V>,
Expand Down Expand Up @@ -180,7 +180,12 @@ export class ObservableMap<K = any, V = any>
if (entry) {
entry.setNewValue(value)
} else {
entry = new ObservableValue(value, referenceEnhancer, `${this.name}.${key}?`, false)
entry = new ObservableValue(
value,
referenceEnhancer,
`${this.name}.${stringifyKey(key)}?`,
false
)
this._hasMap.set(key, entry)
}
return entry
Expand Down Expand Up @@ -214,7 +219,7 @@ export class ObservableMap<K = any, V = any>
const observable = new ObservableValue(
newValue,
this.enhancer,
`${this.name}.${key}`,
`${this.name}.${stringifyKey(key)}`,
false
)
this._data.set(key, observable)
Expand Down Expand Up @@ -340,7 +345,9 @@ export class ObservableMap<K = any, V = any>
*/
toPOJO(): IKeyValueMap<V> {
const res: IKeyValueMap<V> = {}
this._keys.forEach(key => (res["" + key] = this.get(key)!))
this._keys.forEach(
key => (res[typeof key === "symbol" ? <any>key : stringifyKey(key)] = this.get(key)!)
)
return res
}

Expand All @@ -363,7 +370,7 @@ export class ObservableMap<K = any, V = any>
return (
this.name +
"[{ " +
this._keys.map(key => `${key}: ${"" + this.get(key)}`).join(", ") +
this._keys.map(key => `${stringifyKey(key)}: ${"" + this.get(key)}`).join(", ") +
" }]"
)
}
Expand All @@ -387,6 +394,11 @@ export class ObservableMap<K = any, V = any>
}
}

function stringifyKey(key: any): string {
if (key && key.toString) return key.toString()
else return new String(key).toString()
}

declareIterator(ObservableMap.prototype, function() {
return this.entries()
})
Expand Down
27 changes: 19 additions & 8 deletions test/base/map.js
Expand Up @@ -31,14 +31,23 @@ test("map crud", function() {
expect(m.has(k)).toBe(true)
expect(m.get(k)).toBe("arrVal")

expect(mobx.keys(m)).toEqual(["1", 1, k])
expect(mobx.values(m)).toEqual(["aa", "b", "arrVal"])
expect(Array.from(m)).toEqual([["1", "aa"], [1, "b"], [k, "arrVal"]])
expect(m.toJS()).toEqual(new Map([["1", "aa"], [1, "b"], [k, "arrVal"]]))
expect(m.toPOJO()).toEqual({ "1": "b", arr: "arrVal" })
var s = Symbol("test")
expect(m.has(s)).toBe(false)
expect(m.get(s)).toBe(undefined)
m.set(s, "symbol-value")
expect(m.get(s)).toBe("symbol-value")
expect(m.get(s.toString())).toBe(undefined)

expect(mobx.keys(m)).toEqual(["1", 1, k, s])
expect(mobx.values(m)).toEqual(["aa", "b", "arrVal", "symbol-value"])
expect(Array.from(m)).toEqual([["1", "aa"], [1, "b"], [k, "arrVal"], [s, "symbol-value"]])
expect(m.toJS()).toEqual(new Map([["1", "aa"], [1, "b"], [k, "arrVal"], [s, "symbol-value"]]))
expect(m.toPOJO()).toEqual({ "1": "b", arr: "arrVal", [s]: "symbol-value" })
expect(JSON.stringify(m)).toEqual('{"1":"b","arr":"arrVal"}')
expect(m.toString()).toBe("ObservableMap@1[{ 1: aa, 1: b, arr: arrVal }]")
expect(m.size).toBe(3)
expect(m.toString()).toBe(
"ObservableMap@1[{ 1: aa, 1: b, arr: arrVal, Symbol(test): symbol-value }]"
)
expect(m.size).toBe(4)

m.clear()
expect(mobx.keys(m)).toEqual([])
Expand All @@ -56,9 +65,11 @@ test("map crud", function() {
{ object: m, name: "1", newValue: "aa", oldValue: "a", type: "update" },
{ object: m, name: 1, newValue: "b", type: "add" },
{ object: m, name: ["arr"], newValue: "arrVal", type: "add" },
{ object: m, name: s, newValue: "symbol-value", type: "add" },
{ object: m, name: "1", oldValue: "aa", type: "delete" },
{ object: m, name: 1, oldValue: "b", type: "delete" },
{ object: m, name: ["arr"], oldValue: "arrVal", type: "delete" }
{ object: m, name: ["arr"], oldValue: "arrVal", type: "delete" },
{ object: m, name: s, oldValue: "symbol-value", type: "delete" }
])
})

Expand Down

0 comments on commit 2b09e99

Please sign in to comment.