From 0e19e3812bdc0032cacd4fb3eff6df14236b55cb Mon Sep 17 00:00:00 2001 From: Yoad Snapir Date: Mon, 4 Sep 2017 11:21:33 +0300 Subject: [PATCH] fix: Support builtin js objects in dotted notation Converting a dotted notation shouldn't try and nest keys of builtin objects --- src/utils/__tests__/toMongoDottedObject-test.js | 12 ++++++++++++ src/utils/toMongoDottedObject.js | 9 ++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/utils/__tests__/toMongoDottedObject-test.js b/src/utils/__tests__/toMongoDottedObject-test.js index ef41cdf2..faa2baf3 100644 --- a/src/utils/__tests__/toMongoDottedObject-test.js +++ b/src/utils/__tests__/toMongoDottedObject-test.js @@ -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), + }); + }); }); diff --git a/src/utils/toMongoDottedObject.js b/src/utils/toMongoDottedObject.js index a35912a3..eea83752 100644 --- a/src/utils/toMongoDottedObject.js +++ b/src/utils/toMongoDottedObject.js @@ -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('.')], @@ -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 */ }