Skip to content

Commit

Permalink
Implemented Symbol.toStringTag for observable arrays, fixes #1490
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed Apr 16, 2018
1 parent 394c418 commit b21b5e2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/types/observablearray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,14 @@ Object.defineProperty(ObservableArray.prototype, "length", {
}
})

if (typeof Symbol !== "undefined" && Symbol.toStringTag) {
addHiddenProp(
ObservableArray.prototype,
typeof Symbol !== "undefined" ? Symbol.toStringTag : "@@toStringTag" as any,
"Array"
)
}

// Internet Explorer on desktop doesn't support this.....
// So, let's don't do this to avoid different semantics
// See #1395
Expand Down
2 changes: 1 addition & 1 deletion src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export function makeNonEnumerable(object: any, propNames: string[]) {
}
}

export function addHiddenProp(object: any, propName: string, value: any) {
export function addHiddenProp(object: any, propName: PropertyKey, value: any) {
Object.defineProperty(object, propName, {
enumerable: false,
writable: true,
Expand Down
7 changes: 7 additions & 0 deletions test/base/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -504,3 +504,10 @@ test("array is spreadable, #1395", () => {
const y = mobx.observable([])
expect([5, ...y]).toEqual([5])
})

test("array supports toStringTag, #1490", () => {
// N.B. on old environments this requires polyfils for these symbols *and* Object.prototype.toString.
// core-js provides both
const a = mobx.observable([])
expect(Object.prototype.toString.call(a)).toBe("[object Array]")
})

0 comments on commit b21b5e2

Please sign in to comment.