@intlayer/core
Version:
Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.
38 lines (36 loc) • 1.61 kB
JavaScript
import * as NodeTypes from "@intlayer/types/nodeType";
//#region src/dictionaryManipulator/renameContentNodeByKeyPath.ts
const renameContentNodeByKeyPath = (dictionaryContent, newKey, keyPath) => {
let currentValue = dictionaryContent;
let parentValue = null;
let lastKey = null;
for (const keyObj of keyPath) {
parentValue = currentValue;
if (keyObj.type === NodeTypes.OBJECT || keyObj.type === NodeTypes.ARRAY) {
lastKey = keyObj.key;
currentValue = currentValue[keyObj.key];
}
if (keyObj.type === NodeTypes.TRANSLATION || keyObj.type === NodeTypes.ENUMERATION || keyObj.type === NodeTypes.PLURAL || keyObj.type === NodeTypes.CONDITION) {
lastKey = keyObj.type;
currentValue = currentValue[keyObj.type][keyObj.key];
}
if (keyObj.type === NodeTypes.MARKDOWN || keyObj.type === NodeTypes.REACT_NODE || keyObj.type === NodeTypes.HTML || keyObj.type === NodeTypes.INSERTION || keyObj.type === NodeTypes.FILE) {
lastKey = keyObj.type;
currentValue = currentValue[keyObj.type];
}
}
if (parentValue && lastKey !== null) if (Array.isArray(parentValue)) parentValue[lastKey] = currentValue;
else {
const newParentValue = {};
for (const key of Object.keys(parentValue)) if (key === lastKey && typeof newKey !== "undefined") newParentValue[newKey] = currentValue;
else newParentValue[key] = parentValue[key];
Object.keys(parentValue).forEach((key) => {
delete parentValue[key];
});
Object.assign(parentValue, newParentValue);
}
return dictionaryContent;
};
//#endregion
export { renameContentNodeByKeyPath };
//# sourceMappingURL=renameContentNodeByKeyPath.mjs.map