Skip to content

Commit

Permalink
fix: properly copy Dates
Browse files Browse the repository at this point in the history
Fix #164
Firestore has native support for timestamps, which show up in JS as `Date` objects. We need a special case in `extractRefs` because recursing into a `Date` and copying its fields doesn't produce a proper copy. Instead we should pass it through the same way we do with more fundamental types, like strings or numbers.
  • Loading branch information
epall authored and posva committed Feb 14, 2018
1 parent 1df56c4 commit 1bd4450
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export function extractRefs (doc, oldDoc, path = '', result = [{}, {}]) {
// TODO handle array
tot[0][key] = Array(ref.length).fill(null)
extractRefs(ref, oldDoc[key], path + key + '.', [tot[0][key], tot[1]])
} else if (ref instanceof Date) {
tot[0][key] = ref
} else if (isObject(ref)) {
tot[0][key] = {}
extractRefs(ref, oldDoc[key], path + key + '.', [tot[0][key], tot[1]])
Expand Down
11 changes: 11 additions & 0 deletions test/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ test('extract refs from document', () => {
})
})

test('leave Date objects alone when extracting refs', () => {
const d = new Date()
const [doc, refs] = extractRefs({
foo: 1,
bar: d
})
expect(doc.foo).toEqual(1)
expect(doc.bar).toEqual(d)
expect(refs).toEqual({})
})

test('extract object nested refs from document', () => {
const [noRefsDoc, refs] = extractRefs({
obj: {
Expand Down

0 comments on commit 1bd4450

Please sign in to comment.