Skip to content

Commit

Permalink
add isPlainES6Map()
Browse files Browse the repository at this point in the history
  • Loading branch information
g6123 committed Jun 28, 2024
1 parent 5881536 commit 103d4b0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/mobx/src/types/observablemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
hasListeners,
interceptChange,
isES6Map,
isPlainES6Map,
isPlainObject,
isSpyEnabled,
makeIterable,
Expand Down Expand Up @@ -351,7 +352,7 @@ export class ObservableMap<K = any, V = any>
} else if (Array.isArray(other)) {
other.forEach(([key, value]) => this.set(key, value))
} else if (isES6Map(other)) {
if (other.constructor.name !== "Map") {
if (!isPlainES6Map(other)) {
die(19, other)
}
other.forEach((value, key) => this.set(key, value))
Expand Down
7 changes: 7 additions & 0 deletions packages/mobx/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ export function isES6Map(thing: any): thing is Map<any, any> {
return thing != null && Object.prototype.toString.call(thing) === "[object Map]"
}

export function isPlainES6Map(thing: Map<any, any>): boolean {
const mapProto = Object.getPrototypeOf(thing)
const objectProto = Object.getPrototypeOf(mapProto)
const nullProto = Object.getPrototypeOf(objectProto)
return nullProto === null
}

export function isES6Set(thing: any): thing is Set<any> {
return thing != null && Object.prototype.toString.call(thing) === "[object Set]"
}
Expand Down

0 comments on commit 103d4b0

Please sign in to comment.