@intlayer/core
Version:
Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.
1 lines • 5.61 kB
Source Map (JSON)
{"version":3,"file":"editDictionaryByKeyPath.cjs","names":["currentValue: any","parentValue: any","lastKeys: LastKeyType[]","NodeType"],"sources":["../../../src/dictionaryManipulator/editDictionaryByKeyPath.ts"],"sourcesContent":["import { type ContentNode, type KeyPath, NodeType } from '@intlayer/types';\n\ntype LastKeyType = string | number;\n\nexport const editDictionaryByKeyPath = (\n dictionaryContent: ContentNode,\n keyPath: KeyPath[],\n newValue: ContentNode\n): ContentNode => {\n let currentValue: any = dictionaryContent;\n let parentValue: any = null;\n let lastKeys: LastKeyType[] = [];\n\n if (keyPath.length === 0) {\n return newValue;\n }\n\n try {\n for (let i = 0; i < keyPath.length; i++) {\n const keyObj = keyPath[i];\n parentValue = currentValue;\n\n if (keyObj.type === NodeType.Object || keyObj.type === NodeType.Array) {\n lastKeys = [keyObj.key];\n if (\n !currentValue[keyObj.key] ||\n typeof currentValue[keyObj.key] !== 'object'\n ) {\n currentValue[keyObj.key] = {};\n }\n currentValue = currentValue[keyObj.key];\n }\n\n if (\n keyObj.type === NodeType.Translation ||\n keyObj.type === NodeType.Enumeration\n ) {\n lastKeys = [keyObj.type, keyObj.key];\n if (\n !currentValue[keyObj.type] ||\n typeof currentValue[keyObj.type] !== 'object'\n ) {\n currentValue[keyObj.type] = {};\n }\n if (\n !currentValue[keyObj.type][keyObj.key] ||\n typeof currentValue[keyObj.type][keyObj.key] !== 'object'\n ) {\n currentValue[keyObj.type][keyObj.key] = {};\n }\n currentValue = currentValue[keyObj.type][keyObj.key];\n }\n\n if (\n keyObj.type === NodeType.Enumeration ||\n keyObj.type === NodeType.Condition\n ) {\n // Note: Logic above already handles Enumeration, ensure no duplication in your actual file\n // or keep the specific block if your logic differs.\n // The important part is below in the final update block.\n\n // Assuming this block runs for Condition/Gender/etc:\n if (keyObj.type !== NodeType.Enumeration) {\n lastKeys = [keyObj.type, keyObj.key];\n currentValue = currentValue[keyObj.type][keyObj.key];\n }\n }\n\n if (\n keyObj.type === NodeType.Markdown ||\n keyObj.type === NodeType.Insertion\n ) {\n lastKeys = [keyObj.type];\n if (\n !currentValue[keyObj.type] ||\n typeof currentValue[keyObj.type] !== 'object'\n ) {\n currentValue[keyObj.type] = '';\n }\n currentValue = currentValue[keyObj.type];\n }\n\n if (keyObj.type === NodeType.File) {\n lastKeys = ['content'];\n currentValue = currentValue.content;\n }\n\n // Only update the value when processing the last key in the keyPath.\n if (i === keyPath.length - 1 && parentValue && lastKeys.length > 0) {\n let target = parentValue;\n\n // Drill down to the container holding the value to be changed\n for (const key of lastKeys.slice(0, -1)) {\n target = target[key];\n }\n\n const finalKey = lastKeys[lastKeys.length - 1];\n\n if (typeof newValue === 'undefined') {\n // Use splice for arrays to re-index the list, use delete for objects\n if (Array.isArray(target)) {\n const index = Number(finalKey);\n if (!isNaN(index) && index >= 0 && index < target.length) {\n target.splice(index, 1);\n }\n } else {\n delete target[finalKey];\n }\n } else {\n target[finalKey] = newValue;\n }\n }\n }\n\n return dictionaryContent;\n } catch (error) {\n console.error(\n 'Cannot edit dictionary by key path',\n { dictionaryContent, keyPath, newValue },\n error\n );\n return dictionaryContent;\n }\n};\n"],"mappings":";;;;AAIA,MAAa,2BACX,mBACA,SACA,aACgB;CAChB,IAAIA,eAAoB;CACxB,IAAIC,cAAmB;CACvB,IAAIC,WAA0B,EAAE;AAEhC,KAAI,QAAQ,WAAW,EACrB,QAAO;AAGT,KAAI;AACF,OAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,QAAQ,KAAK;GACvC,MAAM,SAAS,QAAQ;AACvB,iBAAc;AAEd,OAAI,OAAO,SAASC,yBAAS,UAAU,OAAO,SAASA,yBAAS,OAAO;AACrE,eAAW,CAAC,OAAO,IAAI;AACvB,QACE,CAAC,aAAa,OAAO,QACrB,OAAO,aAAa,OAAO,SAAS,SAEpC,cAAa,OAAO,OAAO,EAAE;AAE/B,mBAAe,aAAa,OAA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