@intlayer/core
Version:
Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.
1 lines • 5.37 kB
Source Map (JSON)
{"version":3,"file":"editDictionaryByKeyPath.mjs","names":["currentValue: any","parentValue: any","lastKeys: LastKeyType[]"],"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 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.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\n currentValue = currentValue.content;\n }\n\n if (keyObj.type) {\n // No treated TypedNode\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 // Drill down if lastKeys contains more than one key.\n for (const key of lastKeys.slice(0, -1)) {\n target = target[key];\n }\n if (typeof newValue === 'undefined') {\n delete target[lastKeys[lastKeys.length - 1]];\n } else {\n target[lastKeys[lastKeys.length - 1]] = 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,SAAS,SAAS,UAAU,OAAO,SAAS,SAAS,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,OAAO;;AAGrC,OACE,OAAO,SAAS,SAAS,eACzB,OAAO,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