UNPKG

@lucidcms/core

Version:

The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.

1 lines 2.06 kB
{"version":3,"file":"deep-merge.mjs","names":[],"sources":["../../../src/utils/helpers/deep-merge.ts"],"sourcesContent":["type MergeObject = Record<string, unknown>;\n\nconst isMergeObject = (value: unknown): value is MergeObject => {\n\tif (!value || typeof value !== \"object\" || Array.isArray(value)) {\n\t\treturn false;\n\t}\n\n\tconst prototype = Object.getPrototypeOf(value);\n\treturn prototype === Object.prototype || prototype === null;\n};\n\nconst mergeValue = (targetValue: unknown, sourceValue: unknown): unknown => {\n\tif (sourceValue === undefined) {\n\t\treturn targetValue;\n\t}\n\n\tif (Array.isArray(sourceValue)) {\n\t\tconst targetArray = Array.isArray(targetValue) ? targetValue : [];\n\n\t\tsourceValue.forEach((item, index) => {\n\t\t\ttargetArray[index] = mergeValue(targetArray[index], item);\n\t\t});\n\n\t\treturn targetArray;\n\t}\n\n\tif (isMergeObject(sourceValue)) {\n\t\tconst targetObject = isMergeObject(targetValue) ? targetValue : {};\n\t\treturn deepMerge(targetObject, sourceValue);\n\t}\n\n\treturn sourceValue;\n};\n\nconst deepMerge = <Target extends object, Source extends object>(\n\ttarget: Target,\n\tsource: Source,\n) => {\n\tconst targetObject = target as MergeObject;\n\n\tfor (const [key, sourceValue] of Object.entries(source)) {\n\t\ttargetObject[key] = mergeValue(targetObject[key], sourceValue);\n\t}\n\n\treturn target as Target & Source;\n};\n\nexport default deepMerge;\n"],"mappings":"AAEA,MAAM,EAAiB,GAAyC,CAC/D,GAAI,CAAC,GAAS,OAAO,GAAU,UAAY,MAAM,QAAQ,CAAK,EAC7D,MAAO,GAGR,IAAM,EAAY,OAAO,eAAe,CAAK,EAC7C,OAAO,IAAc,OAAO,WAAa,IAAc,IACxD,EAEM,GAAc,EAAsB,IAAkC,CAC3E,GAAI,IAAgB,IAAA,GACnB,OAAO,EAGR,GAAI,MAAM,QAAQ,CAAW,EAAG,CAC/B,IAAM,EAAc,MAAM,QAAQ,CAAW,EAAI,EAAc,CAAC,EAMhE,OAJA,EAAY,SAAS,EAAM,IAAU,CACpC,EAAY,GAAS,EAAW,EAAY,GAAQ,CAAI,CACzD,CAAC,EAEM,CACR,CAEA,GAAI,EAAc,CAAW,EAAG,CAC/B,IAAM,EAAe,EAAc,CAAW,EAAI,EAAc,CAAC,EACjE,OAAO,EAAU,EAAc,CAAW,CAC3C,CAEA,OAAO,CACR,EAEM,GACL,EACA,IACI,CACJ,IAAM,EAAe,EAErB,IAAK,GAAM,CAAC,EAAK,KAAgB,OAAO,QAAQ,CAAM,EACrD,EAAa,GAAO,EAAW,EAAa,GAAM,CAAW,EAG9D,OAAO,CACR"}