@thi.ng/arrays 2.10.22 → 2.11.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-04-01T21:42:04Z
3
+ - **Last updated**: 2025-04-30T12:52:32Z
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,16 @@ 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.11.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/arrays@2.11.0) (2025-04-30)
15
+
16
+ #### 🚀 Features
17
+
18
+ - add `permutation()` ([7dcc389](https://github.com/thi-ng/umbrella/commit/7dcc389))
19
+
20
+ #### ♻️ Refactoring
21
+
22
+ - minor update arg types ([52900ef](https://github.com/thi-ng/umbrella/commit/52900ef))
23
+
14
24
  ### [2.10.11](https://github.com/thi-ng/umbrella/tree/@thi.ng/arrays@2.10.11) (2025-01-17)
15
25
 
16
26
  #### 🩹 Bug fixes
package/README.md CHANGED
@@ -7,7 +7,7 @@
7
7
  [![Mastodon Follow](https://img.shields.io/mastodon/follow/109331703950160316?domain=https%3A%2F%2Fmastodon.thi.ng&style=social)](https://mastodon.thi.ng/@toxi)
8
8
 
9
9
  > [!NOTE]
10
- > This is one of 205 standalone projects, maintained as part
10
+ > This is one of 206 standalone projects, maintained as part
11
11
  > of the [@thi.ng/umbrella](https://github.com/thi-ng/umbrella/) monorepo
12
12
  > and anti-framework.
13
13
  >
@@ -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.00 KB
63
+ Package sizes (brotli'd, pre-treeshake): ESM: 3.05 KB
64
64
 
65
65
  ## Dependencies
66
66
 
@@ -112,6 +112,7 @@ directory are using this package:
112
112
  - [`levenshtein()`](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/levenshtein.ts)
113
113
  - [`multiSwap()`](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/swap.ts)
114
114
  - [`peek()`](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/peek.ts)
115
+ - [`permutation()`](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/permutation.ts)
115
116
  - [`quickSort()`](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/quicksort.ts)
116
117
  - [`rotate()` / `rotateTyped()`](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/rotate.ts)
117
118
  - [`selectThresholdMin()` / `selectThresholdMax()`](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/threshold.ts)
package/index.d.ts CHANGED
@@ -18,6 +18,7 @@ export * from "./into.js";
18
18
  export * from "./iterator.js";
19
19
  export * from "./levenshtein.js";
20
20
  export * from "./peek.js";
21
+ export * from "./permutation.js";
21
22
  export * from "./quicksort.js";
22
23
  export * from "./rotate.js";
23
24
  export * from "./shuffle.js";
package/index.js CHANGED
@@ -18,6 +18,7 @@ export * from "./into.js";
18
18
  export * from "./iterator.js";
19
19
  export * from "./levenshtein.js";
20
20
  export * from "./peek.js";
21
+ export * from "./permutation.js";
21
22
  export * from "./quicksort.js";
22
23
  export * from "./rotate.js";
23
24
  export * from "./shuffle.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/arrays",
3
- "version": "2.10.22",
3
+ "version": "2.11.0",
4
4
  "description": "Array / Arraylike utilities",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -39,17 +39,17 @@
39
39
  "tool:tangle": "../../node_modules/.bin/tangle src/**/*.ts"
40
40
  },
41
41
  "dependencies": {
42
- "@thi.ng/api": "^8.11.25",
43
- "@thi.ng/checks": "^3.7.5",
44
- "@thi.ng/compare": "^2.4.17",
45
- "@thi.ng/equiv": "^2.1.81",
46
- "@thi.ng/errors": "^2.5.31",
47
- "@thi.ng/random": "^4.1.16"
42
+ "@thi.ng/api": "^8.11.27",
43
+ "@thi.ng/checks": "^3.7.7",
44
+ "@thi.ng/compare": "^2.4.19",
45
+ "@thi.ng/equiv": "^2.1.83",
46
+ "@thi.ng/errors": "^2.5.33",
47
+ "@thi.ng/random": "^4.1.18"
48
48
  },
49
49
  "devDependencies": {
50
- "esbuild": "^0.25.2",
51
- "typedoc": "^0.28.1",
52
- "typescript": "^5.8.2"
50
+ "esbuild": "^0.25.3",
51
+ "typedoc": "^0.28.3",
52
+ "typescript": "^5.8.3"
53
53
  },
54
54
  "keywords": [
55
55
  "aos",
@@ -139,6 +139,9 @@
139
139
  "./peek": {
140
140
  "default": "./peek.js"
141
141
  },
142
+ "./permutation": {
143
+ "default": "./permutation.js"
144
+ },
142
145
  "./quicksort": {
143
146
  "default": "./quicksort.js"
144
147
  },
@@ -171,5 +174,5 @@
171
174
  "tag": "array",
172
175
  "year": 2018
173
176
  },
174
- "gitHead": "87aa2d0e64a357476c10fd57aabdfded13c79f7d\n"
177
+ "gitHead": "4354686a6fb1f82c09ea48f92f87786191b231a0\n"
175
178
  }
@@ -0,0 +1,66 @@
1
+ import type { NumericArray } from "@thi.ng/api";
2
+ import type { IRandom } from "@thi.ng/random";
3
+ /**
4
+ * Creates a new array of `n` numbers with a random permutation of integers, in
5
+ * the semi-open [0,n) range (optionally using provided `rnd` instance).
6
+ *
7
+ * @remarks
8
+ * This function is syntax sugar, combining {@link fillRange} and
9
+ * {@link shuffle}.
10
+ *
11
+ * @example
12
+ * ```ts tangle:../export/permutation.ts
13
+ * import { permutation } from "@thi.ng/arrays";
14
+ *
15
+ * console.log(permuation(8));
16
+ * // [ 6, 2, 7, 5, 4, 0, 3, 1 ]
17
+ * ```
18
+ *
19
+ * @param n
20
+ * @param rnd
21
+ */
22
+ export declare function permutation(n: number, rnd?: IRandom): number[];
23
+ /**
24
+ * Fill the given array with a random permutation of integers, in the semi-open
25
+ * [0,n) range, where `n` is the length of the array. Optionally uses provided
26
+ * `rnd` instance.
27
+ *
28
+ * @remarks
29
+ * This function is syntax sugar, combining {@link fillRange} and
30
+ * {@link shuffle}.
31
+ *
32
+ * For typed integer arrays, the length of the array must be compatible with its
33
+ * value range, otherwise duplicates will appear (due to value overflow or
34
+ * clamping). E.g. a `Uint8Array` can only have a max length of 256 items if
35
+ * unique values are required. A `Int8Array` can only have a max length of 128
36
+ * values, otherwise negative numbers will appear in the result.
37
+ *
38
+ * @example
39
+ * ```ts tangle:../export/permutation-uint8.ts
40
+ * import { permutation } from "@thi.ng/arrays";
41
+ *
42
+ * const buf = new Uint8Array(16);
43
+ *
44
+ * // permute entire buffer
45
+ * console.log(permutation(buf));
46
+ * // Uint8Array(16) [ 6, 3, 2, 8, 10, 1, 15, 11, 9, 5, 0, 13, 7, 4, 14, 12 ]
47
+ *
48
+ * // apply to only a sub-ranges of the buffer
49
+ * for (let i = 0; i < buf.length; i += 4) {
50
+ * console.log(permutation(buf.subarray(i, i + 4)));
51
+ * }
52
+ * // Uint8Array(4) [ 1, 3, 2, 0 ]
53
+ * // Uint8Array(4) [ 2, 0, 3, 1 ]
54
+ * // Uint8Array(4) [ 1, 3, 2, 0 ]
55
+ * // Uint8Array(4) [ 3, 2, 1, 0 ]
56
+ *
57
+ * // show full buffer
58
+ * console.log(buf);
59
+ * // Uint8Array(16) [ 1, 3, 2, 0, 2, 0, 3, 1, 1, 3, 2, 0, 3, 2, 1, 0 ]
60
+ * ```
61
+ *
62
+ * @param n
63
+ * @param rnd
64
+ */
65
+ export declare function permutation<T extends NumericArray>(buf: T, rnd?: IRandom): T;
66
+ //# sourceMappingURL=permutation.d.ts.map
package/permutation.js ADDED
@@ -0,0 +1,13 @@
1
+ import { isNumber } from "@thi.ng/checks/is-number";
2
+ import { fillRange } from "./fill-range.js";
3
+ import { shuffle } from "./shuffle.js";
4
+ function permutation(buf, rnd) {
5
+ return shuffle(
6
+ fillRange(isNumber(buf) ? new Array(buf) : buf),
7
+ void 0,
8
+ rnd
9
+ );
10
+ }
11
+ export {
12
+ permutation
13
+ };
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: Array<T>, num: number) => T[];
14
+ export declare const rotate: <T>(buf: T[], num: number) => T[];
15
15
  /**
16
16
  * Same as {@link rotate}, but for optimized for typed arrays!
17
17
  *
package/swizzle.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Fn } from "@thi.ng/api";
1
+ import type { Fn, NumericArray } from "@thi.ng/api";
2
2
  /**
3
3
  * Returns optimized function to immutably select, repeat, reshape and /
4
4
  * or reorder array / object values in the specified index order.
@@ -42,5 +42,5 @@ import type { Fn } from "@thi.ng/api";
42
42
  *
43
43
  * @param order - indices
44
44
  */
45
- export declare const swizzle: <T>(order: string | PropertyKey[]) => Fn<T, any[]>;
45
+ export declare const swizzle: <T>(order: string | PropertyKey[] | NumericArray) => Fn<T, any[]>;
46
46
  //# sourceMappingURL=swizzle.d.ts.map