@intlayer/chokidar
Version:
Scans and builds Intlayer declaration files into dictionaries based on Intlayer configuration.
1 lines • 4.97 kB
Source Map (JSON)
{"version":3,"file":"reduceObjectFormat.cjs","names":[],"sources":["../../../src/utils/reduceObjectFormat.ts"],"sourcesContent":["export type Primitive = string | number | boolean | null | undefined;\n\nexport type Recursive =\n | Primitive\n | { [key: string]: Recursive }\n | Array<Recursive>;\n\n/**\n * Reduce an object to only the shape provided by a format object.\n * Values are always taken from the source object; the format is used only for structure.\n *\n * Examples:\n * reduceObjectFormat({ a: 1, b: 2 }, { a: 0 }) => { a: 1 }\n * reduceObjectFormat({ a: { x: 1, y: 2 } }, { a: { x: 0 } }) => { a: { x: 1 } }\n */\nexport const reduceObjectFormat = (\n source: Recursive,\n format: Recursive\n): Recursive => {\n // If the format is an array, reduce each element by its counterpart in source\n if (Array.isArray(format)) {\n const sourceArray = Array.isArray(source) ? source : [];\n return format.map((formatItem, index) =>\n reduceObjectFormat(sourceArray[index], formatItem)\n );\n }\n\n // If the format is an object (and not null), pick matching keys and recurse\n if (typeof format === 'object' && format !== null) {\n const result: Record<string, Recursive> = {};\n const sourceObject =\n typeof source === 'object' && source !== null && !Array.isArray(source)\n ? (source as Record<string, Recursive>)\n : ({} as Record<string, Recursive>);\n\n for (const key of Object.keys(format)) {\n const nextSource = sourceObject[key];\n const nextFormat = (format as Record<string, Recursive>)[key];\n result[key] = reduceObjectFormat(nextSource, nextFormat);\n }\n return result;\n }\n\n // For primitives in the format, simply return the source value (can be undefined)\n return source as Primitive;\n};\n\n/**\n * Returns the subset of source whose keys are NOT present in the format object.\n * Inverse of reduceObjectFormat.\n *\n * Null values in source are always included as explicit fallback markers\n * (they signal \"use default locale\" at render time).\n *\n * Examples:\n * excludeObjectFormat({ a: 1, b: 2 }, { b: 0 }) => { a: 1 }\n * excludeObjectFormat({ a: { x: 1, y: 2 } }, { a: { x: 0 } }) => { a: { y: 2 } }\n * excludeObjectFormat({ a: null, b: 2 }, { b: 0 }) => { a: null }\n */\nexport const excludeObjectFormat = (\n source: Recursive,\n format: Recursive\n): Recursive | undefined => {\n // null in source = explicit fallback marker → always include\n if (source === null) return null;\n\n // No format means nothing is translated yet → include all source\n if (format === undefined || format === null) return source;\n\n // Both plain objects → recurse key-by-key\n if (\n typeof source === 'object' &&\n !Array.isArray(source) &&\n typeof format === 'object' &&\n !Array.isArray(format)\n ) {\n const result: Record<string, Recursive> = {};\n let hasContent = false;\n\n for (const key of Object.keys(source as Record<string, Recursive>)) {\n const excluded = excludeObjectFormat(\n (source as Record<string, Recursive>)[key],\n (format as Record<string, Recursive>)[key]\n );\n if (excluded !== undefined) {\n result[key] = excluded;\n hasContent = true;\n }\n }\n\n return hasContent ? result : undefined;\n }\n\n // Arrays → recurse by index\n if (Array.isArray(source)) {\n const formatArr = Array.isArray(format) ? format : [];\n const result: Recursive[] = [];\n\n for (let i = 0; i < source.length; i++) {\n const excluded = excludeObjectFormat(source[i], formatArr[i]);\n if (excluded !== undefined) {\n result.push(excluded);\n }\n }\n\n return result.length > 0 ? result : undefined;\n }\n\n // Primitive in source and format has a value → already translated → exclude\n return undefined;\n};\n"],"mappings":";;;;;;;;;;;AAeA,MAAa,sBACX,QACA,WACc;AAEd,KAAI,MAAM,QAAQ,OAAO,EAAE;EACzB,MAAM,cAAc,MAAM,QAAQ,OAAO,GAAG,SAAS,EAAE;AACvD,SAAO,OAAO,KAAK,YAAY,UAC7B,mBAAmB,YAAY,QAAQ,WAAW,CACnD;;AAIH,KAAI,OAAO,WAAW,YAAY,WAAW,MAAM;EACjD,MAAM,SAAoC,EAAE;EAC5C,MAAM,eACJ,OAAO,WAAW,YAAY,WAAW,QAAQ,CAAC,MAAM,QAAQ,OAAO,GAClE,SACA,EAAE;AAET,OAAK,MAAM,OAAO,OAAO,KAAK,OAAO,EAAE;GACrC,MAAM,aAAa,aAAa;GAChC,MAAM,aAAc,OAAqC;AACzD,UAAO,OAAO,mBAAmB,YAAY,WAAW;;AAE1D,SAAO;;AAIT,QAAO;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