@intlayer/core
Version:
Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.
56 lines (54 loc) • 1.68 kB
JavaScript
import * as NodeTypes from "@intlayer/types/nodeType";
//#region src/interpreter/getContent/deepTransform.ts
/**
* Recursively traverses a node (object/array/primitive).
* Applies the *first* plugin that can transform a node, then stops descending further.
* If no plugin transforms it, it recurses into its children.
*/
const deepTransformNode = (node, props) => {
for (const plugin of props.plugins ?? []) if (plugin.canHandle(node)) return plugin.transform(node, props, (node, props) => deepTransformNode(node, props));
if (node === null || typeof node !== "object") return node;
if (node.$$typeof !== void 0 || node.__v_isVNode !== void 0 || node._isVNode !== void 0 || node.isJSX !== void 0 || typeof node === "function") return node;
if (Array.isArray(node)) return node.map((child, index) => {
return deepTransformNode(child, {
...props,
children: child,
keyPath: [...props.keyPath, {
type: NodeTypes.ARRAY,
key: index
}]
});
});
const result = {};
for (const key in node) {
const childProps = {
...props,
children: node[key],
keyPath: [...props.keyPath, {
type: NodeTypes.OBJECT,
key
}]
};
if (props.eager) {
result[key] = deepTransformNode(node[key], childProps);
continue;
}
Object.defineProperty(result, key, {
enumerable: true,
configurable: true,
get: function() {
const transformed = deepTransformNode(node[key], childProps);
Object.defineProperty(this, key, {
value: transformed,
enumerable: true,
configurable: true
});
return transformed;
}
});
}
return result;
};
//#endregion
export { deepTransformNode };
//# sourceMappingURL=deepTransform.mjs.map