@thi.ng/arrays 2.11.2 → 2.13.0

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**: 2025-06-09T17:24:08Z
3
+ - **Last updated**: 2025-06-18T12:01:21Z
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.
@@ -11,6 +11,23 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
11
11
  **Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
12
12
  and/or version bumps of transitive dependencies.
13
13
 
14
+ ## [2.13.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/arrays@2.13.0) (2025-06-18)
15
+
16
+ #### 🚀 Features
17
+
18
+ - add filterAll() ([91791ba](https://github.com/thi-ng/umbrella/commit/91791ba))
19
+ - add docs & code example
20
+
21
+ ## [2.12.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/arrays@2.12.0) (2025-06-14)
22
+
23
+ #### 🚀 Features
24
+
25
+ - add lookup() / lookupUnsafe() fns ([deb2b37](https://github.com/thi-ng/umbrella/commit/deb2b37))
26
+
27
+ #### ♻️ Refactoring
28
+
29
+ - minor update swizzle() ([84d3aba](https://github.com/thi-ng/umbrella/commit/84d3aba))
30
+
14
31
  ## [2.11.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/arrays@2.11.0) (2025-04-30)
15
32
 
16
33
  #### 🚀 Features
package/README.md CHANGED
@@ -60,7 +60,7 @@ For Node.js REPL:
60
60
  const arr = await import("@thi.ng/arrays");
61
61
  ```
62
62
 
63
- Package sizes (brotli'd, pre-treeshake): ESM: 3.05 KB
63
+ Package sizes (brotli'd, pre-treeshake): ESM: 3.17 KB
64
64
 
65
65
  ## Dependencies
66
66
 
@@ -0,0 +1,36 @@
1
+ import type { Predicate } from "@thi.ng/api";
2
+ /**
3
+ * Similar to JS native `filter()`, but for filtering multiple arrays at once.
4
+ * All arrays are assumed to have the same length as the first. Applies given
5
+ * predicate `pred` to first array ONLY. If predicate is truthy for the current
6
+ * index, the values for that index in all arrays will be copied to their
7
+ * filtered results. Function returns result arrays as tuple.
8
+ *
9
+ * @example
10
+ * ```ts tangle:../export/filter-all.ts
11
+ * import { filterAll } from "@thi.ng/arrays";
12
+ *
13
+ * const [a, b, c] = filterAll(
14
+ * (x) => x==="a",
15
+ * // the predicate is applied to this array
16
+ * ["a", "b", "a"],
17
+ * // any number of additional arrays...
18
+ * [1, 2, 3],
19
+ * [{ id: 123 }, { id: 456 }, { id: 789 }]
20
+ * );
21
+ *
22
+ * console.log("a", a);
23
+ * // [ "a", "a" ]
24
+ *
25
+ * console.log("b", b);
26
+ * // [ 1, 3 ]
27
+ *
28
+ * console.log("c", c);
29
+ * // [{ id: 123 }, { id: 789 }]
30
+ * ```
31
+ *
32
+ * @param pred
33
+ * @param xs
34
+ */
35
+ export declare const filterAll: <A, Xs extends [A[], ...any[][]]>(pred: Predicate<A>, ...xs: Xs) => Xs;
36
+ //# sourceMappingURL=filter-all.d.ts.map
package/filter-all.js ADDED
@@ -0,0 +1,13 @@
1
+ const filterAll = (pred, ...xs) => {
2
+ const res = new Array(xs.length + 1).fill(0).map(() => []);
3
+ const a = xs[0];
4
+ for (let i = 0, n = a.length, m = xs.length; i < n; i++) {
5
+ if (pred(a[i])) {
6
+ for (let j = 0; j < m; j++) res[j].push(xs[j][i]);
7
+ }
8
+ }
9
+ return res;
10
+ };
11
+ export {
12
+ filterAll
13
+ };
package/index.d.ts CHANGED
@@ -8,6 +8,7 @@ export * from "./ends-with.js";
8
8
  export * from "./ensure-array.js";
9
9
  export * from "./ensure-iterable.js";
10
10
  export * from "./fill-range.js";
11
+ export * from "./filter-all.js";
11
12
  export * from "./find.js";
12
13
  export * from "./find-sequence.js";
13
14
  export * from "./floyd-rivest.js";
@@ -17,6 +18,7 @@ export * from "./insert.js";
17
18
  export * from "./into.js";
18
19
  export * from "./iterator.js";
19
20
  export * from "./levenshtein.js";
21
+ export * from "./lookup.js";
20
22
  export * from "./peek.js";
21
23
  export * from "./permutation.js";
22
24
  export * from "./quicksort.js";
package/index.js CHANGED
@@ -8,6 +8,7 @@ export * from "./ends-with.js";
8
8
  export * from "./ensure-array.js";
9
9
  export * from "./ensure-iterable.js";
10
10
  export * from "./fill-range.js";
11
+ export * from "./filter-all.js";
11
12
  export * from "./find.js";
12
13
  export * from "./find-sequence.js";
13
14
  export * from "./floyd-rivest.js";
@@ -17,6 +18,7 @@ export * from "./insert.js";
17
18
  export * from "./into.js";
18
19
  export * from "./iterator.js";
19
20
  export * from "./levenshtein.js";
21
+ export * from "./lookup.js";
20
22
  export * from "./peek.js";
21
23
  export * from "./permutation.js";
22
24
  export * from "./quicksort.js";
package/lookup.d.ts ADDED
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Similar to {@link swizzle}, but immediate and bounds-checked. Takes an array
3
+ * of `table` values and an array of indices `ids`. Returns an array of
4
+ * looked-up values for given IDs, ensuring each index is valid (otherwise
5
+ * throws an error).
6
+ *
7
+ * @example
8
+ * ```ts tangle:../export/lookup.ts
9
+ * import { lookup } from "@thi.ng/arrays";
10
+ *
11
+ * console.log(lookup([10,20,30], [2,2,0,1,1]));
12
+ * // [30, 30, 10, 20, 20]
13
+ *
14
+ * console.log(lookup([10,20,30], [3]));
15
+ * // error: index out of bounds: 3
16
+ * ```
17
+ *
18
+ * @param table
19
+ * @param ids
20
+ */
21
+ export declare const lookup: <T>(table: ArrayLike<T>, ids: number[]) => T[];
22
+ /**
23
+ * Non-bounds-checked version of {@link lookup}.
24
+ *
25
+ * @param table
26
+ * @param ids
27
+ */
28
+ export declare const lookupUnsafe: <T>(table: ArrayLike<T>, ids: number[]) => T[];
29
+ //# sourceMappingURL=lookup.d.ts.map
package/lookup.js ADDED
@@ -0,0 +1,7 @@
1
+ import { ensureIndex } from "@thi.ng/errors/out-of-bounds";
2
+ const lookup = (table, ids) => ids.map((i) => (ensureIndex(i, 0, table.length), table[i]));
3
+ const lookupUnsafe = (table, ids) => ids.map((i) => table[i]);
4
+ export {
5
+ lookup,
6
+ lookupUnsafe
7
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/arrays",
3
- "version": "2.11.2",
3
+ "version": "2.13.0",
4
4
  "description": "Array / Arraylike utilities",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -56,11 +56,18 @@
56
56
  "array",
57
57
  "binary",
58
58
  "distance",
59
+ "fill",
60
+ "floyd-rivest",
59
61
  "fuzzy",
60
62
  "levenshtein",
63
+ "lookup",
64
+ "permutation",
65
+ "range",
61
66
  "search",
67
+ "sequence",
62
68
  "shuffle",
63
69
  "sort",
70
+ "swap",
64
71
  "swizzle",
65
72
  "topology",
66
73
  "typescript"
@@ -109,6 +116,9 @@
109
116
  "./fill-range": {
110
117
  "default": "./fill-range.js"
111
118
  },
119
+ "./filter-all": {
120
+ "default": "./filter-all.js"
121
+ },
112
122
  "./find-sequence": {
113
123
  "default": "./find-sequence.js"
114
124
  },
@@ -136,6 +146,9 @@
136
146
  "./levenshtein": {
137
147
  "default": "./levenshtein.js"
138
148
  },
149
+ "./lookup": {
150
+ "default": "./lookup.js"
151
+ },
139
152
  "./peek": {
140
153
  "default": "./peek.js"
141
154
  },
@@ -174,5 +187,5 @@
174
187
  "tag": "array",
175
188
  "year": 2018
176
189
  },
177
- "gitHead": "93cdcd8db4d4669561a7f0ebc47697bdbfd04214\n"
190
+ "gitHead": "b076434a497b291ad33e81b1a15f6a71e2c82cc2\n"
178
191
  }
package/swizzle.js CHANGED
@@ -21,10 +21,9 @@ const swizzle = (order) => {
21
21
  return (x) => [x[a], x[b], x[c], x[d], x[e], x[f], x[g], x[h]];
22
22
  default:
23
23
  return (x) => {
24
- const res = [];
25
- for (let i = order.length; i-- > 0; ) {
26
- res[i] = x[order[i]];
27
- }
24
+ let n = order.length;
25
+ const res = new Array(n);
26
+ for (; n-- > 0; ) res[n] = x[order[n]];
28
27
  return res;
29
28
  };
30
29
  }