UNPKG

@ukbhra/common-utils

Version:

A collection of reusable utility functions for string, object, validation, and general operations in Node.js projects.

44 lines 1.47 kB
export const deepClone = (obj) => JSON.parse(JSON.stringify(obj)); export const isEmptyObject = (obj) => Object.keys(obj).length === 0; export const groupBy = (arr, keyFn) => arr.reduce((acc, item) => { const key = keyFn(item); (acc[key] || (acc[key] = [])).push(item); return acc; }, {}); export const uniq = (arr) => Array.from(new Set(arr)); export const flatten = (arr) => arr.reduce((flat, toFlatten) => flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten), []); // Merge two objects deeply export const deepMerge = (target, source) => { for (const key in source) { if (source[key] && typeof source[key] === 'object' && !Array.isArray(source[key])) { target[key] = deepMerge(target[key] || {}, source[key]); } else { target[key] = source[key]; } } return target; }; // Remove keys from object export const omit = (obj, keys) => { const clone = { ...obj }; for (const key of keys) { delete clone[key]; } return clone; }; // Pick keys from object export const pick = (obj, keys) => { const result = {}; for (const key of keys) { if (key in obj) { result[key] = obj[key]; } } return result; }; // Check if value is plain object export const isPlainObject = (val) => val !== null && typeof val === 'object' && val.constructor === Object; //# sourceMappingURL=objectUtil.js.map