Skip to content

Commit

Permalink
The object will be recursively converted when calling toJS on it
Browse files Browse the repository at this point in the history
fix tests

Fix some language inaccuracy
  • Loading branch information
Yizhe Wang authored and wangyiz4262 committed Sep 20, 2018
1 parent ed4827f commit 54b3298
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
33 changes: 13 additions & 20 deletions src/api/tojs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,25 @@ function cache<K, V>(map: Map<any, any>, key: K, value: V, options: ToJSOptions)
}

function toJSHelper(source, options: ToJSOptions, __alreadySeen: Map<any, any>) {
if (!isObservable(source)) return source
if (typeof source !== "object") {
return source
}

const detectCycles = options.detectCycles === true

if (
detectCycles &&
source !== null &&
typeof source === "object" &&
__alreadySeen.has(source)
) {
if (detectCycles && source !== null && __alreadySeen.has(source)) {
return __alreadySeen.get(source)
}

if (isObservableArray(source)) {
if (isObservableArray(source) || Object.getPrototypeOf(source) === Array.prototype) {
const res = cache(__alreadySeen, source, [] as any, options)
const toAdd = source.map(value => toJSHelper(value, options!, __alreadySeen))
res.length = toAdd.length
for (let i = 0, l = toAdd.length; i < l; i++) res[i] = toAdd[i]
return res
}

if (isObservableObject(source)) {
const res = cache(__alreadySeen, source, {}, options)
keys(source) // make sure we track the keys of the object
for (let key in source) {
res[key] = toJSHelper(source[key], options!, __alreadySeen)
}
return res
}

if (isObservableMap(source)) {
if (isObservableMap(source) || Object.getPrototypeOf(source) === Map.prototype) {
if (options.exportMapsAsObjects === false) {
const res = cache(__alreadySeen, source, new Map(), options)
source.forEach((value, key) => {
Expand All @@ -71,6 +59,13 @@ function toJSHelper(source, options: ToJSOptions, __alreadySeen: Map<any, any>)

if (isObservableValue(source)) return toJSHelper(source.get(), options!, __alreadySeen)

// Fallback to situation if source is an ObservableObject or a plain object
const res = cache(__alreadySeen, source, {}, options)
for (let key in source) {
res[key] = toJSHelper(source[key], options!, __alreadySeen)
}
return res

return source
}

Expand All @@ -81,8 +76,6 @@ export function toJS<T>(source: T, options?: ToJSOptions): T
export function toJS(source: any, options?: ToJSOptions): any
export function toJS(source, options: ToJSOptions) // internal overload
export function toJS(source, options?: ToJSOptions) {
if (!isObservable(source)) return source

// backward compatibility
if (typeof options === "boolean") options = { detectCycles: options }
if (!options) options = defaultOptions
Expand Down
2 changes: 1 addition & 1 deletion test/base/tojs.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ test("#285 non-mobx class instances with toJS", () => {
// check before lazy initialization
expect(mobx.toJS(p1)).toEqual({
firstName: "michel",
lastName: nameObservable // toJS doesn't recurse into non observable objects!
lastName: "weststrate" // toJS will recurse into any object that may contain observable value
})
})

Expand Down

0 comments on commit 54b3298

Please sign in to comment.