@lizardbyte/contribkit 2025.315.185528

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.
@@ -0,0 +1,590 @@
1
+ function count(node) {
2
+ var sum = 0,
3
+ children = node.children,
4
+ i = children && children.length;
5
+ if (!i) sum = 1;
6
+ else while (--i >= 0) sum += children[i].value;
7
+ node.value = sum;
8
+ }
9
+
10
+ function node_count() {
11
+ return this.eachAfter(count);
12
+ }
13
+
14
+ function node_each(callback, that) {
15
+ let index = -1;
16
+ for (const node of this) {
17
+ callback.call(that, node, ++index, this);
18
+ }
19
+ return this;
20
+ }
21
+
22
+ function node_eachBefore(callback, that) {
23
+ var node = this, nodes = [node], children, i, index = -1;
24
+ while (node = nodes.pop()) {
25
+ callback.call(that, node, ++index, this);
26
+ if (children = node.children) {
27
+ for (i = children.length - 1; i >= 0; --i) {
28
+ nodes.push(children[i]);
29
+ }
30
+ }
31
+ }
32
+ return this;
33
+ }
34
+
35
+ function node_eachAfter(callback, that) {
36
+ var node = this, nodes = [node], next = [], children, i, n, index = -1;
37
+ while (node = nodes.pop()) {
38
+ next.push(node);
39
+ if (children = node.children) {
40
+ for (i = 0, n = children.length; i < n; ++i) {
41
+ nodes.push(children[i]);
42
+ }
43
+ }
44
+ }
45
+ while (node = next.pop()) {
46
+ callback.call(that, node, ++index, this);
47
+ }
48
+ return this;
49
+ }
50
+
51
+ function node_find(callback, that) {
52
+ let index = -1;
53
+ for (const node of this) {
54
+ if (callback.call(that, node, ++index, this)) {
55
+ return node;
56
+ }
57
+ }
58
+ }
59
+
60
+ function node_sum(value) {
61
+ return this.eachAfter(function(node) {
62
+ var sum = +value(node.data) || 0,
63
+ children = node.children,
64
+ i = children && children.length;
65
+ while (--i >= 0) sum += children[i].value;
66
+ node.value = sum;
67
+ });
68
+ }
69
+
70
+ function node_sort(compare) {
71
+ return this.eachBefore(function(node) {
72
+ if (node.children) {
73
+ node.children.sort(compare);
74
+ }
75
+ });
76
+ }
77
+
78
+ function node_path(end) {
79
+ var start = this,
80
+ ancestor = leastCommonAncestor(start, end),
81
+ nodes = [start];
82
+ while (start !== ancestor) {
83
+ start = start.parent;
84
+ nodes.push(start);
85
+ }
86
+ var k = nodes.length;
87
+ while (end !== ancestor) {
88
+ nodes.splice(k, 0, end);
89
+ end = end.parent;
90
+ }
91
+ return nodes;
92
+ }
93
+
94
+ function leastCommonAncestor(a, b) {
95
+ if (a === b) return a;
96
+ var aNodes = a.ancestors(),
97
+ bNodes = b.ancestors(),
98
+ c = null;
99
+ a = aNodes.pop();
100
+ b = bNodes.pop();
101
+ while (a === b) {
102
+ c = a;
103
+ a = aNodes.pop();
104
+ b = bNodes.pop();
105
+ }
106
+ return c;
107
+ }
108
+
109
+ function node_ancestors() {
110
+ var node = this, nodes = [node];
111
+ while (node = node.parent) {
112
+ nodes.push(node);
113
+ }
114
+ return nodes;
115
+ }
116
+
117
+ function node_descendants() {
118
+ return Array.from(this);
119
+ }
120
+
121
+ function node_leaves() {
122
+ var leaves = [];
123
+ this.eachBefore(function(node) {
124
+ if (!node.children) {
125
+ leaves.push(node);
126
+ }
127
+ });
128
+ return leaves;
129
+ }
130
+
131
+ function node_links() {
132
+ var root = this, links = [];
133
+ root.each(function(node) {
134
+ if (node !== root) { // Don’t include the root’s parent, if any.
135
+ links.push({source: node.parent, target: node});
136
+ }
137
+ });
138
+ return links;
139
+ }
140
+
141
+ function* node_iterator() {
142
+ var node = this, current, next = [node], children, i, n;
143
+ do {
144
+ current = next.reverse(), next = [];
145
+ while (node = current.pop()) {
146
+ yield node;
147
+ if (children = node.children) {
148
+ for (i = 0, n = children.length; i < n; ++i) {
149
+ next.push(children[i]);
150
+ }
151
+ }
152
+ }
153
+ } while (next.length);
154
+ }
155
+
156
+ function hierarchy(data, children) {
157
+ if (data instanceof Map) {
158
+ data = [undefined, data];
159
+ if (children === undefined) children = mapChildren;
160
+ } else if (children === undefined) {
161
+ children = objectChildren;
162
+ }
163
+
164
+ var root = new Node$1(data),
165
+ node,
166
+ nodes = [root],
167
+ child,
168
+ childs,
169
+ i,
170
+ n;
171
+
172
+ while (node = nodes.pop()) {
173
+ if ((childs = children(node.data)) && (n = (childs = Array.from(childs)).length)) {
174
+ node.children = childs;
175
+ for (i = n - 1; i >= 0; --i) {
176
+ nodes.push(child = childs[i] = new Node$1(childs[i]));
177
+ child.parent = node;
178
+ child.depth = node.depth + 1;
179
+ }
180
+ }
181
+ }
182
+
183
+ return root.eachBefore(computeHeight);
184
+ }
185
+
186
+ function node_copy() {
187
+ return hierarchy(this).eachBefore(copyData);
188
+ }
189
+
190
+ function objectChildren(d) {
191
+ return d.children;
192
+ }
193
+
194
+ function mapChildren(d) {
195
+ return Array.isArray(d) ? d[1] : null;
196
+ }
197
+
198
+ function copyData(node) {
199
+ if (node.data.value !== undefined) node.value = node.data.value;
200
+ node.data = node.data.data;
201
+ }
202
+
203
+ function computeHeight(node) {
204
+ var height = 0;
205
+ do node.height = height;
206
+ while ((node = node.parent) && (node.height < ++height));
207
+ }
208
+
209
+ function Node$1(data) {
210
+ this.data = data;
211
+ this.depth =
212
+ this.height = 0;
213
+ this.parent = null;
214
+ }
215
+
216
+ Node$1.prototype = hierarchy.prototype = {
217
+ constructor: Node$1,
218
+ count: node_count,
219
+ each: node_each,
220
+ eachAfter: node_eachAfter,
221
+ eachBefore: node_eachBefore,
222
+ find: node_find,
223
+ sum: node_sum,
224
+ sort: node_sort,
225
+ path: node_path,
226
+ ancestors: node_ancestors,
227
+ descendants: node_descendants,
228
+ leaves: node_leaves,
229
+ links: node_links,
230
+ copy: node_copy,
231
+ [Symbol.iterator]: node_iterator
232
+ };
233
+
234
+ function optional(f) {
235
+ return f == null ? null : required(f);
236
+ }
237
+
238
+ function required(f) {
239
+ if (typeof f !== "function") throw new Error;
240
+ return f;
241
+ }
242
+
243
+ function constantZero() {
244
+ return 0;
245
+ }
246
+
247
+ function constant(x) {
248
+ return function() {
249
+ return x;
250
+ };
251
+ }
252
+
253
+ // https://en.wikipedia.org/wiki/Linear_congruential_generator#Parameters_in_common_use
254
+ const a = 1664525;
255
+ const c = 1013904223;
256
+ const m = 4294967296; // 2^32
257
+
258
+ function lcg() {
259
+ let s = 1;
260
+ return () => (s = (a * s + c) % m) / m;
261
+ }
262
+
263
+ function array(x) {
264
+ return typeof x === "object" && "length" in x
265
+ ? x // Array, TypedArray, NodeList, array-like
266
+ : Array.from(x); // Map, Set, iterable, string, or anything else
267
+ }
268
+
269
+ function shuffle(array, random) {
270
+ let m = array.length,
271
+ t,
272
+ i;
273
+
274
+ while (m) {
275
+ i = random() * m-- | 0;
276
+ t = array[m];
277
+ array[m] = array[i];
278
+ array[i] = t;
279
+ }
280
+
281
+ return array;
282
+ }
283
+
284
+ function packEncloseRandom(circles, random) {
285
+ var i = 0, n = (circles = shuffle(Array.from(circles), random)).length, B = [], p, e;
286
+
287
+ while (i < n) {
288
+ p = circles[i];
289
+ if (e && enclosesWeak(e, p)) ++i;
290
+ else e = encloseBasis(B = extendBasis(B, p)), i = 0;
291
+ }
292
+
293
+ return e;
294
+ }
295
+
296
+ function extendBasis(B, p) {
297
+ var i, j;
298
+
299
+ if (enclosesWeakAll(p, B)) return [p];
300
+
301
+ // If we get here then B must have at least one element.
302
+ for (i = 0; i < B.length; ++i) {
303
+ if (enclosesNot(p, B[i])
304
+ && enclosesWeakAll(encloseBasis2(B[i], p), B)) {
305
+ return [B[i], p];
306
+ }
307
+ }
308
+
309
+ // If we get here then B must have at least two elements.
310
+ for (i = 0; i < B.length - 1; ++i) {
311
+ for (j = i + 1; j < B.length; ++j) {
312
+ if (enclosesNot(encloseBasis2(B[i], B[j]), p)
313
+ && enclosesNot(encloseBasis2(B[i], p), B[j])
314
+ && enclosesNot(encloseBasis2(B[j], p), B[i])
315
+ && enclosesWeakAll(encloseBasis3(B[i], B[j], p), B)) {
316
+ return [B[i], B[j], p];
317
+ }
318
+ }
319
+ }
320
+
321
+ // If we get here then something is very wrong.
322
+ throw new Error;
323
+ }
324
+
325
+ function enclosesNot(a, b) {
326
+ var dr = a.r - b.r, dx = b.x - a.x, dy = b.y - a.y;
327
+ return dr < 0 || dr * dr < dx * dx + dy * dy;
328
+ }
329
+
330
+ function enclosesWeak(a, b) {
331
+ var dr = a.r - b.r + Math.max(a.r, b.r, 1) * 1e-9, dx = b.x - a.x, dy = b.y - a.y;
332
+ return dr > 0 && dr * dr > dx * dx + dy * dy;
333
+ }
334
+
335
+ function enclosesWeakAll(a, B) {
336
+ for (var i = 0; i < B.length; ++i) {
337
+ if (!enclosesWeak(a, B[i])) {
338
+ return false;
339
+ }
340
+ }
341
+ return true;
342
+ }
343
+
344
+ function encloseBasis(B) {
345
+ switch (B.length) {
346
+ case 1: return encloseBasis1(B[0]);
347
+ case 2: return encloseBasis2(B[0], B[1]);
348
+ case 3: return encloseBasis3(B[0], B[1], B[2]);
349
+ }
350
+ }
351
+
352
+ function encloseBasis1(a) {
353
+ return {
354
+ x: a.x,
355
+ y: a.y,
356
+ r: a.r
357
+ };
358
+ }
359
+
360
+ function encloseBasis2(a, b) {
361
+ var x1 = a.x, y1 = a.y, r1 = a.r,
362
+ x2 = b.x, y2 = b.y, r2 = b.r,
363
+ x21 = x2 - x1, y21 = y2 - y1, r21 = r2 - r1,
364
+ l = Math.sqrt(x21 * x21 + y21 * y21);
365
+ return {
366
+ x: (x1 + x2 + x21 / l * r21) / 2,
367
+ y: (y1 + y2 + y21 / l * r21) / 2,
368
+ r: (l + r1 + r2) / 2
369
+ };
370
+ }
371
+
372
+ function encloseBasis3(a, b, c) {
373
+ var x1 = a.x, y1 = a.y, r1 = a.r,
374
+ x2 = b.x, y2 = b.y, r2 = b.r,
375
+ x3 = c.x, y3 = c.y, r3 = c.r,
376
+ a2 = x1 - x2,
377
+ a3 = x1 - x3,
378
+ b2 = y1 - y2,
379
+ b3 = y1 - y3,
380
+ c2 = r2 - r1,
381
+ c3 = r3 - r1,
382
+ d1 = x1 * x1 + y1 * y1 - r1 * r1,
383
+ d2 = d1 - x2 * x2 - y2 * y2 + r2 * r2,
384
+ d3 = d1 - x3 * x3 - y3 * y3 + r3 * r3,
385
+ ab = a3 * b2 - a2 * b3,
386
+ xa = (b2 * d3 - b3 * d2) / (ab * 2) - x1,
387
+ xb = (b3 * c2 - b2 * c3) / ab,
388
+ ya = (a3 * d2 - a2 * d3) / (ab * 2) - y1,
389
+ yb = (a2 * c3 - a3 * c2) / ab,
390
+ A = xb * xb + yb * yb - 1,
391
+ B = 2 * (r1 + xa * xb + ya * yb),
392
+ C = xa * xa + ya * ya - r1 * r1,
393
+ r = -(Math.abs(A) > 1e-6 ? (B + Math.sqrt(B * B - 4 * A * C)) / (2 * A) : C / B);
394
+ return {
395
+ x: x1 + xa + xb * r,
396
+ y: y1 + ya + yb * r,
397
+ r: r
398
+ };
399
+ }
400
+
401
+ function place(b, a, c) {
402
+ var dx = b.x - a.x, x, a2,
403
+ dy = b.y - a.y, y, b2,
404
+ d2 = dx * dx + dy * dy;
405
+ if (d2) {
406
+ a2 = a.r + c.r, a2 *= a2;
407
+ b2 = b.r + c.r, b2 *= b2;
408
+ if (a2 > b2) {
409
+ x = (d2 + b2 - a2) / (2 * d2);
410
+ y = Math.sqrt(Math.max(0, b2 / d2 - x * x));
411
+ c.x = b.x - x * dx - y * dy;
412
+ c.y = b.y - x * dy + y * dx;
413
+ } else {
414
+ x = (d2 + a2 - b2) / (2 * d2);
415
+ y = Math.sqrt(Math.max(0, a2 / d2 - x * x));
416
+ c.x = a.x + x * dx - y * dy;
417
+ c.y = a.y + x * dy + y * dx;
418
+ }
419
+ } else {
420
+ c.x = a.x + c.r;
421
+ c.y = a.y;
422
+ }
423
+ }
424
+
425
+ function intersects(a, b) {
426
+ var dr = a.r + b.r - 1e-6, dx = b.x - a.x, dy = b.y - a.y;
427
+ return dr > 0 && dr * dr > dx * dx + dy * dy;
428
+ }
429
+
430
+ function score(node) {
431
+ var a = node._,
432
+ b = node.next._,
433
+ ab = a.r + b.r,
434
+ dx = (a.x * b.r + b.x * a.r) / ab,
435
+ dy = (a.y * b.r + b.y * a.r) / ab;
436
+ return dx * dx + dy * dy;
437
+ }
438
+
439
+ function Node(circle) {
440
+ this._ = circle;
441
+ this.next = null;
442
+ this.previous = null;
443
+ }
444
+
445
+ function packSiblingsRandom(circles, random) {
446
+ if (!(n = (circles = array(circles)).length)) return 0;
447
+
448
+ var a, b, c, n, aa, ca, i, j, k, sj, sk;
449
+
450
+ // Place the first circle.
451
+ a = circles[0], a.x = 0, a.y = 0;
452
+ if (!(n > 1)) return a.r;
453
+
454
+ // Place the second circle.
455
+ b = circles[1], a.x = -b.r, b.x = a.r, b.y = 0;
456
+ if (!(n > 2)) return a.r + b.r;
457
+
458
+ // Place the third circle.
459
+ place(b, a, c = circles[2]);
460
+
461
+ // Initialize the front-chain using the first three circles a, b and c.
462
+ a = new Node(a), b = new Node(b), c = new Node(c);
463
+ a.next = c.previous = b;
464
+ b.next = a.previous = c;
465
+ c.next = b.previous = a;
466
+
467
+ // Attempt to place each remaining circle…
468
+ pack: for (i = 3; i < n; ++i) {
469
+ place(a._, b._, c = circles[i]), c = new Node(c);
470
+
471
+ // Find the closest intersecting circle on the front-chain, if any.
472
+ // “Closeness” is determined by linear distance along the front-chain.
473
+ // “Ahead” or “behind” is likewise determined by linear distance.
474
+ j = b.next, k = a.previous, sj = b._.r, sk = a._.r;
475
+ do {
476
+ if (sj <= sk) {
477
+ if (intersects(j._, c._)) {
478
+ b = j, a.next = b, b.previous = a, --i;
479
+ continue pack;
480
+ }
481
+ sj += j._.r, j = j.next;
482
+ } else {
483
+ if (intersects(k._, c._)) {
484
+ a = k, a.next = b, b.previous = a, --i;
485
+ continue pack;
486
+ }
487
+ sk += k._.r, k = k.previous;
488
+ }
489
+ } while (j !== k.next);
490
+
491
+ // Success! Insert the new circle c between a and b.
492
+ c.previous = a, c.next = b, a.next = b.previous = b = c;
493
+
494
+ // Compute the new closest circle pair to the centroid.
495
+ aa = score(a);
496
+ while ((c = c.next) !== b) {
497
+ if ((ca = score(c)) < aa) {
498
+ a = c, aa = ca;
499
+ }
500
+ }
501
+ b = a.next;
502
+ }
503
+
504
+ // Compute the enclosing circle of the front chain.
505
+ a = [b._], c = b; while ((c = c.next) !== b) a.push(c._); c = packEncloseRandom(a, random);
506
+
507
+ // Translate the circles to put the enclosing circle around the origin.
508
+ for (i = 0; i < n; ++i) a = circles[i], a.x -= c.x, a.y -= c.y;
509
+
510
+ return c.r;
511
+ }
512
+
513
+ function defaultRadius(d) {
514
+ return Math.sqrt(d.value);
515
+ }
516
+
517
+ function index() {
518
+ var radius = null,
519
+ dx = 1,
520
+ dy = 1,
521
+ padding = constantZero;
522
+
523
+ function pack(root) {
524
+ const random = lcg();
525
+ root.x = dx / 2, root.y = dy / 2;
526
+ if (radius) {
527
+ root.eachBefore(radiusLeaf(radius))
528
+ .eachAfter(packChildrenRandom(padding, 0.5, random))
529
+ .eachBefore(translateChild(1));
530
+ } else {
531
+ root.eachBefore(radiusLeaf(defaultRadius))
532
+ .eachAfter(packChildrenRandom(constantZero, 1, random))
533
+ .eachAfter(packChildrenRandom(padding, root.r / Math.min(dx, dy), random))
534
+ .eachBefore(translateChild(Math.min(dx, dy) / (2 * root.r)));
535
+ }
536
+ return root;
537
+ }
538
+
539
+ pack.radius = function(x) {
540
+ return arguments.length ? (radius = optional(x), pack) : radius;
541
+ };
542
+
543
+ pack.size = function(x) {
544
+ return arguments.length ? (dx = +x[0], dy = +x[1], pack) : [dx, dy];
545
+ };
546
+
547
+ pack.padding = function(x) {
548
+ return arguments.length ? (padding = typeof x === "function" ? x : constant(+x), pack) : padding;
549
+ };
550
+
551
+ return pack;
552
+ }
553
+
554
+ function radiusLeaf(radius) {
555
+ return function(node) {
556
+ if (!node.children) {
557
+ node.r = Math.max(0, +radius(node) || 0);
558
+ }
559
+ };
560
+ }
561
+
562
+ function packChildrenRandom(padding, k, random) {
563
+ return function(node) {
564
+ if (children = node.children) {
565
+ var children,
566
+ i,
567
+ n = children.length,
568
+ r = padding(node) * k || 0,
569
+ e;
570
+
571
+ if (r) for (i = 0; i < n; ++i) children[i].r += r;
572
+ e = packSiblingsRandom(children, random);
573
+ if (r) for (i = 0; i < n; ++i) children[i].r -= r;
574
+ node.r = e + r;
575
+ }
576
+ };
577
+ }
578
+
579
+ function translateChild(k) {
580
+ return function(node) {
581
+ var parent = node.parent;
582
+ node.r *= k;
583
+ if (parent) {
584
+ node.x = parent.x + k * node.x;
585
+ node.y = parent.y + k * node.y;
586
+ }
587
+ };
588
+ }
589
+
590
+ export { Node$1 as Node, hierarchy, index as pack };