Prevent null being converted to empty object in deepMap utility function (#8103)

* prevent null to empty object conversion in deepmap

* Use isObjectLike instead of explicit null check

Co-authored-by: rijkvanzanten <rijkvanzanten@me.com>
This commit is contained in:
Azri Kahar
2021-09-18 07:16:11 +08:00
committed by GitHub
parent 76ce24b65e
commit cc7394bf1d

View File

@@ -1,20 +1,22 @@
import { isObjectLike } from 'lodash';
export function deepMap(
object: Record<string, any>,
object: any,
iterator: (value: any, key: string | number) => any,
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
context?: any
): any {
if (Array.isArray(object)) {
return object.map(function (val, key) {
return typeof val === 'object' ? deepMap(val, iterator, context) : iterator.call(context, val, key);
return isObjectLike(val) ? deepMap(val, iterator, context) : iterator.call(context, val, key);
});
} else if (typeof object === 'object') {
} else if (isObjectLike(object)) {
const res: Record<string, any> = {};
for (const key in object) {
const val = object[key];
if (typeof val === 'object') {
if (isObjectLike(val)) {
res[key] = deepMap(val, iterator, context);
} else {
res[key] = iterator.call(context, val, key);