@soratani-code/samtools 1.3.19 → 1.3.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/tree.d.ts +1 -0
- package/lib/tree.js +37 -1
- package/package.json +1 -1
package/lib/tree.d.ts
CHANGED
|
@@ -2,3 +2,4 @@ export declare function findNodeFormKey<D = any>(key: string, nodes: D[], id: st
|
|
|
2
2
|
export declare function updateNodeFormKey<D = any>(key: string, nodes: D[], id: string, value: any): D[] | null;
|
|
3
3
|
export declare function deleteNodeFormKey<D = any>(key: string, nodes: D[], id: string): D[];
|
|
4
4
|
export declare function toList<D = any>(nodes: D[]): any[];
|
|
5
|
+
export declare function slice<T extends Record<string, any>>(tree: T[] | T, level: number, childrenKey?: string): T | T[] | undefined;
|
package/lib/tree.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.toList = exports.deleteNodeFormKey = exports.updateNodeFormKey = exports.findNodeFormKey = void 0;
|
|
3
|
+
exports.slice = exports.toList = exports.deleteNodeFormKey = exports.updateNodeFormKey = exports.findNodeFormKey = void 0;
|
|
4
4
|
const lodash_1 = require("lodash");
|
|
5
5
|
function findNodeFormKey(key, nodes, id) {
|
|
6
6
|
if (!id)
|
|
@@ -77,3 +77,39 @@ function toList(nodes) {
|
|
|
77
77
|
return result;
|
|
78
78
|
}
|
|
79
79
|
exports.toList = toList;
|
|
80
|
+
function slice(tree, level, childrenKey = 'children') {
|
|
81
|
+
if (level < 1)
|
|
82
|
+
return undefined;
|
|
83
|
+
const process = (nodes, maxLevel) => {
|
|
84
|
+
let currentLevel = 1;
|
|
85
|
+
let queue = nodes.map(node => (Object.assign({}, node)));
|
|
86
|
+
let result = queue;
|
|
87
|
+
while (currentLevel < maxLevel) {
|
|
88
|
+
let nextLevelNodes = [];
|
|
89
|
+
for (let node of queue) {
|
|
90
|
+
const n = node;
|
|
91
|
+
if (Array.isArray(n[childrenKey])) {
|
|
92
|
+
n[childrenKey] = n[childrenKey].map((child) => (Object.assign({}, child)));
|
|
93
|
+
nextLevelNodes.push(...n[childrenKey]);
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
delete n[childrenKey];
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
queue = nextLevelNodes;
|
|
100
|
+
currentLevel++;
|
|
101
|
+
}
|
|
102
|
+
for (let node of queue) {
|
|
103
|
+
const n = node;
|
|
104
|
+
if (n[childrenKey]) {
|
|
105
|
+
delete n[childrenKey];
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return result;
|
|
109
|
+
};
|
|
110
|
+
if (Array.isArray(tree)) {
|
|
111
|
+
return process(tree, level);
|
|
112
|
+
}
|
|
113
|
+
return process([tree], level)[0];
|
|
114
|
+
}
|
|
115
|
+
exports.slice = slice;
|