Skip to content

Commit

Permalink
fix: deep map (CT-000)
Browse files Browse the repository at this point in the history
  • Loading branch information
z4o4z committed Oct 30, 2023
1 parent 1cc9d18 commit a2f8bd8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages/common/src/utils/object/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AnyRecord, Struct } from '@common/types';
import _isPlainObject from 'lodash/isPlainObject';

export { default as shallowEquals } from 'shallowequal';

Expand All @@ -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 = <T, K extends keyof T | string>(obj: T, key: K): obj is T & Record<K, unknown> =>
Object.prototype.hasOwnProperty.call(obj, key);

Expand Down
6 changes: 3 additions & 3 deletions packages/common/src/utils/object/deepMap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Struct } from '@common/types';

import { isObject } from './common';
import { isPlainObject } from './common';

export const deepMap = <T = Struct>(
object: unknown,
Expand Down Expand Up @@ -45,7 +45,7 @@ export const deepMap = <T = Struct>(

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!);
};
Expand Down Expand Up @@ -92,7 +92,7 @@ export const deepMapKeys = <T = Struct>(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;
};
Expand Down
17 changes: 17 additions & 0 deletions packages/common/tests/utils/object/deepMap.unit.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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,
Expand Down

0 comments on commit a2f8bd8

Please sign in to comment.