Skip to content

Commit 4abf247

Browse files
yoadsnnodkz
authored andcommitted
fix: Support builtin js objects in dotted notation
Converting a dotted notation shouldn't try and nest keys of builtin objects
1 parent 28d4e8d commit 4abf247

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/utils/__tests__/toMongoDottedObject-test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,16 @@ describe('toMongoDottedObject()', () => {
2828
'a.b': 3,
2929
});
3030
});
31+
32+
it('should handle date object values as scalars', () => {
33+
expect(toMongoDottedObject({ dateField: new Date(100) })).toEqual({
34+
dateField: new Date(100),
35+
});
36+
});
37+
38+
it('should handle date object values when nested', () => {
39+
expect(toMongoDottedObject({ a: { dateField: new Date(100) } })).toEqual({
40+
'a.dateField': new Date(100),
41+
});
42+
});
3143
});

src/utils/toMongoDottedObject.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ export default function toMongoDottedObject(
1919
target?: Object = {},
2020
path?: string[] = []
2121
): { [dottedPath: string]: mixed } {
22+
const objKeys = Object.keys(obj);
23+
2224
/* eslint-disable */
23-
Object.keys(obj).forEach(key => {
25+
objKeys.forEach(key => {
2426
if (key.startsWith('$')) {
2527
target[path.join('.')] = {
2628
...target[path.join('.')],
@@ -32,6 +34,11 @@ export default function toMongoDottedObject(
3234
target[path.concat(key).join('.')] = obj[key];
3335
}
3436
});
37+
38+
if (objKeys.length === 0) {
39+
target[path.join('.')] = obj;
40+
}
41+
3542
return target;
3643
/* eslint-enable */
3744
}

0 commit comments

Comments
 (0)