@thi.ng/arrays 2.5.23 → 2.6.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**: 2023-08-27T11:20:58Z
3
+ - **Last updated**: 2023-10-05T11:44:15Z
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.
@@ -9,6 +9,13 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
9
9
  **Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
10
10
  and/or version bumps of transitive dependencies.
11
11
 
12
+ ## [2.6.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/arrays@2.6.0) (2023-10-05)
13
+
14
+ #### 🚀 Features
15
+
16
+ - add argMin()/argMax() ([33512ec](https://github.com/thi-ng/umbrella/commit/33512ec))
17
+ - add selectThresholdMin/Max() fns ([de9ba50](https://github.com/thi-ng/umbrella/commit/de9ba50))
18
+
12
19
  ### [2.5.15](https://github.com/thi-ng/umbrella/tree/@thi.ng/arrays@2.5.15) (2023-08-04)
13
20
 
14
21
  #### ♻️ Refactoring
package/README.md CHANGED
@@ -50,7 +50,7 @@ For Node.js REPL:
50
50
  const arrays = await import("@thi.ng/arrays");
51
51
  ```
52
52
 
53
- Package sizes (brotli'd, pre-treeshake): ESM: 2.61 KB
53
+ Package sizes (brotli'd, pre-treeshake): ESM: 2.75 KB
54
54
 
55
55
  ## Dependencies
56
56
 
@@ -69,9 +69,10 @@ directory are using this package.
69
69
 
70
70
  A selection:
71
71
 
72
- | Screenshot | Description | Live demo | Source |
73
- |:------------------------------------------------------------------------------------------------------------------|:---------------------------------|:-------------------------------------------------|:------------------------------------------------------------------------------|
74
- | <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/kmeans-viz.jpg" width="240"/> | k-means clustering visualization | [Demo](https://demo.thi.ng/umbrella/kmeans-viz/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/kmeans-viz) |
72
+ | Screenshot | Description | Live demo | Source |
73
+ |:----------------------------------------------------------------------------------------------------------------------|:-------------------------------------------------------|:-----------------------------------------------------|:----------------------------------------------------------------------------------|
74
+ | <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/kmeans-viz.jpg" width="240"/> | k-means clustering visualization | [Demo](https://demo.thi.ng/umbrella/kmeans-viz/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/kmeans-viz) |
75
+ | <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/stacked-layout.png" width="240"/> | Responsive & reactively computed stacked column layout | [Demo](https://demo.thi.ng/umbrella/stacked-layout/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/stacked-layout) |
75
76
 
76
77
  ## API
77
78
 
package/argmin.d.ts ADDED
@@ -0,0 +1,34 @@
1
+ import type { Predicate2 } from "@thi.ng/api";
2
+ /**
3
+ * Takes an array of numbers and returns the index of the item which is
4
+ * considered the minimum value according to the given initial `min` value
5
+ * (default: ∞) and predicate (both optional). Returns -1 if `items` is empty.
6
+ *
7
+ * @remarks
8
+ * See {@link argMax}.
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * argMin([42, 11, 66, 23])
13
+ * // 1
14
+ *
15
+ * // same as argmax() with defaults
16
+ * argMin([42, 11, 66, 23], -Infinity, (a, b) => a > b)
17
+ * // 2
18
+ * ```
19
+ *
20
+ * @param buf
21
+ * @param min
22
+ * @param pred
23
+ */
24
+ export declare const argMin: (buf: ArrayLike<number>, min?: number, pred?: Predicate2<number>) => number;
25
+ /**
26
+ * Similar to {@link argMin}, but selects index of maximum item. Returns -1 if
27
+ * `items` is empty.
28
+ *
29
+ * @param items
30
+ * @param min
31
+ * @param pred
32
+ */
33
+ export declare const argMax: (items: ArrayLike<number>, min?: number, pred?: Predicate2<number>) => number;
34
+ //# sourceMappingURL=argmin.d.ts.map
package/argmin.js ADDED
@@ -0,0 +1,42 @@
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
+ }
31
+ }
32
+ return id;
33
+ };
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/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from "./api.js";
2
+ export * from "./argmin.js";
2
3
  export * from "./arg-sort.js";
3
4
  export * from "./binary-search.js";
4
5
  export * from "./bisect.js";
@@ -22,5 +23,6 @@ export * from "./sort-cached.js";
22
23
  export * from "./starts-with.js";
23
24
  export * from "./swap.js";
24
25
  export * from "./swizzle.js";
26
+ export * from "./threshold.js";
25
27
  export * from "./topo-sort.js";
26
28
  //# sourceMappingURL=index.d.ts.map
package/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from "./api.js";
2
+ export * from "./argmin.js";
2
3
  export * from "./arg-sort.js";
3
4
  export * from "./binary-search.js";
4
5
  export * from "./bisect.js";
@@ -22,4 +23,5 @@ export * from "./sort-cached.js";
22
23
  export * from "./starts-with.js";
23
24
  export * from "./swap.js";
24
25
  export * from "./swizzle.js";
26
+ export * from "./threshold.js";
25
27
  export * from "./topo-sort.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/arrays",
3
- "version": "2.5.23",
3
+ "version": "2.6.0",
4
4
  "description": "Array / Arraylike utilities",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -39,7 +39,7 @@
39
39
  "@thi.ng/compare": "^2.2.0",
40
40
  "@thi.ng/equiv": "^2.1.30",
41
41
  "@thi.ng/errors": "^2.3.5",
42
- "@thi.ng/random": "^3.6.4"
42
+ "@thi.ng/random": "^3.6.6"
43
43
  },
