@polgubau/utils
Version:
A collection of utility functions for TypeScript
1 lines • 1.8 kB
Source Map (JSON)
{"version":3,"sources":["../../../../src/objects/merge-deep/merge-deep.ts"],"sourcesContent":["import { isObject } from \"../../comparators\";\nimport { cloneDeep } from \"../clone-deps/clone-deep\";\n\n/**\n * Merge and deep copy the values of all of the enumerable own properties of target object from source object to a new object\n * @param target The target object to get properties from.\n * @param source The source object from which to copy properties.\n * @return A new merged and deep copied object.\n */\nexport function mergeDeep<T extends object, S extends object>(target: T, source: S): T & S {\n if (isObject(source) && Object.keys(source).length === 0) {\n return cloneDeep({ ...target, ...source });\n }\n\n const output = { ...target, ...source };\n\n if (isObject(source) && isObject(target)) {\n for (const key in source) {\n if (isObject(source[key]) && key in target && isObject(target[key])) {\n (output as Record<string, unknown>)[key] = mergeDeep(target[key] as object, source[key] as object);\n } else {\n (output as Record<string, unknown>)[key] = isObject(source[key]) ? cloneDeep(source[key]) : source[key];\n }\n }\n }\n\n return output;\n}\n"],"mappings":";AAAA,SAAS,gBAAgB;AACzB,SAAS,iBAAiB;AAQnB,SAAS,UAA8C,QAAW,QAAkB;AACzF,MAAI,SAAS,MAAM,KAAK,OAAO,KAAK,MAAM,EAAE,WAAW,GAAG;AACxD,WAAO,UAAU,EAAE,GAAG,QAAQ,GAAG,OAAO,CAAC;AAAA,EAC3C;AAEA,QAAM,SAAS,EAAE,GAAG,QAAQ,GAAG,OAAO;AAEtC,MAAI,SAAS,MAAM,KAAK,SAAS,MAAM,GAAG;AACxC,eAAW,OAAO,QAAQ;AACxB,UAAI,SAAS,OAAO,GAAG,CAAC,KAAK,OAAO,UAAU,SAAS,OAAO,GAAG,CAAC,GAAG;AACnE,QAAC,OAAmC,GAAG,IAAI,UAAU,OAAO,GAAG,GAAa,OAAO,GAAG,CAAW;AAAA,MACnG,OAAO;AACL,QAAC,OAAmC,GAAG,IAAI,SAAS,OAAO,GAAG,CAAC,IAAI,UAAU,OAAO,GAAG,CAAC,IAAI,OAAO,GAAG;AAAA,MACxG;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}