UNPKG

@dvcol/common-utils

Version:

Typescript library for common utility functions and constants

34 lines (32 loc) 1.05 kB
// lib/common/utils/object.utils.ts function shallowClone(obj, depth = 1, omit = []) { if (!obj || depth <= 0 || typeof obj !== "object") { return obj; } if (Array.isArray(obj)) { return obj.map((item) => omit.includes(item) ? item : shallowClone(item, depth - 1, omit)); } return Object.keys(obj).reduce((acc, key) => { const value = obj[key]; if (!omit.includes(key)) { acc[key] = shallowClone(value, depth - 1, omit); } else { acc[key] = value; } return acc; }, {}); } var isShallowEqual = (a, b, depth = 1) => { if (depth <= 0) return a === b; if (a === b) return true; if (typeof a !== "object" || typeof b !== "object") return a === b; if (a instanceof URL && b instanceof URL) return a.href === b.href; if (a && !b || !a && b) return false; const keys = [...Object.keys(a), ...Object.keys(b)]; if (keys.every((key) => a[key] === b[key])) return true; return keys.every((key) => isShallowEqual(a[key], b[key], depth - 1)); }; export { shallowClone, isShallowEqual };