@thi.ng/arrays 2.7.7 → 2.7.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/api.js +0 -1
- package/arg-sort.js +4 -28
- package/argmin.js +14 -40
- package/binary-search.js +69 -176
- package/bisect.js +10 -21
- package/blit.js +33 -30
- package/ends-with.js +11 -24
- package/ensure-array.js +6 -20
- package/ensure-iterable.js +6 -10
- package/fill-range.js +12 -33
- package/find.js +13 -26
- package/floyd-rivest.js +45 -65
- package/fuzzy-match.js +21 -36
- package/insert.js +11 -34
- package/into.js +10 -15
- package/is-sorted.js +12 -34
- package/iterator.js +12 -22
- package/levenshtein.js +90 -136
- package/package.json +12 -9
- package/peek.js +6 -12
- package/quicksort.js +22 -19
- package/rotate.js +24 -40
- package/shuffle.js +18 -35
- package/sort-cached.js +8 -32
- package/starts-with.js +11 -24
- package/swap.js +39 -71
- package/swizzle.js +33 -55
- package/threshold.js +16 -48
- package/topo-sort.js +22 -52
package/CHANGELOG.md
CHANGED
package/api.js
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/arg-sort.js
CHANGED
|
@@ -1,31 +1,7 @@
|
|
|
1
1
|
import { compare } from "@thi.ng/compare/compare";
|
|
2
2
|
import { fillRange } from "./fill-range.js";
|
|
3
3
|
import { sortByCachedKey } from "./sort-cached.js";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
*
|
|
9
|
-
* @remarks
|
|
10
|
-
* Also see {@link swizzle} to read an array in custom order.
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* ```ts
|
|
14
|
-
* const src = ["a", "c", "d", "b"];
|
|
15
|
-
*
|
|
16
|
-
* argSort(src)
|
|
17
|
-
* // [ 0, 3, 1, 2 ]
|
|
18
|
-
*
|
|
19
|
-
* // src[0] => "a"
|
|
20
|
-
* // src[3] => "b"
|
|
21
|
-
* // src[1] => "c"
|
|
22
|
-
* // src[2] => "d"
|
|
23
|
-
*
|
|
24
|
-
* swizzle(argSort(src))(src)
|
|
25
|
-
* // [ 'a', 'b', 'c', 'd' ]
|
|
26
|
-
* ```
|
|
27
|
-
*
|
|
28
|
-
* @param src -
|
|
29
|
-
* @param cmp -
|
|
30
|
-
*/
|
|
31
|
-
export const argSort = (src, cmp = compare) => sortByCachedKey(fillRange(new Array(src.length)), src.slice(), cmp);
|
|
4
|
+
const argSort = (src, cmp = compare) => sortByCachedKey(fillRange(new Array(src.length)), src.slice(), cmp);
|
|
5
|
+
export {
|
|
6
|
+
argSort
|
|
7
|
+
};
|
package/argmin.js
CHANGED
|
@@ -1,42 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* ```ts
|
|
11
|
-
* argMin([42, 11, 66, 23])
|
|
12
|
-
* // 1
|
|
13
|
-
*
|
|
14
|
-
* // same as argmax() with defaults
|
|
15
|
-
* argMin([42, 11, 66, 23], -Infinity, (a, b) => a > b)
|
|
16
|
-
* // 2
|
|
17
|
-
* ```
|
|
18
|
-
*
|
|
19
|
-
* @param buf
|
|
20
|
-
* @param min
|
|
21
|
-
* @param pred
|
|
22
|
-
*/
|
|
23
|
-
export const argMin = (buf, min = Infinity, pred = (a, b) => a < b) => {
|
|
24
|
-
let id = -1;
|
|
25
|
-
for (let i = 0, n = buf.length; i < n; i++) {
|
|
26
|
-
const x = buf[i];
|
|
27
|
-
if (pred(x, min)) {
|
|
28
|
-
min = x;
|
|
29
|
-
id = i;
|
|
30
|
-
}
|
|
1
|
+
const argMin = (buf, min = Infinity, pred = (a, b) => a < b) => {
|
|
2
|
+
let id = -1;
|
|
3
|
+
for (let i = 0, n = buf.length; i < n; i++) {
|
|
4
|
+
const x = buf[i];
|
|
5
|
+
if (pred(x, min)) {
|
|
6
|
+
min = x;
|
|
7
|
+
id = i;
|
|
31
8
|
}
|
|
32
|
-
|
|
9
|
+
}
|
|
10
|
+
return id;
|
|
11
|
+
};
|
|
12
|
+
const argMax = (items, min = -Infinity, pred = (a, b) => a > b) => argMin(items, min, pred);
|
|
13
|
+
export {
|
|
14
|
+
argMax,
|
|
15
|
+
argMin
|
|
33
16
|
};
|
|
34
|
-
/**
|
|
35
|
-
* Similar to {@link argMin}, but selects index of maximum item. Returns -1 if
|
|
36
|
-
* `items` is empty.
|
|
37
|
-
*
|
|
38
|
-
* @param items
|
|
39
|
-
* @param min
|
|
40
|
-
* @param pred
|
|
41
|
-
*/
|
|
42
|
-
export const argMax = (items, min = -Infinity, pred = (a, b) => a > b) => argMin(items, min, pred);
|
package/binary-search.js
CHANGED
|
@@ -1,187 +1,80 @@
|
|
|
1
1
|
import { compare } from "@thi.ng/compare/compare";
|
|
2
2
|
import { compareNumAsc } from "@thi.ng/compare/numeric";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
* The optional `cmp` comparator (default:
|
|
15
|
-
* [`compare()`](https://docs.thi.ng/umbrella/compare/functions/compare.html))
|
|
16
|
-
* is then used to identify the index of `x`. The sort order of `buf` MUST be
|
|
17
|
-
* compatible with that of `cmp`.
|
|
18
|
-
*
|
|
19
|
-
* @example
|
|
20
|
-
* ```ts
|
|
21
|
-
* binarySearch([2, 4, 6], 5);
|
|
22
|
-
* // -3
|
|
23
|
-
* ```
|
|
24
|
-
*
|
|
25
|
-
* @param buf - array
|
|
26
|
-
* @param x - search value
|
|
27
|
-
* @param key - key function
|
|
28
|
-
* @param cmp - comparator
|
|
29
|
-
* @param low - min index
|
|
30
|
-
* @param high - max index
|
|
31
|
-
*/
|
|
32
|
-
export const binarySearch = (buf, x, key = (x) => x, cmp = compare, low = 0, high = buf.length - 1) => {
|
|
33
|
-
const kx = key(x);
|
|
34
|
-
while (low <= high) {
|
|
35
|
-
const mid = (low + high) >>> 1;
|
|
36
|
-
const c = cmp(key(buf[mid]), kx);
|
|
37
|
-
if (c < 0) {
|
|
38
|
-
low = mid + 1;
|
|
39
|
-
}
|
|
40
|
-
else if (c > 0) {
|
|
41
|
-
high = mid - 1;
|
|
42
|
-
}
|
|
43
|
-
else {
|
|
44
|
-
return mid;
|
|
45
|
-
}
|
|
3
|
+
const binarySearch = (buf, x, key = (x2) => x2, cmp = compare, low = 0, high = buf.length - 1) => {
|
|
4
|
+
const kx = key(x);
|
|
5
|
+
while (low <= high) {
|
|
6
|
+
const mid = low + high >>> 1;
|
|
7
|
+
const c = cmp(key(buf[mid]), kx);
|
|
8
|
+
if (c < 0) {
|
|
9
|
+
low = mid + 1;
|
|
10
|
+
} else if (c > 0) {
|
|
11
|
+
high = mid - 1;
|
|
12
|
+
} else {
|
|
13
|
+
return mid;
|
|
46
14
|
}
|
|
47
|
-
|
|
15
|
+
}
|
|
16
|
+
return -low - 1;
|
|
48
17
|
};
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
*/
|
|
60
|
-
export const binarySearchNumeric = (buf, x, cmp = compareNumAsc, low = 0, high = buf.length - 1) => {
|
|
61
|
-
while (low <= high) {
|
|
62
|
-
const mid = (low + high) >>> 1;
|
|
63
|
-
const c = cmp(buf[mid], x);
|
|
64
|
-
if (c < 0) {
|
|
65
|
-
low = mid + 1;
|
|
66
|
-
}
|
|
67
|
-
else if (c > 0) {
|
|
68
|
-
high = mid - 1;
|
|
69
|
-
}
|
|
70
|
-
else {
|
|
71
|
-
return mid;
|
|
72
|
-
}
|
|
18
|
+
const binarySearchNumeric = (buf, x, cmp = compareNumAsc, low = 0, high = buf.length - 1) => {
|
|
19
|
+
while (low <= high) {
|
|
20
|
+
const mid = low + high >>> 1;
|
|
21
|
+
const c = cmp(buf[mid], x);
|
|
22
|
+
if (c < 0) {
|
|
23
|
+
low = mid + 1;
|
|
24
|
+
} else if (c > 0) {
|
|
25
|
+
high = mid - 1;
|
|
26
|
+
} else {
|
|
27
|
+
return mid;
|
|
73
28
|
}
|
|
74
|
-
|
|
29
|
+
}
|
|
30
|
+
return -low - 1;
|
|
75
31
|
};
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
32
|
+
const binarySearch2 = (buf, x) => {
|
|
33
|
+
let idx = buf[1] <= x ? 1 : 0;
|
|
34
|
+
return buf[idx] === x ? idx : buf[0] < x ? -idx - 2 : -1;
|
|
79
35
|
};
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
* @param buf -
|
|
85
|
-
* @param x -
|
|
86
|
-
*/
|
|
87
|
-
export const binarySearch4 = (buf, x) => {
|
|
88
|
-
let idx = buf[2] <= x ? 2 : 0;
|
|
89
|
-
idx |= buf[idx + 1] <= x ? 1 : 0;
|
|
90
|
-
return buf[idx] === x ? idx : buf[0] < x ? -idx - 2 : -1;
|
|
36
|
+
const binarySearch4 = (buf, x) => {
|
|
37
|
+
let idx = buf[2] <= x ? 2 : 0;
|
|
38
|
+
idx |= buf[idx + 1] <= x ? 1 : 0;
|
|
39
|
+
return buf[idx] === x ? idx : buf[0] < x ? -idx - 2 : -1;
|
|
91
40
|
};
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
* @param x -
|
|
98
|
-
*/
|
|
99
|
-
export const binarySearch8 = (buf, x) => {
|
|
100
|
-
let idx = buf[4] <= x ? 4 : 0;
|
|
101
|
-
idx |= buf[idx + 2] <= x ? 2 : 0;
|
|
102
|
-
idx |= buf[idx + 1] <= x ? 1 : 0;
|
|
103
|
-
return buf[idx] === x ? idx : buf[0] < x ? -idx - 2 : -1;
|
|
41
|
+
const binarySearch8 = (buf, x) => {
|
|
42
|
+
let idx = buf[4] <= x ? 4 : 0;
|
|
43
|
+
idx |= buf[idx + 2] <= x ? 2 : 0;
|
|
44
|
+
idx |= buf[idx + 1] <= x ? 1 : 0;
|
|
45
|
+
return buf[idx] === x ? idx : buf[0] < x ? -idx - 2 : -1;
|
|
104
46
|
};
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
*/
|
|
112
|
-
export const binarySearch16 = (buf, x) => {
|
|
113
|
-
let idx = buf[8] <= x ? 8 : 0;
|
|
114
|
-
idx |= buf[idx + 4] <= x ? 4 : 0;
|
|
115
|
-
idx |= buf[idx + 2] <= x ? 2 : 0;
|
|
116
|
-
idx |= buf[idx + 1] <= x ? 1 : 0;
|
|
117
|
-
return buf[idx] === x ? idx : buf[0] < x ? -idx - 2 : -1;
|
|
47
|
+
const binarySearch16 = (buf, x) => {
|
|
48
|
+
let idx = buf[8] <= x ? 8 : 0;
|
|
49
|
+
idx |= buf[idx + 4] <= x ? 4 : 0;
|
|
50
|
+
idx |= buf[idx + 2] <= x ? 2 : 0;
|
|
51
|
+
idx |= buf[idx + 1] <= x ? 1 : 0;
|
|
52
|
+
return buf[idx] === x ? idx : buf[0] < x ? -idx - 2 : -1;
|
|
118
53
|
};
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
54
|
+
const binarySearch32 = (buf, x) => {
|
|
55
|
+
let idx = buf[16] <= x ? 16 : 0;
|
|
56
|
+
idx |= buf[idx + 4] <= x ? 8 : 0;
|
|
57
|
+
idx |= buf[idx + 4] <= x ? 4 : 0;
|
|
58
|
+
idx |= buf[idx + 2] <= x ? 2 : 0;
|
|
59
|
+
idx |= buf[idx + 1] <= x ? 1 : 0;
|
|
60
|
+
return buf[idx] === x ? idx : buf[0] < x ? -idx - 2 : -1;
|
|
61
|
+
};
|
|
62
|
+
const bsLT = (i) => i < 0 ? -i - 2 : i - 1;
|
|
63
|
+
const bsLE = (i) => i < 0 ? -i - 2 : i;
|
|
64
|
+
const bsGT = (i, n) => (i = i < 0 ? -i - 1 : i + 1, i < n ? i : -1);
|
|
65
|
+
const bsGE = (i, n) => (i = i < 0 ? -i - 1 : i, i < n ? i : -1);
|
|
66
|
+
const bsEQ = (i) => i < 0 ? -1 : i;
|
|
67
|
+
export {
|
|
68
|
+
binarySearch,
|
|
69
|
+
binarySearch16,
|
|
70
|
+
binarySearch2,
|
|
71
|
+
binarySearch32,
|
|
72
|
+
binarySearch4,
|
|
73
|
+
binarySearch8,
|
|
74
|
+
binarySearchNumeric,
|
|
75
|
+
bsEQ,
|
|
76
|
+
bsGE,
|
|
77
|
+
bsGT,
|
|
78
|
+
bsLE,
|
|
79
|
+
bsLT
|
|
133
80
|
};
|
|
134
|
-
/**
|
|
135
|
-
* {@link binarySearch} result index classifier for predecessor queries.
|
|
136
|
-
* Returns index of last item less than search value or -1 if no such
|
|
137
|
-
* values exist.
|
|
138
|
-
*
|
|
139
|
-
* @example
|
|
140
|
-
* ```ts
|
|
141
|
-
* bsLT(binarySearch([10, 20, 30, 40], 25))
|
|
142
|
-
* // 1
|
|
143
|
-
* ```
|
|
144
|
-
*
|
|
145
|
-
* @param i - binarySearch result index
|
|
146
|
-
*/
|
|
147
|
-
export const bsLT = (i) => (i < 0 ? -i - 2 : i - 1);
|
|
148
|
-
/**
|
|
149
|
-
* Similar to {@link bsLT}, but for less-than-equals queries.
|
|
150
|
-
*
|
|
151
|
-
* @param i - binarySearch result index
|
|
152
|
-
*/
|
|
153
|
-
export const bsLE = (i) => (i < 0 ? -i - 2 : i);
|
|
154
|
-
/**
|
|
155
|
-
* {@link binarySearch} result index classifier for successor queries.
|
|
156
|
-
* Returns index of first item greater than search value or -1 if no
|
|
157
|
-
* such values exist.
|
|
158
|
-
*
|
|
159
|
-
* @example
|
|
160
|
-
* ```ts
|
|
161
|
-
* src = [10, 20, 30, 40];
|
|
162
|
-
*
|
|
163
|
-
* bsGT(binarySearch(src, 25), src.length)
|
|
164
|
-
* // 2
|
|
165
|
-
*
|
|
166
|
-
* bsGT(binarySearch(src, 40), src.length)
|
|
167
|
-
* // -1
|
|
168
|
-
* ```
|
|
169
|
-
*
|
|
170
|
-
* @param i - binarySearch result index
|
|
171
|
-
* @param n - array length
|
|
172
|
-
*/
|
|
173
|
-
export const bsGT = (i, n) => ((i = i < 0 ? -i - 1 : i + 1), i < n ? i : -1);
|
|
174
|
-
/**
|
|
175
|
-
* Similar to {@link bsGT}, but for greater-than-equals queries.
|
|
176
|
-
*
|
|
177
|
-
* @param i - binarySearch result index
|
|
178
|
-
* @param n - array length
|
|
179
|
-
*/
|
|
180
|
-
export const bsGE = (i, n) => ((i = i < 0 ? -i - 1 : i), i < n ? i : -1);
|
|
181
|
-
/**
|
|
182
|
-
* {@link binarySearch} result index classifier for equals queries.
|
|
183
|
-
* Merely syntax sugar, casting any non-found result indices to -1.
|
|
184
|
-
*
|
|
185
|
-
* @param i - binarySearch result index
|
|
186
|
-
*/
|
|
187
|
-
export const bsEQ = (i) => (i < 0 ? -1 : i);
|
package/bisect.js
CHANGED
|
@@ -1,23 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* @param src -
|
|
5
|
-
* @param i -
|
|
6
|
-
*/
|
|
7
|
-
export const bisect = (src, i = src.length >>> 1) => [
|
|
8
|
-
src.slice(0, i),
|
|
9
|
-
src.slice(i),
|
|
1
|
+
const bisect = (src, i = src.length >>> 1) => [
|
|
2
|
+
src.slice(0, i),
|
|
3
|
+
src.slice(i)
|
|
10
4
|
];
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
* @param pred -
|
|
19
|
-
*/
|
|
20
|
-
export const bisectWith = (src, pred) => {
|
|
21
|
-
const i = src.findIndex(pred);
|
|
22
|
-
return i >= 0 ? bisect(src, i) : [src, []];
|
|
5
|
+
const bisectWith = (src, pred) => {
|
|
6
|
+
const i = src.findIndex(pred);
|
|
7
|
+
return i >= 0 ? bisect(src, i) : [src, []];
|
|
8
|
+
};
|
|
9
|
+
export {
|
|
10
|
+
bisect,
|
|
11
|
+
bisectWith
|
|
23
12
|
};
|
package/blit.js
CHANGED
|
@@ -1,36 +1,39 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
return dest;
|
|
5
|
-
for (let i = 0; i < sw; i++) {
|
|
6
|
-
const val = src[sx + i];
|
|
7
|
-
val !== mask && (dest[dx + i] = val);
|
|
8
|
-
}
|
|
1
|
+
function blit1d(dest, x, src, mask) {
|
|
2
|
+
const [sx, sw, dx, dw] = __clip(0, src.length, x, dest.length);
|
|
3
|
+
if (sw < 1 || dx >= dw)
|
|
9
4
|
return dest;
|
|
5
|
+
for (let i = 0; i < sw; i++) {
|
|
6
|
+
const val = src[sx + i];
|
|
7
|
+
val !== mask && (dest[dx + i] = val);
|
|
8
|
+
}
|
|
9
|
+
return dest;
|
|
10
10
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
return dest;
|
|
16
|
-
const sstride = ssize[0];
|
|
17
|
-
const dstride = dsize[0];
|
|
18
|
-
for (let y = 0; y < sh; y++) {
|
|
19
|
-
for (let x = 0, soff = (sy + y) * sstride + sx, doff = (dy + y) * dstride + dx; x < sw; x++) {
|
|
20
|
-
const val = src[soff + x];
|
|
21
|
-
val !== mask && (dest[doff + x] = val);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
11
|
+
function blit2d(dest, dpos, dsize, src, ssize, mask) {
|
|
12
|
+
const [sx, sw, dx, dw] = __clip(0, ssize[0], dpos[0], dsize[0]);
|
|
13
|
+
const [sy, sh, dy, dh] = __clip(0, ssize[1], dpos[1], dsize[1]);
|
|
14
|
+
if (sw < 1 || sh < 1 || dx >= dw || dy >= dh)
|
|
24
15
|
return dest;
|
|
16
|
+
const sstride = ssize[0];
|
|
17
|
+
const dstride = dsize[0];
|
|
18
|
+
for (let y = 0; y < sh; y++) {
|
|
19
|
+
for (let x = 0, soff = (sy + y) * sstride + sx, doff = (dy + y) * dstride + dx; x < sw; x++) {
|
|
20
|
+
const val = src[soff + x];
|
|
21
|
+
val !== mask && (dest[doff + x] = val);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return dest;
|
|
25
25
|
}
|
|
26
26
|
const __clip = (sx, sw, dx, dw) => {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
27
|
+
if (dx < 0) {
|
|
28
|
+
sx -= dx;
|
|
29
|
+
sw += dx;
|
|
30
|
+
dx = 0;
|
|
31
|
+
} else if (dx + sw > dw) {
|
|
32
|
+
sw = dw - dx;
|
|
33
|
+
}
|
|
34
|
+
return [sx, sw, dx, dw];
|
|
35
|
+
};
|
|
36
|
+
export {
|
|
37
|
+
blit1d,
|
|
38
|
+
blit2d
|
|
36
39
|
};
|
package/ends-with.js
CHANGED
|
@@ -1,26 +1,13 @@
|
|
|
1
1
|
import { equiv as _eq } from "@thi.ng/equiv";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
* {@link startsWith}
|
|
14
|
-
*
|
|
15
|
-
* @param buf - array
|
|
16
|
-
* @param needle - search values (array)
|
|
17
|
-
* @param equiv - equivalence predicate
|
|
18
|
-
*/
|
|
19
|
-
export const endsWith = (buf, needle, equiv = _eq) => {
|
|
20
|
-
let i = buf.length;
|
|
21
|
-
let j = needle.length;
|
|
22
|
-
if (i < j)
|
|
23
|
-
return false;
|
|
24
|
-
while ((--i, j-- > 0 && equiv(buf[i], needle[j]))) { }
|
|
25
|
-
return j < 0;
|
|
2
|
+
const endsWith = (buf, needle, equiv = _eq) => {
|
|
3
|
+
let i = buf.length;
|
|
4
|
+
let j = needle.length;
|
|
5
|
+
if (i < j)
|
|
6
|
+
return false;
|
|
7
|
+
while (--i, j-- > 0 && equiv(buf[i], needle[j])) {
|
|
8
|
+
}
|
|
9
|
+
return j < 0;
|
|
10
|
+
};
|
|
11
|
+
export {
|
|
12
|
+
endsWith
|
|
26
13
|
};
|
package/ensure-array.js
CHANGED
|
@@ -1,23 +1,9 @@
|
|
|
1
1
|
import { isArray } from "@thi.ng/checks/is-array";
|
|
2
2
|
import { isArrayLike } from "@thi.ng/checks/is-arraylike";
|
|
3
3
|
import { ensureIterable } from "./ensure-iterable.js";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
* to obtain an iterator from `x` and if successful collects it as array
|
|
11
|
-
* and returns it. Throws error if `x` isn't iterable.
|
|
12
|
-
*
|
|
13
|
-
* @param x -
|
|
14
|
-
*/
|
|
15
|
-
export const ensureArray = (x) => isArray(x) ? x : [...ensureIterable(x)];
|
|
16
|
-
/**
|
|
17
|
-
* Similar to {@link ensureArray}, but for `ArrayLike` types.
|
|
18
|
-
*
|
|
19
|
-
* {@link ensureArray}
|
|
20
|
-
*
|
|
21
|
-
* @param x -
|
|
22
|
-
*/
|
|
23
|
-
export const ensureArrayLike = (x) => isArrayLike(x) ? x : [...ensureIterable(x)];
|
|
4
|
+
const ensureArray = (x) => isArray(x) ? x : [...ensureIterable(x)];
|
|
5
|
+
const ensureArrayLike = (x) => isArrayLike(x) ? x : [...ensureIterable(x)];
|
|
6
|
+
export {
|
|
7
|
+
ensureArray,
|
|
8
|
+
ensureArrayLike
|
|
9
|
+
};
|
package/ensure-iterable.js
CHANGED
|
@@ -1,12 +1,8 @@
|
|
|
1
1
|
import { illegalArgs } from "@thi.ng/errors/illegal-arguments";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export const ensureIterable = (x) => {
|
|
9
|
-
(x == null || !x[Symbol.iterator]) &&
|
|
10
|
-
illegalArgs(`value is not iterable: ${x}`);
|
|
11
|
-
return x;
|
|
2
|
+
const ensureIterable = (x) => {
|
|
3
|
+
(x == null || !x[Symbol.iterator]) && illegalArgs(`value is not iterable: ${x}`);
|
|
4
|
+
return x;
|
|
5
|
+
};
|
|
6
|
+
export {
|
|
7
|
+
ensureIterable
|
|
12
8
|
};
|
package/fill-range.js
CHANGED
|
@@ -1,34 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
*
|
|
14
|
-
* fillRange(fillRange([], 0, 10, 20, 2), 5, 20, 8, -2)
|
|
15
|
-
* // [ 10, 12, 14, 16, 18, 20, 18, 16, 14, 12, 10 ]
|
|
16
|
-
* ```
|
|
17
|
-
*
|
|
18
|
-
* @param buf -
|
|
19
|
-
* @param index -
|
|
20
|
-
* @param start -
|
|
21
|
-
* @param end -
|
|
22
|
-
* @param step -
|
|
23
|
-
*/
|
|
24
|
-
export const fillRange = (buf, index = 0, start = 0, end = buf.length, step = end > start ? 1 : -1) => {
|
|
25
|
-
if (step > 0) {
|
|
26
|
-
for (; start < end; start += step)
|
|
27
|
-
buf[index++] = start;
|
|
28
|
-
}
|
|
29
|
-
else {
|
|
30
|
-
for (; start > end; start += step)
|
|
31
|
-
buf[index++] = start;
|
|
32
|
-
}
|
|
33
|
-
return buf;
|
|
1
|
+
const fillRange = (buf, index = 0, start = 0, end = buf.length, step = end > start ? 1 : -1) => {
|
|
2
|
+
if (step > 0) {
|
|
3
|
+
for (; start < end; start += step)
|
|
4
|
+
buf[index++] = start;
|
|
5
|
+
} else {
|
|
6
|
+
for (; start > end; start += step)
|
|
7
|
+
buf[index++] = start;
|
|
8
|
+
}
|
|
9
|
+
return buf;
|
|
10
|
+
};
|
|
11
|
+
export {
|
|
12
|
+
fillRange
|
|
34
13
|
};
|
package/find.js
CHANGED
|
@@ -1,29 +1,16 @@
|
|
|
1
1
|
import { equiv as _equiv } from "@thi.ng/equiv";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
*
|
|
6
|
-
* @param buf - array
|
|
7
|
-
* @param x - search value
|
|
8
|
-
* @param equiv - equivalence predicate
|
|
9
|
-
*/
|
|
10
|
-
export const find = (buf, x, equiv = _equiv) => {
|
|
11
|
-
const i = findIndex(buf, x, equiv);
|
|
12
|
-
return i !== -1 ? buf[i] : undefined;
|
|
2
|
+
const find = (buf, x, equiv = _equiv) => {
|
|
3
|
+
const i = findIndex(buf, x, equiv);
|
|
4
|
+
return i !== -1 ? buf[i] : void 0;
|
|
13
5
|
};
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
for (let i = buf.length; i-- > 0;) {
|
|
25
|
-
if (equiv(x, buf[i]))
|
|
26
|
-
return i;
|
|
27
|
-
}
|
|
28
|
-
return -1;
|
|
6
|
+
const findIndex = (buf, x, equiv = _equiv) => {
|
|
7
|
+
for (let i = buf.length; i-- > 0; ) {
|
|
8
|
+
if (equiv(x, buf[i]))
|
|
9
|
+
return i;
|
|
10
|
+
}
|
|
11
|
+
return -1;
|
|
12
|
+
};
|
|
13
|
+
export {
|
|
14
|
+
find,
|
|
15
|
+
findIndex
|
|
29
16
|
};
|