@dxos/util 0.5.1-next.f040be1 → 0.5.2-main.16cd857

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.
@@ -1 +1 @@
1
- {"version":3,"file":"map-values.d.ts","sourceRoot":"","sources":["../../../src/map-values.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,SAAS,qDAAsD,MAAM,4BAMjF,CAAC"}
1
+ {"version":3,"file":"map-values.d.ts","sourceRoot":"","sources":["../../../src/map-values.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,SAAS,qDAAsD,MAAM,4BAMjF,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,aAAa,UAAW,GAAG,cAAc,GAAG,mBAAmB,GAAG,KAAK,GAAG,KAAK,GAAG,KAAG,GAEjG,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=map-values.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"map-values.test.d.ts","sourceRoot":"","sources":["../../../src/map-values.test.ts"],"names":[],"mappings":""}
@@ -1,3 +1,11 @@
1
- export declare const range: (n: number) => number[];
2
- export declare const rangeFromTo: (from: number, to: number) => number[];
1
+ export declare const range: {
2
+ (n: number): number[];
3
+ <T>(n: number, mapper: RangeMapper<T>): T[];
4
+ };
5
+ export declare const rangeFromTo: {
6
+ (from: number, to: number): number[];
7
+ <T>(from: number, to: number, mapper: RangeMapper<T>): T[];
8
+ };
9
+ type RangeMapper<T> = (n: number) => T;
10
+ export {};
3
11
  //# sourceMappingURL=range.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"range.d.ts","sourceRoot":"","sources":["../../../src/range.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,KAAK,MAAO,MAAM,aAAgC,CAAC;AAEhE,eAAO,MAAM,WAAW,SAAU,MAAM,MAAM,MAAM,aAA0C,CAAC"}
1
+ {"version":3,"file":"range.d.ts","sourceRoot":"","sources":["../../../src/range.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,KAAK,EAAE;IAClB,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IACtB,CAAC,CAAC,EAAE,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC;CAI7C,CAAC;AAEF,eAAO,MAAM,WAAW,EAAE;IACxB,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IACrC,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC;CAG5D,CAAC;AAEF,KAAK,WAAW,CAAC,CAAC,IAAI,CAAC,CAAC,EAAE,MAAM,KAAK,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/util",
3
- "version": "0.5.1-next.f040be1",
3
+ "version": "0.5.2-main.16cd857",
4
4
  "description": "Temporary bucket for misc functions, which should graduate into separate packages.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -16,13 +16,13 @@
16
16
  "src"
17
17
  ],
18
18
  "dependencies": {
19
- "@dxos/debug": "0.5.1-next.f040be1",
20
- "@dxos/keys": "0.5.1-next.f040be1",
21
- "@dxos/invariant": "0.5.1-next.f040be1",
22
- "@dxos/node-std": "0.5.1-next.f040be1"
19
+ "@dxos/debug": "0.5.2-main.16cd857",
20
+ "@dxos/keys": "0.5.2-main.16cd857",
21
+ "@dxos/node-std": "0.5.2-main.16cd857",
22
+ "@dxos/invariant": "0.5.2-main.16cd857"
23
23
  },
24
24
  "devDependencies": {
25
- "@dxos/crypto": "0.5.1-next.f040be1"
25
+ "@dxos/crypto": "0.5.2-main.16cd857"
26
26
  },
27
27
  "publishConfig": {
28
28
  "access": "public"
package/src/complex.ts CHANGED
@@ -209,6 +209,13 @@ export class ComplexMap<K, V> implements Map<K, V> {
209
209
  return this._values.values();
210
210
  }
211
211
 
212
+ mapValues<R>(mapper: (v: V, k: K) => R): ComplexMap<K, R> {
213
+ return new ComplexMap(
214
+ this._keyProjection,
215
+ [...this.entries()].map(([key, value]) => [key, mapper(value, key)]),
216
+ );
217
+ }
218
+
212
219
  get [Symbol.toStringTag](): string {
213
220
  return 'ComplexMap';
214
221
  }
@@ -0,0 +1,44 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { test } from 'vitest';
6
+
7
+ import { deepMapValues } from './map-values';
8
+
9
+ test('deepMapValues', ({ expect }) => {
10
+ const obj = {
11
+ a: 1,
12
+ b: {
13
+ c: 2,
14
+ },
15
+ d: [
16
+ 3,
17
+ {
18
+ e: 4,
19
+ },
20
+ ],
21
+ };
22
+
23
+ // Double all numbers.
24
+ const res = deepMapValues(obj, (value, recurse) => {
25
+ if (typeof value === 'number') {
26
+ return value * 2;
27
+ }
28
+
29
+ return recurse(value);
30
+ });
31
+
32
+ expect(res).toEqual({
33
+ a: 2,
34
+ b: {
35
+ c: 4,
36
+ },
37
+ d: [
38
+ 6,
39
+ {
40
+ e: 8,
41
+ },
42
+ ],
43
+ });
44
+ });
package/src/map-values.ts CHANGED
@@ -9,3 +9,49 @@ export const mapValues = <T, U>(obj: Record<string, T>, fn: (value: T, key: stri
9
9
  });
10
10
  return result;
11
11
  };
12
+
13
+ /**
14
+ * Recursively maps values traversing arrays and objects.
15
+ * @param fn Function to apply to each value. Second argument is a function to recurse into the value.
16
+ */
17
+ export const deepMapValues = (value: any, fn: (value: any, recurse: (value: any) => any) => any): any => {
18
+ return new DeepMapper(fn).map(value);
19
+ };
20
+
21
+ class DeepMapper {
22
+ private readonly _cyclic = new Map<any, any>();
23
+
24
+ constructor(private readonly _fn: (value: any, recurse: (value: any) => any) => any) {}
25
+
26
+ map(value: any): any {
27
+ if (this._cyclic.has(value)) {
28
+ return this._cyclic.get(value);
29
+ }
30
+
31
+ return this._fn(value, this._recurse);
32
+ }
33
+
34
+ private _recurse = (value: any) => {
35
+ if (this._cyclic.has(value)) {
36
+ return this._cyclic.get(value);
37
+ }
38
+
39
+ if (Array.isArray(value)) {
40
+ const res = new Array(value.length);
41
+ this._cyclic.set(value, res);
42
+ for (let i = 0; i < value.length; i++) {
43
+ res[i] = this.map(value[i]);
44
+ }
45
+ return res;
46
+ } else if (value !== null && typeof value === 'object') {
47
+ const res: any = {};
48
+ this._cyclic.set(value, res);
49
+ for (const key in value) {
50
+ res[key] = this.map(value[key]);
51
+ }
52
+ return res;
53
+ } else {
54
+ return value;
55
+ }
56
+ };
57
+ }
package/src/range.ts CHANGED
@@ -2,6 +2,19 @@
2
2
  // Copyright 2020 DXOS.org
3
3
  //
4
4
 
5
- export const range = (n: number) => Array.from(Array(n).keys());
5
+ export const range: {
6
+ (n: number): number[];
7
+ <T>(n: number, mapper: RangeMapper<T>): T[];
8
+ } = <T>(n: number, mapper?: RangeMapper<T>) => {
9
+ const range = Array.from(Array(n).keys());
10
+ return mapper == null ? range : range.map(mapper);
11
+ };
6
12
 
7
- export const rangeFromTo = (from: number, to: number) => range(to - from).map((i) => i + from);
13
+ export const rangeFromTo: {
14
+ (from: number, to: number): number[];
15
+ <T>(from: number, to: number, mapper: RangeMapper<T>): T[];
16
+ } = <T = number>(from: number, to: number, mapper?: RangeMapper<T>) => {
17
+ return mapper == null ? range(to - from, (i) => i + from) : range(to - from, (i) => mapper(i + from));
18
+ };
19
+
20
+ type RangeMapper<T> = (n: number) => T;