@thi.ng/arrays 2.8.7 → 2.8.8
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/CHANGELOG.md +1 -1
- package/arg-sort.d.ts +7 -3
- package/argmin.d.ts +7 -3
- package/binary-search.d.ts +16 -8
- package/blit.d.ts +13 -11
- package/fill-range.d.ts +7 -3
- package/floyd-rivest.d.ts +4 -2
- package/is-sorted.d.ts +7 -3
- package/levenshtein.d.ts +14 -10
- package/package.json +12 -12
- package/quicksort.d.ts +11 -8
- package/rotate.d.ts +1 -1
- package/sort-cached.d.ts +8 -4
- package/swap.d.ts +13 -8
- package/swizzle.d.ts +20 -6
- package/threshold.d.ts +9 -5
- package/topo-sort.d.ts +10 -5
package/CHANGELOG.md
CHANGED
package/arg-sort.d.ts
CHANGED
|
@@ -7,12 +7,14 @@
|
|
|
7
7
|
* Also see {@link swizzle} to read an array in custom order.
|
|
8
8
|
*
|
|
9
9
|
* @example
|
|
10
|
-
* ```ts
|
|
10
|
+
* ```ts tangle:../export/arg-sort.ts
|
|
11
11
|
* import { argSort, swizzle } from "@thi.ng/arrays";
|
|
12
12
|
*
|
|
13
13
|
* const src = ["a", "c", "d", "b"];
|
|
14
14
|
*
|
|
15
|
-
*
|
|
15
|
+
* console.log(
|
|
16
|
+
* argSort(src)
|
|
17
|
+
* );
|
|
16
18
|
* // [ 0, 3, 1, 2 ]
|
|
17
19
|
*
|
|
18
20
|
* // src[0] => "a"
|
|
@@ -20,7 +22,9 @@
|
|
|
20
22
|
* // src[1] => "c"
|
|
21
23
|
* // src[2] => "d"
|
|
22
24
|
*
|
|
23
|
-
*
|
|
25
|
+
* console.log(
|
|
26
|
+
* swizzle(argSort(src))(src)
|
|
27
|
+
* );
|
|
24
28
|
* // [ 'a', 'b', 'c', 'd' ]
|
|
25
29
|
* ```
|
|
26
30
|
*
|
package/argmin.d.ts
CHANGED
|
@@ -8,14 +8,18 @@ import type { Predicate2 } from "@thi.ng/api";
|
|
|
8
8
|
* See {@link argMax}.
|
|
9
9
|
*
|
|
10
10
|
* @example
|
|
11
|
-
* ```ts
|
|
11
|
+
* ```ts tangle:../export/argmin.ts
|
|
12
12
|
* import { argMin } from "@thi.ng/arrays";
|
|
13
13
|
*
|
|
14
|
-
*
|
|
14
|
+
* console.log(
|
|
15
|
+
* argMin([42, 11, 66, 23])
|
|
16
|
+
* );
|
|
15
17
|
* // 1
|
|
16
18
|
*
|
|
17
19
|
* // same as argmax() with defaults
|
|
18
|
-
*
|
|
20
|
+
* console.log(
|
|
21
|
+
* argMin([42, 11, 66, 23], -Infinity, (a, b) => a > b)
|
|
22
|
+
* );
|
|
19
23
|
* // 2
|
|
20
24
|
* ```
|
|
21
25
|
*
|
package/binary-search.d.ts
CHANGED
|
@@ -16,10 +16,12 @@ import type { Comparator, Fn, FnN, FnN2 } from "@thi.ng/api";
|
|
|
16
16
|
* compatible with that of `cmp`.
|
|
17
17
|
*
|
|
18
18
|
* @example
|
|
19
|
-
* ```ts
|
|
19
|
+
* ```ts tangle:../export/binary-search1.ts
|
|
20
20
|
* import { binarySearch } from "@thi.ng/arrays";
|
|
21
21
|
*
|
|
22
|
-
*
|
|
22
|
+
* console.log(
|
|
23
|
+
* binarySearch([2, 4, 6], 5)
|
|
24
|
+
* );
|
|
23
25
|
* // -3
|
|
24
26
|
* ```
|
|
25
27
|
*
|
|
@@ -82,10 +84,12 @@ export declare const binarySearch32: (buf: ArrayLike<number>, x: number) => numb
|
|
|
82
84
|
* values exist.
|
|
83
85
|
*
|
|
84
86
|
* @example
|
|
85
|
-
* ```ts
|
|
87
|
+
* ```ts tangle:../export/binary-search2.ts
|
|
86
88
|
* import { binarySearch, bsLT } from "@thi.ng/arrays";
|
|
87
89
|
*
|
|
88
|
-
*
|
|
90
|
+
* console.log(
|
|
91
|
+
* bsLT(binarySearch([10, 20, 30, 40], 25))
|
|
92
|
+
* )
|
|
89
93
|
* // 1
|
|
90
94
|
* ```
|
|
91
95
|
*
|
|
@@ -104,15 +108,19 @@ export declare const bsLE: FnN;
|
|
|
104
108
|
* such values exist.
|
|
105
109
|
*
|
|
106
110
|
* @example
|
|
107
|
-
* ```ts
|
|
111
|
+
* ```ts tangle:../export/binary-search3.ts
|
|
108
112
|
* import { binarySearch, bsGT } from "@thi.ng/arrays";
|
|
109
113
|
*
|
|
110
|
-
* src = [10, 20, 30, 40];
|
|
114
|
+
* const src = [10, 20, 30, 40];
|
|
111
115
|
*
|
|
112
|
-
*
|
|
116
|
+
* console.log(
|
|
117
|
+
* bsGT(binarySearch(src, 25), src.length)
|
|
118
|
+
* )
|
|
113
119
|
* // 2
|
|
114
120
|
*
|
|
115
|
-
*
|
|
121
|
+
* console.log(
|
|
122
|
+
* bsGT(binarySearch(src, 40), src.length)
|
|
123
|
+
* )
|
|
116
124
|
* // -1
|
|
117
125
|
* ```
|
|
118
126
|
*
|
package/blit.d.ts
CHANGED
|
@@ -9,19 +9,21 @@ import type { Fn3, TypedArray } from "@thi.ng/api";
|
|
|
9
9
|
* the [0..dest.length) interval.
|
|
10
10
|
*
|
|
11
11
|
* @example
|
|
12
|
-
* ```ts
|
|
12
|
+
* ```ts tangle:../export/blit1d.ts
|
|
13
13
|
* import { blit1d } from "@thi.ng/arrays";
|
|
14
14
|
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
15
|
+
* console.log(
|
|
16
|
+
* blit1d(
|
|
17
|
+
* // dest array
|
|
18
|
+
* [1, 1, 1, 1, 1, 1, 1, 1, 1],
|
|
19
|
+
* // paste from index 2
|
|
20
|
+
* 2,
|
|
21
|
+
* // source array
|
|
22
|
+
* [2, 3, 2, 3, 2],
|
|
23
|
+
* // mask value
|
|
24
|
+
* 3
|
|
25
|
+
* )
|
|
26
|
+
* );
|
|
25
27
|
* //[1, 1, 2, 1, 2, 1, 2, 1, 1]
|
|
26
28
|
* ```
|
|
27
29
|
*
|
package/fill-range.d.ts
CHANGED
|
@@ -8,13 +8,17 @@ import type { TypedArray } from "@thi.ng/api";
|
|
|
8
8
|
* or -1 (depending on range). Returns array.
|
|
9
9
|
*
|
|
10
10
|
* @example
|
|
11
|
-
* ```ts
|
|
11
|
+
* ```ts tangle:../export/fill-range.ts
|
|
12
12
|
* import { fillRange } from "@thi.ng/arrays";
|
|
13
13
|
*
|
|
14
|
-
*
|
|
14
|
+
* console.log(
|
|
15
|
+
* fillRange(new Array(5))
|
|
16
|
+
* );
|
|
15
17
|
* // [ 0, 1, 2, 3, 4 ]
|
|
16
18
|
*
|
|
17
|
-
*
|
|
19
|
+
* console.log(
|
|
20
|
+
* fillRange(fillRange([], 0, 10, 20, 2), 5, 20, 8, -2)
|
|
21
|
+
* );
|
|
18
22
|
* // [ 10, 12, 14, 16, 18, 20, 18, 16, 14, 12, 10 ]
|
|
19
23
|
* ```
|
|
20
24
|
*
|
package/floyd-rivest.d.ts
CHANGED
|
@@ -12,10 +12,12 @@ import type { Comparator } from "@thi.ng/api";
|
|
|
12
12
|
* the `[left, k]` interval are the smallest.
|
|
13
13
|
*
|
|
14
14
|
* @example
|
|
15
|
-
* ```ts
|
|
15
|
+
* ```ts tangle:../export/floyd-rivest.ts
|
|
16
16
|
* import { floydRivest } from "@thi.ng/arrays";
|
|
17
17
|
*
|
|
18
|
-
*
|
|
18
|
+
* console.log(
|
|
19
|
+
* floydRivest([5, 3, -1, -10, 20, 7, 0, 4, 2], 3)
|
|
20
|
+
* );
|
|
19
21
|
* // [ -10, 0, -1, 2, 3, 4, 5, 20, 7 ]
|
|
20
22
|
* ```
|
|
21
23
|
*
|
package/is-sorted.d.ts
CHANGED
|
@@ -10,14 +10,18 @@ import type { Comparator } from "@thi.ng/api";
|
|
|
10
10
|
* two elements. No bounds checking.
|
|
11
11
|
*
|
|
12
12
|
* @example
|
|
13
|
-
* ```ts
|
|
13
|
+
* ```ts tangle:../export/is-sorted.ts
|
|
14
14
|
* import { isSorted } from "@thi.ng/arrays";
|
|
15
15
|
*
|
|
16
|
-
*
|
|
16
|
+
* console.log(
|
|
17
|
+
* isSorted([3, 2, 1])
|
|
18
|
+
* );
|
|
17
19
|
* // false
|
|
18
20
|
*
|
|
19
21
|
* // w/ custom comparator
|
|
20
|
-
*
|
|
22
|
+
* console.log(
|
|
23
|
+
* isSorted([3, 2, 1], (a, b) => b - a)
|
|
24
|
+
* );
|
|
21
25
|
* // true
|
|
22
26
|
* ```
|
|
23
27
|
*
|
package/levenshtein.d.ts
CHANGED
|
@@ -13,20 +13,24 @@ import type { Predicate2 } from "@thi.ng/api";
|
|
|
13
13
|
* - https://github.com/gustf/js-levenshtein/blob/develop/index.js
|
|
14
14
|
*
|
|
15
15
|
* @example
|
|
16
|
-
* ```ts
|
|
16
|
+
* ```ts tangle:../export/levenshtein.ts
|
|
17
17
|
* import { levenshtein } from "@thi.ng/arrays";
|
|
18
18
|
*
|
|
19
|
-
*
|
|
19
|
+
* console.log(
|
|
20
|
+
* levenshtein([1, 2, 3, 4, 5], [1, 2, 4, 3, 5])
|
|
21
|
+
* );
|
|
20
22
|
* // 2
|
|
21
23
|
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
* )
|
|
24
|
+
* console.log(
|
|
25
|
+
* levenshtein(
|
|
26
|
+
* [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }, { id: 5 }],
|
|
27
|
+
* [{ id: 4 }, { id: 5 }, { id: 3 }, { id: 1 }, { id: 2 }],
|
|
28
|
+
* // max dist
|
|
29
|
+
* 2,
|
|
30
|
+
* // predicate
|
|
31
|
+
* (a, b) => a.id === b.id
|
|
32
|
+
* )
|
|
33
|
+
* );
|
|
30
34
|
* // Infinity
|
|
31
35
|
* ```
|
|
32
36
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/arrays",
|
|
3
|
-
"version": "2.8.
|
|
3
|
+
"version": "2.8.8",
|
|
4
4
|
"description": "Array / Arraylike utilities",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -36,19 +36,19 @@
|
|
|
36
36
|
"tool:tangle": "../../node_modules/.bin/tangle src/**/*.ts"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@thi.ng/api": "^8.9.
|
|
40
|
-
"@thi.ng/checks": "^3.5.
|
|
41
|
-
"@thi.ng/compare": "^2.2.
|
|
42
|
-
"@thi.ng/equiv": "^2.1.
|
|
43
|
-
"@thi.ng/errors": "^2.
|
|
44
|
-
"@thi.ng/random": "^3.6.
|
|
39
|
+
"@thi.ng/api": "^8.9.30",
|
|
40
|
+
"@thi.ng/checks": "^3.5.3",
|
|
41
|
+
"@thi.ng/compare": "^2.2.26",
|
|
42
|
+
"@thi.ng/equiv": "^2.1.52",
|
|
43
|
+
"@thi.ng/errors": "^2.5.0",
|
|
44
|
+
"@thi.ng/random": "^3.6.37"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
|
-
"@microsoft/api-extractor": "^7.
|
|
48
|
-
"esbuild": "^0.20.
|
|
47
|
+
"@microsoft/api-extractor": "^7.42.3",
|
|
48
|
+
"esbuild": "^0.20.1",
|
|
49
49
|
"rimraf": "^5.0.5",
|
|
50
|
-
"typedoc": "^0.25.
|
|
51
|
-
"typescript": "^5.
|
|
50
|
+
"typedoc": "^0.25.12",
|
|
51
|
+
"typescript": "^5.4.2"
|
|
52
52
|
},
|
|
53
53
|
"keywords": [
|
|
54
54
|
"aos",
|
|
@@ -166,5 +166,5 @@
|
|
|
166
166
|
"thi.ng": {
|
|
167
167
|
"year": 2018
|
|
168
168
|
},
|
|
169
|
-
"gitHead": "
|
|
169
|
+
"gitHead": "7f3fcbd6c0462b0ce45afa141fe163d1f297fd51\n"
|
|
170
170
|
}
|
package/quicksort.d.ts
CHANGED
|
@@ -16,20 +16,23 @@ import type { Comparator, Fn3, TypedArray } from "@thi.ng/api";
|
|
|
16
16
|
* - https://en.wikipedia.org/wiki/Quicksort#Hoare_partition_scheme
|
|
17
17
|
*
|
|
18
18
|
* @example
|
|
19
|
-
* ```ts
|
|
20
|
-
* import { quickSort } from "@thi.ng/arrays";
|
|
19
|
+
* ```ts tangle:../export/quicksort.ts
|
|
20
|
+
* import { quickSort, multiSwap } from "@thi.ng/arrays";
|
|
21
21
|
*
|
|
22
|
-
* a = [4, 3, 1, 8, 5]
|
|
23
|
-
* b = [40, 30, 10, 80, 50]
|
|
24
|
-
* c = [-4, -3, -1, -8, -5]
|
|
22
|
+
* const a = [4, 3, 1, 8, 5]
|
|
23
|
+
* const b = [40, 30, 10, 80, 50]
|
|
24
|
+
* const c = [-4, -3, -1, -8, -5]
|
|
25
25
|
*
|
|
26
26
|
* // use `multiSwap` to sort extra arrays based on sort order of `a`
|
|
27
|
-
*
|
|
27
|
+
* console.log(
|
|
28
|
+
* quickSort(a, undefined, multiSwap(b, c))
|
|
29
|
+
* );
|
|
28
30
|
* // [ 1, 3, 4, 5, 8 ] (a)
|
|
29
31
|
*
|
|
30
|
-
* b
|
|
32
|
+
* console.log(b);
|
|
31
33
|
* // [ 10, 30, 40, 50, 80 ]
|
|
32
|
-
*
|
|
34
|
+
*
|
|
35
|
+
* console.log(c);
|
|
33
36
|
* // [ -1, -3, -4, -5, -8 ]
|
|
34
37
|
* ```
|
|
35
38
|
*
|
package/rotate.d.ts
CHANGED
|
@@ -11,7 +11,7 @@ import type { TypedArray } from "@thi.ng/api";
|
|
|
11
11
|
* @param buf
|
|
12
12
|
* @param num
|
|
13
13
|
*/
|
|
14
|
-
export declare const rotate: <T>(buf: T
|
|
14
|
+
export declare const rotate: <T>(buf: Array<T>, num: number) => T[];
|
|
15
15
|
/**
|
|
16
16
|
* Same as {@link rotate}, but for optimized for typed arrays!
|
|
17
17
|
*
|
package/sort-cached.d.ts
CHANGED
|
@@ -13,14 +13,18 @@ import type { Comparator, Fn } from "@thi.ng/api";
|
|
|
13
13
|
* re-evaluated multiple times from within a comparator.
|
|
14
14
|
*
|
|
15
15
|
* @example
|
|
16
|
-
* ```ts
|
|
16
|
+
* ```ts tangle:../export/sort-cached.ts
|
|
17
17
|
* import { sortByCachedKey } from "@thi.ng/arrays";
|
|
18
18
|
*
|
|
19
19
|
* // sort by length in descending order
|
|
20
|
-
*
|
|
20
|
+
* console.log(
|
|
21
|
+
* sortByCachedKey(["a","bbbb","ccc","dd"], (x) => x.length, (a, b) => b - a)
|
|
22
|
+
* );
|
|
21
23
|
* // [ 'bbbb', 'ccc', 'dd', 'a' ]
|
|
22
24
|
*
|
|
23
|
-
*
|
|
25
|
+
* console.log(
|
|
26
|
+
* sortByCachedKey(["a", "b", "c", "d"], [3, 2, 1, 0])
|
|
27
|
+
* );
|
|
24
28
|
* // [ 'd', 'c', 'b', 'a' ]
|
|
25
29
|
* ```
|
|
26
30
|
*
|
|
@@ -28,5 +32,5 @@ import type { Comparator, Fn } from "@thi.ng/api";
|
|
|
28
32
|
* @param key -
|
|
29
33
|
* @param cmp -
|
|
30
34
|
*/
|
|
31
|
-
export declare const sortByCachedKey: <T, K>(src: T[], key: Fn<T, K
|
|
35
|
+
export declare const sortByCachedKey: <T, K>(src: T[], key: K[] | Fn<T, K>, cmp?: Comparator<K>) => T[];
|
|
32
36
|
//# sourceMappingURL=sort-cached.d.ts.map
|
package/swap.d.ts
CHANGED
|
@@ -21,19 +21,24 @@ export declare const swap: (arr: AnyArray, x: number, y: number) => void;
|
|
|
21
21
|
* {@link quickSort}
|
|
22
22
|
*
|
|
23
23
|
* @example
|
|
24
|
-
* ```ts
|
|
24
|
+
* ```ts tangle:../export/multiswap.ts
|
|
25
25
|
* import { multiSwap } from "@thi.ng/arrays";
|
|
26
26
|
*
|
|
27
|
-
* a = [2, 1];
|
|
28
|
-
* b = [20, 10];
|
|
29
|
-
* c = [40, 30];
|
|
27
|
+
* const a = [2, 1];
|
|
28
|
+
* const b = [20, 10];
|
|
29
|
+
* const c = [40, 30];
|
|
30
30
|
*
|
|
31
|
-
* ms = multiSwap(b, c);
|
|
31
|
+
* const ms = multiSwap(b, c);
|
|
32
32
|
* ms(a, 0, 1);
|
|
33
33
|
*
|
|
34
|
-
*
|
|
35
|
-
* //
|
|
36
|
-
*
|
|
34
|
+
* console.log(a);
|
|
35
|
+
* // [1, 2]
|
|
36
|
+
*
|
|
37
|
+
* console.log(b);
|
|
38
|
+
* // [10, 20]
|
|
39
|
+
*
|
|
40
|
+
* console.log(c);
|
|
41
|
+
* // [30, 40]
|
|
37
42
|
* ```
|
|
38
43
|
*
|
|
39
44
|
* @param xs - arrays to swap in later
|
package/swizzle.d.ts
CHANGED
|
@@ -8,22 +8,36 @@ import type { Fn } from "@thi.ng/api";
|
|
|
8
8
|
* approach is used.
|
|
9
9
|
*
|
|
10
10
|
* @example
|
|
11
|
-
* ```ts
|
|
11
|
+
* ```ts tangle:../export/swizzle1.ts
|
|
12
12
|
* import { swizzle } from "@thi.ng/arrays";
|
|
13
13
|
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
14
|
+
* console.log(
|
|
15
|
+
* swizzle([0, 0, 0])([1, 2, 3, 4])
|
|
16
|
+
* );
|
|
17
|
+
* // [ 1, 1, 1 ]
|
|
18
|
+
*
|
|
19
|
+
* console.log(
|
|
20
|
+
* swizzle([1, 1, 3, 3])([1, 2, 3, 4])
|
|
21
|
+
* );
|
|
22
|
+
* // [ 2, 2, 4, 4 ]
|
|
23
|
+
*
|
|
24
|
+
* console.log(
|
|
25
|
+
* swizzle([2, 0])([1, 2, 3])
|
|
26
|
+
* );
|
|
27
|
+
* // [ 3, 1 ]
|
|
17
28
|
* ```
|
|
18
29
|
*
|
|
19
30
|
* @example
|
|
20
31
|
* Objects can be used as input to the generated function, but the
|
|
21
32
|
* result will always be in array form.
|
|
22
33
|
|
|
23
|
-
* ```ts
|
|
34
|
+
* ```ts tangle:../export/swizzle2.ts
|
|
24
35
|
* import { swizzle } from "@thi.ng/arrays";
|
|
25
36
|
*
|
|
26
|
-
*
|
|
37
|
+
* console.log(
|
|
38
|
+
* swizzle(["a", "c", "b"])({a: 1, b: 2, c: 3})
|
|
39
|
+
* );
|
|
40
|
+
* // [ 1, 3, 2 ]
|
|
27
41
|
* ```
|
|
28
42
|
*
|
|
29
43
|
* @param order - indices
|
package/threshold.d.ts
CHANGED
|
@@ -9,18 +9,22 @@
|
|
|
9
9
|
* comparison.
|
|
10
10
|
*
|
|
11
11
|
* @example
|
|
12
|
-
* ```ts
|
|
12
|
+
* ```ts tangle:../export/select-threshold.ts
|
|
13
13
|
* import { selectThresholdMin } from "@thi.ng/arrays";
|
|
14
14
|
*
|
|
15
15
|
* const numColumns = selectThresholdMin({ 480: 1, 640: 2, 960: 3 }, 4);
|
|
16
16
|
*
|
|
17
|
-
* numColumns(320)
|
|
17
|
+
* console.log(numColumns(320));
|
|
18
|
+
* // 1
|
|
18
19
|
*
|
|
19
|
-
* numColumns(481)
|
|
20
|
+
* console.log(numColumns(481));
|
|
21
|
+
* // 2
|
|
20
22
|
*
|
|
21
|
-
* numColumns(768)
|
|
23
|
+
* console.log(numColumns(768));
|
|
24
|
+
* // 3
|
|
22
25
|
*
|
|
23
|
-
* numColumns(1024)
|
|
26
|
+
* console.log(numColumns(1024));
|
|
27
|
+
* // 4
|
|
24
28
|
* ```
|
|
25
29
|
*
|
|
26
30
|
* @param thresholds
|
package/topo-sort.d.ts
CHANGED
|
@@ -11,22 +11,27 @@ import type { Fn, Nullable, IObjectOf } from "@thi.ng/api";
|
|
|
11
11
|
* cycle will be part of the error message (see second example below).
|
|
12
12
|
*
|
|
13
13
|
* @example
|
|
14
|
-
* ```ts
|
|
14
|
+
* ```ts tangle:../export/topo-sort.ts
|
|
15
15
|
* import { topoSort } from "@thi.ng/arrays";
|
|
16
16
|
*
|
|
17
|
-
* const graph = {
|
|
17
|
+
* const graph: Record<string, { deps?: string[] }> = {
|
|
18
18
|
* a: { deps: ["c", "b"] },
|
|
19
19
|
* b: {},
|
|
20
20
|
* c: { deps: ["d"] },
|
|
21
21
|
* d: { deps: ["b"] }
|
|
22
22
|
* };
|
|
23
|
-
*
|
|
23
|
+
*
|
|
24
|
+
* console.log(
|
|
25
|
+
* topoSort(graph, (node) => node.deps)
|
|
26
|
+
* );
|
|
24
27
|
* // [ "b", "d", "c", "a" ]
|
|
25
28
|
*
|
|
26
29
|
* // An error will be thrown if the graph contains cycles...
|
|
27
|
-
* graph.d.deps
|
|
30
|
+
* graph.d.deps!.push("a");
|
|
28
31
|
*
|
|
29
|
-
*
|
|
32
|
+
* console.log(
|
|
33
|
+
* topoSort(graph, (node) => node.deps)
|
|
34
|
+
* );
|
|
30
35
|
* // Uncaught Error: illegal state: dependency cycle: a -> c -> d -> a
|
|
31
36
|
* ```
|
|
32
37
|
*
|