source: node_modules/d3-hierarchy/src/hierarchy/index.js@ e4c61dd

Last change on this file since e4c61dd was e4c61dd, checked in by istevanoska <ilinastevanoska@…>, 6 months ago

Prototype 1.1

  • Property mode set to 100644
File size: 2.2 KB
Line 
1import node_count from "./count.js";
2import node_each from "./each.js";
3import node_eachBefore from "./eachBefore.js";
4import node_eachAfter from "./eachAfter.js";
5import node_find from "./find.js";
6import node_sum from "./sum.js";
7import node_sort from "./sort.js";
8import node_path from "./path.js";
9import node_ancestors from "./ancestors.js";
10import node_descendants from "./descendants.js";
11import node_leaves from "./leaves.js";
12import node_links from "./links.js";
13import node_iterator from "./iterator.js";
14
15export default function hierarchy(data, children) {
16 if (data instanceof Map) {
17 data = [undefined, data];
18 if (children === undefined) children = mapChildren;
19 } else if (children === undefined) {
20 children = objectChildren;
21 }
22
23 var root = new Node(data),
24 node,
25 nodes = [root],
26 child,
27 childs,
28 i,
29 n;
30
31 while (node = nodes.pop()) {
32 if ((childs = children(node.data)) && (n = (childs = Array.from(childs)).length)) {
33 node.children = childs;
34 for (i = n - 1; i >= 0; --i) {
35 nodes.push(child = childs[i] = new Node(childs[i]));
36 child.parent = node;
37 child.depth = node.depth + 1;
38 }
39 }
40 }
41
42 return root.eachBefore(computeHeight);
43}
44
45function node_copy() {
46 return hierarchy(this).eachBefore(copyData);
47}
48
49function objectChildren(d) {
50 return d.children;
51}
52
53function mapChildren(d) {
54 return Array.isArray(d) ? d[1] : null;
55}
56
57function copyData(node) {
58 if (node.data.value !== undefined) node.value = node.data.value;
59 node.data = node.data.data;
60}
61
62export function computeHeight(node) {
63 var height = 0;
64 do node.height = height;
65 while ((node = node.parent) && (node.height < ++height));
66}
67
68export function Node(data) {
69 this.data = data;
70 this.depth =
71 this.height = 0;
72 this.parent = null;
73}
74
75Node.prototype = hierarchy.prototype = {
76 constructor: Node,
77 count: node_count,
78 each: node_each,
79 eachAfter: node_eachAfter,
80 eachBefore: node_eachBefore,
81 find: node_find,
82 sum: node_sum,
83 sort: node_sort,
84 path: node_path,
85 ancestors: node_ancestors,
86 descendants: node_descendants,
87 leaves: node_leaves,
88 links: node_links,
89 copy: node_copy,
90 [Symbol.iterator]: node_iterator
91};
Note: See TracBrowser for help on using the repository browser.