Skip to content
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
12 changes: 12 additions & 0 deletions src/utils/__tests__/toMongoDottedObject-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,16 @@ describe('toMongoDottedObject()', () => {
'a.b': 3,
});
});

it('should handle date object values as scalars', () => {
expect(toMongoDottedObject({ dateField: new Date(100) })).toEqual({
dateField: new Date(100),
});
});

it('should handle date object values when nested', () => {
expect(toMongoDottedObject({ a: { dateField: new Date(100) } })).toEqual({
'a.dateField': new Date(100),
});
});
});
9 changes: 8 additions & 1 deletion src/utils/toMongoDottedObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ export default function toMongoDottedObject(
target?: Object = {},
path?: string[] = []
): { [dottedPath: string]: mixed } {
const objKeys = Object.keys(obj);

/* eslint-disable */
Object.keys(obj).forEach(key => {
objKeys.forEach(key => {
if (key.startsWith('$')) {
target[path.join('.')] = {
...target[path.join('.')],
Expand All @@ -32,6 +34,11 @@ export default function toMongoDottedObject(
target[path.concat(key).join('.')] = obj[key];
}
});

if (objKeys.length === 0) {
target[path.join('.')] = obj;
}

return target;
/* eslint-enable */
}