UNPKG

@polgubau/utils

Version:

A collection of utility functions for TypeScript

1 lines 3.01 kB
{"version":3,"sources":["../../../../src/objects/merge/merge.ts"],"sourcesContent":["/**\n * Merges the properties of the source object into the target object.\n *\n * This function performs a deep merge, meaning nested objects and arrays are merged recursively.\n * If a property in the source object is an array or an object and the corresponding property in the target object is also an array or object, they will be merged.\n * If a property in the source object is undefined, it will not overwrite a defined property in the target object.\n *\n * Note that this function mutates the target object.\n *\n * @param {T} target - The target object into which the source object properties will be merged. This object is modified in place.\n * @param {S} source - The source object whose properties will be merged into the target object.\n * @returns {T & S} The updated target object with properties from the source object merged in.\n *\n * @template T - Type of the target object.\n * @template S - Type of the source object.\n *\n * @example\n * const target = { a: 1, b: { x: 1, y: 2 } };\n * const source = { b: { y: 3, z: 4 }, c: 5 };\n *\n * const result = merge(target, source);\n * console.info(result);\n * // Output: { a: 1, b: { x: 1, y: 3, z: 4 }, c: 5 }\n *\n * @example\n * const target = { a: [1, 2], b: { x: 1 } };\n * const source = { a: [3], b: { y: 2 } };\n *\n * const result = merge(target, source);\n * console.info(result);\n * // Output: { a: [3, 2], b: { x: 1, y: 2 } }\n *\n * @example\n * const target = { a: null };\n * const source = { a: [1, 2, 3] };\n *\n * const result = merge(target, source);\n * console.info(result);\n * // Output: { a: [1, 2, 3] }\n */\n\nexport function isObjectLike(value: unknown): value is object {\n return typeof value === \"object\" && value !== null;\n}\nexport function merge<T, S>(target: T, source: S): T & S;\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\nexport function merge(target: any, source: any) {\n const sourceKeys = Object.keys(source);\n\n for (const key of sourceKeys) {\n const sourceValue = source[key];\n const targetValue = target[key];\n\n if (Array.isArray(sourceValue)) {\n target[key] = merge(targetValue ?? [], sourceValue);\n } else if (isObjectLike(targetValue) && isObjectLike(sourceValue)) {\n target[key] = merge(targetValue ?? {}, sourceValue);\n } else if (targetValue === undefined || sourceValue !== undefined) {\n target[key] = sourceValue;\n }\n }\n\n return target;\n}\n"],"mappings":";AAyCO,SAAS,aAAa,OAAiC;AAC5D,SAAO,OAAO,UAAU,YAAY,UAAU;AAChD;AAGO,SAAS,MAAM,QAAa,QAAa;AAC9C,QAAM,aAAa,OAAO,KAAK,MAAM;AAErC,aAAW,OAAO,YAAY;AAC5B,UAAM,cAAc,OAAO,GAAG;AAC9B,UAAM,cAAc,OAAO,GAAG;AAE9B,QAAI,MAAM,QAAQ,WAAW,GAAG;AAC9B,aAAO,GAAG,IAAI,MAAM,eAAe,CAAC,GAAG,WAAW;AAAA,IACpD,WAAW,aAAa,WAAW,KAAK,aAAa,WAAW,GAAG;AACjE,aAAO,GAAG,IAAI,MAAM,eAAe,CAAC,GAAG,WAAW;AAAA,IACpD,WAAW,gBAAgB,UAAa,gBAAgB,QAAW;AACjE,aAAO,GAAG,IAAI;AAAA,IAChB;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}