@thi.ng/transducers 9.0.13 → 9.1.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**: 2024-08-10T15:03:07Z
3
+ - **Last updated**: 2024-08-13T19:40:07Z
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
+ ## [9.1.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/transducers@9.1.0) (2024-08-13)
13
+
14
+ #### 🚀 Features
15
+
16
+ - add sortedFrequencies() reducer ([1257a79](https://github.com/thi-ng/umbrella/commit/1257a79))
17
+ - add pushKeys() reducer ([f19d152](https://github.com/thi-ng/umbrella/commit/f19d152))
18
+
12
19
  ### [9.0.6](https://github.com/thi-ng/umbrella/tree/@thi.ng/transducers@9.0.6) (2024-06-21)
13
20
 
14
21
  #### ♻️ Refactoring
package/README.md CHANGED
@@ -150,7 +150,7 @@ For Node.js REPL:
150
150
  const tx = await import("@thi.ng/transducers");
151
151
  ```
152
152
 
153
- Package sizes (brotli'd, pre-treeshake): ESM: 8.94 KB
153
+ Package sizes (brotli'd, pre-treeshake): ESM: 8.97 KB
154
154
 
155
155
  ## Dependencies
156
156
 
@@ -1071,9 +1071,11 @@ result (as if it would be called via `reduce()`).
1071
1071
  - [normFrequenciesAuto](https://github.com/thi-ng/umbrella/tree/develop/packages/transducers/src/norm-frequencies-auto.ts)
1072
1072
  - [push](https://github.com/thi-ng/umbrella/tree/develop/packages/transducers/src/push.ts)
1073
1073
  - [pushCopy](https://github.com/thi-ng/umbrella/tree/develop/packages/transducers/src/push-copy.ts)
1074
+ - [pushKeys](https://github.com/thi-ng/umbrella/tree/develop/packages/transducers/src/push-keys.ts)
1074
1075
  - [pushSort](https://github.com/thi-ng/umbrella/tree/develop/packages/transducers/src/push-sort.ts)
1075
1076
  - [reductions](https://github.com/thi-ng/umbrella/tree/develop/packages/transducers/src/reductions.ts)
1076
1077
  - [some](https://github.com/thi-ng/umbrella/tree/develop/packages/transducers/src/some.ts)
1078
+ - [sortedFrequencies](https://github.com/thi-ng/umbrella/tree/develop/packages/transducers/src/sorted-frequencies.ts)
1077
1079
  - [str](https://github.com/thi-ng/umbrella/tree/develop/packages/transducers/src/str.ts)
1078
1080
  - [sub](https://github.com/thi-ng/umbrella/tree/develop/packages/transducers/src/sub.ts)
1079
1081
 
package/index.d.ts CHANGED
@@ -42,9 +42,11 @@ export * from "./norm-frequencies.js";
42
42
  export * from "./norm-frequencies-auto.js";
43
43
  export * from "./push.js";
44
44
  export * from "./push-copy.js";
45
+ export * from "./push-keys.js";
45
46
  export * from "./push-sort.js";
46
47
  export * from "./reductions.js";
47
48
  export * from "./some.js";
49
+ export * from "./sorted-frequencies.js";
48
50
  export * from "./str.js";
49
51
  export * from "./sub.js";
50
52
  export * from "./benchmark.js";
package/index.js CHANGED
@@ -42,9 +42,11 @@ export * from "./norm-frequencies.js";
42
42
  export * from "./norm-frequencies-auto.js";
43
43
  export * from "./push.js";
44
44
  export * from "./push-copy.js";
45
+ export * from "./push-keys.js";
45
46
  export * from "./push-sort.js";
46
47
  export * from "./reductions.js";
47
48
  export * from "./some.js";
49
+ export * from "./sorted-frequencies.js";
48
50
  export * from "./str.js";
49
51
  export * from "./sub.js";
50
52
  export * from "./benchmark.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/transducers",
3
- "version": "9.0.13",
3
+ "version": "9.1.0",
4
4
  "description": "Collection of ~170 lightweight, composable transducers, reducers, generators, iterators for functional data transformations",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -46,7 +46,7 @@
46
46
  "@thi.ng/compare": "^2.4.0",
47
47
  "@thi.ng/compose": "^3.0.11",
48
48
  "@thi.ng/errors": "^2.5.14",
49
- "@thi.ng/math": "^5.11.6",
49
+ "@thi.ng/math": "^5.11.7",
50
50
  "@thi.ng/random": "^4.0.2"
51
51
  },
52
52
  "devDependencies": {
@@ -422,6 +422,9 @@
422
422
  "./push-copy": {
423
423
  "default": "./push-copy.js"
424
424
  },
425
+ "./push-keys": {
426
+ "default": "./push-keys.js"
427
+ },
425
428
  "./push-sort": {
426
429
  "default": "./push-sort.js"
427
430
  },
@@ -494,6 +497,9 @@
494
497
  "./some": {
495
498
  "default": "./some.js"
496
499
  },
500
+ "./sorted-frequencies": {
501
+ "default": "./sorted-frequencies.js"
502
+ },
497
503
  "./sorted-keys": {
498
504
  "default": "./sorted-keys.js"
499
505
  },
@@ -590,5 +596,5 @@
590
596
  ],
591
597
  "year": 2016
592
598
  },
593
- "gitHead": "ec78f98d015e4d214a0b840e72e497407807daf3\n"
599
+ "gitHead": "e914ebbd81c56783c39cf746548c547cbacadc96\n"
594
600
  }
package/push-copy.d.ts CHANGED
@@ -1,3 +1,10 @@
1
1
  import type { Reducer } from "./api.js";
2
+ /**
3
+ * Reducer. Special version of {@link push}, which immutably appends inputs to
4
+ * an array, yielding a new array for each reduction step.
5
+ *
6
+ * @remarks
7
+ * Also see {@link pushKeys}, {@link pushSort}.
8
+ */
2
9
  export declare const pushCopy: <T>() => Reducer<T, T[]>;
3
10
  //# sourceMappingURL=push-copy.d.ts.map
package/push-copy.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { reducer } from "./reduce.js";
2
2
  const pushCopy = () => reducer(
3
3
  () => [],
4
- (acc, x) => ((acc = acc.slice()).push(x), acc)
4
+ (acc, x) => acc.concat([x])
5
5
  );
6
6
  export {
7
7
  pushCopy
package/push-keys.d.ts ADDED
@@ -0,0 +1,25 @@
1
+ import type { Keys } from "@thi.ng/api";
2
+ import type { Reducer } from "./api.js";
3
+ /**
4
+ * Reducer. Combination of {@link push} and {@link pluck}, which looks up given
5
+ * `key` in each input and collects these values into an array.
6
+ *
7
+ * @remarks
8
+ * Also see {@link pushCopy}, {@link pushSort}.
9
+ *
10
+ * @example
11
+ * ```ts tangle:../export/push-keys.ts
12
+ * import { pushKeys } from "@thi.ng/transducers";
13
+ *
14
+ * const data = [{id: "a", val: 1}, {id: "b", val: 2}, {id: "c", val: 3}];
15
+ *
16
+ * console.log(pushKeys("id", data));
17
+ * // ["a", "b", "c"]
18
+ *
19
+ * console.log(pushKeys("val", data));
20
+ * // [1, 2, 3]
21
+ * ```
22
+ */
23
+ export declare function pushKeys<T, K extends Keys<T>>(key: K): Reducer<T, T[K][]>;
24
+ export declare function pushKeys<T, K extends Keys<T>>(key: K, src: Iterable<T>): T[K][];
25
+ //# sourceMappingURL=push-keys.d.ts.map
package/push-keys.js ADDED
@@ -0,0 +1,10 @@
1
+ import { reducer } from "./reduce.js";
2
+ function pushKeys(key, src) {
3
+ return src ? [...src].map((x) => x[key]) : reducer(
4
+ () => [],
5
+ (acc, x) => (acc.push(x[key]), acc)
6
+ );
7
+ }
8
+ export {
9
+ pushKeys
10
+ };
package/push.d.ts CHANGED
@@ -1,6 +1,9 @@
1
1
  import type { Reducer } from "./api.js";
2
2
  /**
3
- * Reducer which collects inputs into a new array.
3
+ * Reducer which collects inputs into an array.
4
+ *
5
+ * @remarks
6
+ * Also see {@link pushCopy}, {@link pushKeys}, {@link pushSort}.
4
7
  */
5
8
  export declare function push<T>(): Reducer<T, T[]>;
6
9
  export declare function push<T>(src: Iterable<T>): T[];
@@ -0,0 +1,34 @@
1
+ import type { Fn } from "@thi.ng/api";
2
+ import type { Reducer } from "./api.js";
3
+ /**
4
+ * Reducer. Similar to {@link frequencies}, but instead of a Map produces an
5
+ * array of `[value, frequency]`-pairs, sorted by the descending number of
6
+ * occurrences of each distinct value.
7
+ *
8
+ * @example
9
+ * ```ts tangle:../export/sorted-frequencies.ts
10
+ * import { filter, sortedFrequencies, transduce } from "@thi.ng/transducers";
11
+ *
12
+ * const input = "hello, world! this transducers-stuff is pretty awesome! :)";
13
+ *
14
+ * const freqs = transduce(
15
+ * filter(x => /[a-z0-9]/.test(x)),
16
+ * sortedFrequencies(),
17
+ * input
18
+ * );
19
+ *
20
+ * console.log(freqs);
21
+ * // [
22
+ * // [ 's', 6 ], [ 'e', 5 ], [ 't', 5 ], [ 'r', 4 ],
23
+ * // [ 'l', 3 ], [ 'o', 3 ], [ 'h', 2 ], [ 'w', 2 ],
24
+ * // [ 'd', 2 ], [ 'i', 2 ], [ 'a', 2 ], [ 'u', 2 ],
25
+ * // [ 'f', 2 ], [ 'n', 1 ], [ 'c', 1 ], [ 'p', 1 ],
26
+ * // [ 'y', 1 ], [ 'm', 1 ]
27
+ * // ]
28
+ * ```
29
+ */
30
+ export declare function sortedFrequencies<A>(): Reducer<A, [A, number][]>;
31
+ export declare function sortedFrequencies<A>(src: Iterable<A>): [A, number][];
32
+ export declare function sortedFrequencies<A, B>(key: Fn<A, B>): Reducer<A, [B, number][]>;
33
+ export declare function sortedFrequencies<A, B>(key: Fn<A, B>, src: Iterable<A>): Map<B, number>;
34
+ //# sourceMappingURL=sorted-frequencies.d.ts.map
@@ -0,0 +1,20 @@
1
+ import { identity } from "@thi.ng/compose/identity";
2
+ import { count } from "./count.js";
3
+ import { groupByMap } from "./group-by-map.js";
4
+ import { $$reduce } from "./reduce.js";
5
+ function sortedFrequencies(...args) {
6
+ const res = $$reduce(sortedFrequencies, args);
7
+ if (res) return res;
8
+ const [init, complete, reduce] = groupByMap({
9
+ key: args[0] || identity,
10
+ group: count()
11
+ });
12
+ return [
13
+ init,
14
+ (acc) => [...complete(acc)].sort((a, b) => b[1] - a[1]),
15
+ (acc, x) => reduce(acc, x)
16
+ ];
17
+ }
18
+ export {
19
+ sortedFrequencies
20
+ };