@intlayer/core
Version:
Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.
39 lines (37 loc) • 1.79 kB
JavaScript
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
let __intlayer_types = require("@intlayer/types");
//#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 === __intlayer_types.NodeType.Object || keyObj.type === __intlayer_types.NodeType.Array) {
lastKey = keyObj.key;
currentValue = currentValue[keyObj.key];
}
if (keyObj.type === __intlayer_types.NodeType.Translation || keyObj.type === __intlayer_types.NodeType.Enumeration || keyObj.type === __intlayer_types.NodeType.Condition) {
lastKey = keyObj.type;
currentValue = currentValue[keyObj.type][keyObj.key];
}
if (keyObj.type === __intlayer_types.NodeType.Markdown || keyObj.type === __intlayer_types.NodeType.ReactNode || keyObj.type === __intlayer_types.NodeType.Insertion || keyObj.type === __intlayer_types.NodeType.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
exports.renameContentNodeByKeyPath = renameContentNodeByKeyPath;
//# sourceMappingURL=renameContentNodeByKeyPath.cjs.map