44
44
  "devDependencies": {
45
45
  "@microsoft/api-extractor": "^7.36.4",
@@ -83,6 +83,9 @@
83
83
  "./arg-sort": {
84
84
  "default": "./arg-sort.js"
85
85
  },
86
+ "./argmin": {
87
+ "default": "./argmin.js"
88
+ },
86
89
  "./binary-search": {
87
90
  "default": "./binary-search.js"
88
91
  },
@@ -149,6 +152,9 @@
149
152
  "./swizzle": {
150
153
  "default": "./swizzle.js"
151
154
  },
155
+ "./threshold": {
156
+ "default": "./threshold.js"
157
+ },
152
158
  "./topo-sort": {
153
159
  "default": "./topo-sort.js"
154
160
  }
@@ -156,5 +162,5 @@
156
162
  "thi.ng": {
157
163
  "year": 2018
158
164
  },
159
- "gitHead": "b2ef5a1b8932d067af4ec2fc7da03d59d6868dc7\n"
165
+ "gitHead": "10d8b3725e96c5d704c759489a89f132892b181e\n"
160
166
  }
package/threshold.d.ts ADDED
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Higher order function. Takes an object of threshold values and their target
3
+ * values, as well as a default value. Returns a new function which then matches
4
+ * a given value against all given thresholds and returns a matching target
5
+ * value, of (if none matched), the given default.
6
+ *
7
+ * @remarks
8
+ * The thresholds will be sorted & matched in ascending order using `<=`
9
+ * comparison.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * const numColumns = selectThresholdMin({ 480: 1, 640: 2, 960: 3 }, 4);
14
+ *
15
+ * numColumns(320) // 1
16
+ *
17
+ * numColumns(481) // 2
18
+ *
19
+ * numColumns(768) // 3
20
+ *
21
+ * numColumns(1024) // 4
22
+ * ```
23
+ *
24
+ * @param thresholds
25
+ * @param defaultVal
26
+ */
27
+ export declare const selectThresholdMin: <T>(thresholds: Record<number, T>, defaultVal: T) => (x: number) => T;
28
+ /**
29
+ * Similar to {@link selectThresholdMin}, but uses `>=` ordering.
30
+ *
31
+ * @param thresholds
32
+ * @param defaultVal
33
+ */
34
+ export declare const selectThresholdMax: <T>(thresholds: Record<number, T>, defaultVal: T) => (x: number) => T;
35
+ //# sourceMappingURL=threshold.d.ts.map
package/threshold.js ADDED
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Higher order function. Takes an object of threshold values and their target
3
+ * values, as well as a default value. Returns a new function which then matches
4
+ * a given value against all given thresholds and returns a matching target
5
+ * value, of (if none matched), the given default.
6
+ *
7
+ * @remarks
8
+ * The thresholds will be sorted & matched in ascending order using `<=`
9
+ * comparison.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * const numColumns = selectThresholdMin({ 480: 1, 640: 2, 960: 3 }, 4);
14
+ *
15
+ * numColumns(320) // 1
16
+ *
17
+ * numColumns(481) // 2
18
+ *
19
+ * numColumns(768) // 3
20
+ *
21
+ * numColumns(1024) // 4
22
+ * ```
23
+ *
24
+ * @param thresholds
25
+ * @param defaultVal
26
+ */
27
+ export const selectThresholdMin = (thresholds, defaultVal) => {
28
+ const $thresholds = Object.entries(thresholds)
29
+ .map(([k, v]) => [+k, v])
30
+ .sort((a, b) => a[0] - b[0]);
31
+ return (x) => {
32
+ const res = $thresholds.find((t) => x <= t[0]);
33
+ return res ? res[1] : defaultVal;
34
+ };
35
+ };
36
+ /**
37
+ * Similar to {@link selectThresholdMin}, but uses `>=` ordering.
38
+ *
39
+ * @param thresholds
40
+ * @param defaultVal
41
+ */
42
+ export const selectThresholdMax = (thresholds, defaultVal) => {
43
+ const $thresholds = Object.entries(thresholds)
44
+ .map(([k, v]) => [+k, v])
45
+ .sort((a, b) => b[0] - a[0]);
46
+ return (x) => {
47
+ const res = $thresholds.find((t) => x >= t[0]);
48
+ return res ? res[1] : defaultVal;
49
+ };
50
+ };