Skip to content

Commit 1bd4450

Browse files
epallposva
authored andcommitted
fix: properly copy Dates
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.
1 parent 1df56c4 commit 1bd4450

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/utils.js

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export function extractRefs (doc, oldDoc, path = '', result = [{}, {}]) {
2727
// TODO handle array
2828
tot[0][key] = Array(ref.length).fill(null)
2929
extractRefs(ref, oldDoc[key], path + key + '.', [tot[0][key], tot[1]])
30+
} else if (ref instanceof Date) {
31+
tot[0][key] = ref
3032
} else if (isObject(ref)) {
3133
tot[0][key] = {}
3234
extractRefs(ref, oldDoc[key], path + key + '.', [tot[0][key], tot[1]])

test/utils.spec.js

+11
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,17 @@ test('extract refs from document', () => {
5454
})
5555
})
5656

57+
test('leave Date objects alone when extracting refs', () => {
58+
const d = new Date()
59+
const [doc, refs] = extractRefs({
60+
foo: 1,
61+
bar: d
62+
})
63+
expect(doc.foo).toEqual(1)
64+
expect(doc.bar).toEqual(d)
65+
expect(refs).toEqual({})
66+
})
67+
5768
test('extract object nested refs from document', () => {
5869
const [noRefsDoc, refs] = extractRefs({
5970
obj: {

0 commit comments

Comments
 (0)