@intlayer/core
Version:
Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.
39 lines (37 loc) • 1.16 kB
JavaScript
import { NodeType } from "@intlayer/types";
//#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$1, props$1) => deepTransformNode(node$1, props$1));
if (node === null || typeof node !== "object") return node;
if (Array.isArray(node)) return node.map((child, index) => {
return deepTransformNode(child, {
...props,
children: child,
keyPath: [...props.keyPath, {
type: NodeType.Array,
key: index
}]
});
});
const result = {};
for (const key in node) {
const childProps = {
...props,
children: node[key],
keyPath: [...props.keyPath, {
type: NodeType.Object,
key
}]
};
result[key] = deepTransformNode(node[key], childProps);
}
return result;
};
//#endregion
export { deepTransformNode };
//# sourceMappingURL=deepTransform.mjs.map