From a2f8bd872ae9759c1006952b7c2ca80d2e2572f6 Mon Sep 17 00:00:00 2001 From: Yauheni Date: Fri, 27 Oct 2023 13:26:39 +0200 Subject: [PATCH] fix: deep map (CT-000) --- packages/common/src/utils/object/common.ts | 3 +++ packages/common/src/utils/object/deepMap.ts | 6 +++--- .../common/tests/utils/object/deepMap.unit.ts | 17 +++++++++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/packages/common/src/utils/object/common.ts b/packages/common/src/utils/object/common.ts index b9819cc15..2fd184eef 100644 --- a/packages/common/src/utils/object/common.ts +++ b/packages/common/src/utils/object/common.ts @@ -1,4 +1,5 @@ import { AnyRecord, Struct } from '@common/types'; +import _isPlainObject from 'lodash/isPlainObject'; export { default as shallowEquals } from 'shallowequal'; @@ -15,6 +16,8 @@ export const selectValue = selectField('value'); export const isObject = (obj: unknown): obj is Struct => obj !== null && typeof obj === 'object'; +export const isPlainObject = (obj: unknown): obj is Struct => _isPlainObject(obj); + export const hasProperty = (obj: T, key: K): obj is T & Record => Object.prototype.hasOwnProperty.call(obj, key); diff --git a/packages/common/src/utils/object/deepMap.ts b/packages/common/src/utils/object/deepMap.ts index 3f2fbfe76..9bececc3c 100644 --- a/packages/common/src/utils/object/deepMap.ts +++ b/packages/common/src/utils/object/deepMap.ts @@ -1,6 +1,6 @@ import { Struct } from '@common/types'; -import { isObject } from './common'; +import { isPlainObject } from './common'; export const deepMap = ( object: unknown, @@ -45,7 +45,7 @@ export const deepMap = ( const map = (value: unknown, key?: string | number) => { if (Array.isArray(value)) return mapArray(value); - if (isObject(value)) return mapObject(value); + if (isPlainObject(value)) return mapObject(value); return mapFunction(value, key!); }; @@ -92,7 +92,7 @@ export const deepMapKeys = (object: unknown, mapFunction: (key: stri const map = (value: unknown) => { if (Array.isArray(value)) return mapArray(value); - if (isObject(value)) return mapObject(value); + if (isPlainObject(value)) return mapObject(value); return value; }; diff --git a/packages/common/tests/utils/object/deepMap.unit.ts b/packages/common/tests/utils/object/deepMap.unit.ts index cd651a016..c00103960 100644 --- a/packages/common/tests/utils/object/deepMap.unit.ts +++ b/packages/common/tests/utils/object/deepMap.unit.ts @@ -1,4 +1,5 @@ import { deepMap, deepMapKeys } from '@common/utils/object/deepMap'; +import ObjectID from 'bson-objectid'; import { expect } from 'chai'; import sinon from 'sinon'; @@ -14,6 +15,22 @@ describe('Utils | object | deepMap', () => { expect(deepMap(['2', 3], transform)).to.eql([4, 9]); }); + it("don't change non plain objects", () => { + const transform = (n: unknown) => (typeof n === 'string' ? Number(n) ** 2 : n); + const date = new Date(); + + expect(deepMap(['2', date], transform)).to.eql([4, date]); + }); + + it('transform objectID', () => { + const originalID = new ObjectID(); + const transformedID = new ObjectID(); + const transform = (n: unknown) => (n === originalID ? transformedID : n); + const date = new Date(); + + expect(deepMap(['2', date, originalID], transform)).to.eql(['2', date, transformedID]); + }); + it('transforms object with nested objects/arrays', () => { expect(deepMap({ two: '2', obj: { three: '3', four: 4 }, arr: [5, '6'] }, transform)).to.eql({ two: 4,