Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check for nullable source on toJS() #1783

Merged
merged 2 commits into from
Oct 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/api/tojs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@ function toJSHelper(source, options: ToJSOptions, __alreadySeen: Map<any, any>)
if (!options.recurseEverything && !isObservable(source)) return source

if (typeof source !== "object") return source

// Directly return null if source is null
if (source === null) return null
wangyiz4262 marked this conversation as resolved.
Show resolved Hide resolved

// Directly return the Date object itself if contained in the observable
if (source instanceof Date) return source

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

// make sure we track the keys of the object
if (isObservable(source)) {
keys(source)
}
if (isObservable(source)) keys(source)

const detectCycles = options.detectCycles === true

Expand Down
5 changes: 5 additions & 0 deletions test/base/tojs.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,4 +403,9 @@ describe("recurseEverything set to true", function() {
const convertedObj = mobx.toJS({ key: cycledObj }, { recurseEverything: true })
expect(convertedObj.key).toBe(convertedObj.key.cycle)
})

test("should return null if source is null", function() {
expect(mobx.toJS(null)).toBeNull()
expect(mobx.toJS(null, { recurseEverything: true })).toBeNull()
})
})