@intenda/opus-ui-json-builder
Version:
Provides the `jsonBuilder` component type for use by Opus UI applications.
40 lines (39 loc) • 1.36 kB
JavaScript
/* eslint-disable max-lines-per-function */
const helper = jsonLines => {
const treeNodes = jsonLines.map(j => {
const leadingSpaces = j.search(/\S/);
return {
line: j,
depth: leadingSpaces / 2,
parentLineNum: null
};
});
const stack = [];
let depthMarker = treeNodes[0].depth;
let parentLineNum = null;
treeNodes.forEach((t, i) => {
if (t.depth === depthMarker) {
t.parentLineNum = parentLineNum;
return;
} else if (t.depth > depthMarker) {
stack.push({
lineNum: i - 1,
depth: t.depth
});
parentLineNum = i - 1;
t.parentLineNum = parentLineNum;
depthMarker = t.depth;
} else if (t.depth < depthMarker) {
while (t.depth < depthMarker) {
var _topOfStack$lineNum, _topOfStack$depth;
stack.pop();
const topOfStack = stack[stack.length - 1];
parentLineNum = (_topOfStack$lineNum = topOfStack === null || topOfStack === void 0 ? void 0 : topOfStack.lineNum) !== null && _topOfStack$lineNum !== void 0 ? _topOfStack$lineNum : 0;
depthMarker = (_topOfStack$depth = topOfStack === null || topOfStack === void 0 ? void 0 : topOfStack.depth) !== null && _topOfStack$depth !== void 0 ? _topOfStack$depth : null;
}
t.parentLineNum = parentLineNum;
}
});
return treeNodes;
};
export default helper;