mirror of
https://github.com/directus/directus.git
synced 2026-04-03 03:00:39 -04:00
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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user