@thi.ng/arrays 2.7.7 → 2.7.9

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2023-12-09T19:12:03Z
3
+ - **Last updated**: 2023-12-18T13:41:19Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
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
- * Returns a new array of numeric indices in same order as sorted values of
6
- * `src` using given (optional) comparator (default: thi.ng/compare). The `src`
7
- * array will remain unmodified.
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
- * Takes an array of numbers and returns the index of the item which is
3
- * considered the minimum value according to the given initial `min` value
4
- * (default: ∞) and predicate (both optional). Returns -1 if `items` is empty.
5
- *
6
- * @remarks
7
- * See {@link argMax}.
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
- return id;
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
- * Returns the supposed index of `x` in pre-sorted array-like collection `buf`.
5
- *
6
- * @remarks
7
- * If `x` can't be found, returns `-index-1`, representing the negative of the
8
- * index, were `x` to be inserted into `buf`. E.g if the return value is -3, `x`
9
- * would appear/insert at index 2.
10
- *
11
- * The optional `key` function is used to obtain the actual sort value of `x`
12
- * and each array item (default: identity).
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
- return -low - 1;
15
+ }
16
+ return -low - 1;
48
17
  };
49
- /**
50
- * Similar to {@link binarySearch}, but optimized for numeric arrays and
51
- * supporting custom comparators (default:
52
- * [`compareNumAsc()`](https://docs.thi.ng/umbrella/compare/functions/compareNumAsc.html)).
53
- *
54
- * @param buf - array
55
- * @param x - search value
56
- * @param cmp - comparator
57
- * @param low - min index
58
- * @param high - max index
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
- return -low - 1;
29
+ }
30
+ return -low - 1;
75
31
  };
76
- export const binarySearch2 = (buf, x) => {
77
- let idx = buf[1] <= x ? 1 : 0;
78
- return buf[idx] === x ? idx : buf[0] < x ? -idx - 2 : -1;
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
- * Non-recursive, optimized binary search for fixed size numeric arrays of 4
82
- * values. Returns index of `x` or `-index-1` if not found.
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
- * Non-recursive, optimized binary search for fixed size numeric arrays of 8
94
- * values. Returns index of `x` or `-index-1` if not found.
95
- *
96
- * @param buf -
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
- * Non-recursive, optimized binary search for fixed size numeric arrays of 16
107
- * values. Returns index of `x` or `-index-1` if not found.
108
- *
109
- * @param buf -
110
- * @param x -
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
- * Non-recursive, optimized binary search for fixed size numeric arrays of 32
121
- * values. Returns index of `x` or `-index-1` if not found.
122
- *
123
- * @param buf -
124
- * @param x -
125
- */
126
- export const binarySearch32 = (buf, x) => {
127
- let idx = buf[16] <= x ? 16 : 0;
128
- idx |= buf[idx + 4] <= x ? 8 : 0;
129
- idx |= buf[idx + 4] <= x ? 4 : 0;
130
- idx |= buf[idx + 2] <= x ? 2 : 0;
131
- idx |= buf[idx + 1] <= x ? 1 : 0;
132
- return buf[idx] === x ? idx : buf[0] < x ? -idx - 2 : -1;
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
- * Splits array at given index (default: floor(src.length/2)) and returns tuple of [lhs, rhs].
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
- * Similar to {@link bisect}, but first finds split index via provided
13
- * predicate. The item for which the predicate first returns a truthy result,
14
- * will be the first item in the RHS array. If the predicate never succeeds, the
15
- * function returns `[src, []]`, i.e. all items will remain in the LHS.
16
- *
17
- * @param src -
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
- export 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)
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
- export 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)
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
- if (dx < 0) {
28
- sx -= dx;
29
- sw += dx;
30
- dx = 0;
31
- }
32
- else if (dx + sw > dw) {
33
- sw = dw - dx;
34
- }
35
- return [sx, sw, dx, dw];
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
- * Returns true if the last items of `buf` are the same items as in `needle`.
4
- *
5
- * @remarks
6
- * This means `buf` should have at least the same length as `needle` for this to
7
- * be true.
8
- *
9
- * By default, uses
10
- * [`equiv()`](https://docs.thi.ng/umbrella/equiv/functions/equiv.html) for
11
- * equality checking.
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
- * Helper function to avoid unnecessary copying if `x` is already an
6
- * array.
7
- *
8
- * @remarks
9
- * First checks if `x` is an array and if so returns it. Else attempts
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
+ };
@@ -1,12 +1,8 @@
1
1
  import { illegalArgs } from "@thi.ng/errors/illegal-arguments";
2
- /**
3
- * Attempts to obtain an iterator from `x` and throws error if `x` is
4
- * not iterable.
5
- *
6
- * @param x -
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
- * Fills given array with values in [start .. end) interval from `index`
3
- * and with optional `step` size.
4
- *
5
- * @remarks
6
- * `start` and `end` default to 0 and array length, `step` defaults to 1
7
- * or -1 (depending on range). Returns array.
8
- *
9
- * @example
10
- * ```ts
11
- * fillRange(new Array(5))
12
- * // [ 0, 1, 2, 3, 4 ]
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
- * Similar to `Array.find()`, but uses [`equiv()`](https://docs.thi.ng/umbrella/equiv/functions/equiv.html) as
4
- * default predicate.
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
- * Similar to `Array.findIndex()`, but uses
16
- * [`equiv()`](https://docs.thi.ng/umbrella/equiv/functions/equiv.html) as
17
- * default predicate.
18
- *
19
- * @param buf - array
20
- * @param x - search value
21
- * @param equiv - equivalence predicate
22
- */
23
- export const findIndex = (buf, x, equiv = _equiv) => {
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
  };