@dxos/util 0.8.2-main.30e4dbb → 0.8.2-main.4c6cf53

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.
Files changed (45) hide show
  1. package/dist/lib/browser/index.mjs +175 -156
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/lib/node/index.cjs +160 -138
  5. package/dist/lib/node/index.cjs.map +4 -4
  6. package/dist/lib/node/meta.json +1 -1
  7. package/dist/lib/node-esm/index.mjs +175 -156
  8. package/dist/lib/node-esm/index.mjs.map +4 -4
  9. package/dist/lib/node-esm/meta.json +1 -1
  10. package/dist/types/src/bitfield.d.ts +1 -1
  11. package/dist/types/src/bitfield.d.ts.map +1 -1
  12. package/dist/types/src/callback-collection.d.ts.map +1 -1
  13. package/dist/types/src/callback.d.ts.map +1 -1
  14. package/dist/types/src/complex.d.ts.map +1 -1
  15. package/dist/types/src/deep.d.ts +3 -2
  16. package/dist/types/src/deep.d.ts.map +1 -1
  17. package/dist/types/src/di-key.d.ts +3 -3
  18. package/dist/types/src/di-key.d.ts.map +1 -1
  19. package/dist/types/src/human-hash.d.ts.map +1 -1
  20. package/dist/types/src/index.d.ts +12 -11
  21. package/dist/types/src/index.d.ts.map +1 -1
  22. package/dist/types/src/map-values.d.ts +5 -0
  23. package/dist/types/src/map-values.d.ts.map +1 -1
  24. package/dist/types/src/object.d.ts +3 -0
  25. package/dist/types/src/object.d.ts.map +1 -0
  26. package/dist/types/src/position.d.ts +2 -3
  27. package/dist/types/src/position.d.ts.map +1 -1
  28. package/dist/types/src/sliding-window-summary.d.ts.map +1 -1
  29. package/dist/types/src/tracer.d.ts.map +1 -1
  30. package/package.json +10 -6
  31. package/src/bitfield.ts +2 -2
  32. package/src/callback-collection.ts +3 -3
  33. package/src/callback.ts +2 -2
  34. package/src/complex.ts +6 -6
  35. package/src/deep.ts +6 -2
  36. package/src/defer.ts +2 -2
  37. package/src/di-key.ts +2 -2
  38. package/src/human-hash.ts +2 -2
  39. package/src/index.ts +12 -11
  40. package/src/map-values.ts +14 -0
  41. package/src/object.ts +7 -0
  42. package/src/position.ts +12 -11
  43. package/src/sliding-window-summary.ts +3 -3
  44. package/src/tracer.ts +6 -6
  45. package/src/weak.ts +2 -2
package/src/index.ts CHANGED
@@ -2,43 +2,48 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- export * from './array';
6
5
  export * from './array-to-hex';
6
+ export * from './array';
7
7
  export * from './binder';
8
8
  export * from './bitfield';
9
- export * from './callback';
10
9
  export * from './callback-collection';
10
+ export * from './callback';
11
11
  export * from './case';
12
12
  export * from './chunk-array';
13
13
  export * from './circular-buffer';
14
+ export * from './clear-undefined';
14
15
  export * from './complex';
15
- export * from './defer';
16
- export * from './defer-function';
17
16
  export * from './deep';
17
+ export * from './defer-function';
18
+ export * from './defer';
18
19
  export * from './entry';
19
20
  export * from './first-two-chars';
20
21
  export * from './for-each-async';
21
22
  export * from './human-hash';
22
- export * from './interval';
23
23
  export * from './instance-id';
24
+ export * from './interval';
24
25
  export * from './join-tables';
25
26
  export * from './json';
26
- export * from './map';
27
27
  export * from './map-values';
28
+ export * from './map';
28
29
  export * from './no-infer';
29
- export * from './order';
30
+ export * from './object';
30
31
  export * from './order-keys';
32
+ export * from './order';
31
33
  export * from './pick';
32
34
  export * from './platform';
33
35
  export * from './position';
34
36
  export * from './random';
35
37
  export * from './range';
36
38
  export * from './reducers';
39
+ export * from './remove-undefined-keys';
37
40
  export * from './safe-await';
38
41
  export * from './safe-instanceof';
39
42
  export * from './safe-parse';
40
43
  export * from './sliding-window-summary';
44
+ export * from './sliding-window-summary';
41
45
  export * from './sort';
46
+ export * from './string';
42
47
  export * from './sum';
43
48
  export * from './throw-unhandled-error';
44
49
  export * from './to-fallback';
@@ -47,7 +52,3 @@ export * from './tree';
47
52
  export * from './types';
48
53
  export * from './uint8array';
49
54
  export * from './weak';
50
- export * from './sliding-window-summary';
51
- export * from './string';
52
- export * from './remove-undefined-keys';
53
- export * from './clear-undefined';
package/src/map-values.ts CHANGED
@@ -124,3 +124,17 @@ class DeepMapperAsync {
124
124
  }
125
125
  };
126
126
  }
127
+
128
+ /**
129
+ * Visits all values on an object or every item in an array.
130
+ * No-op if the value is not an object or array.
131
+ */
132
+ export const visitValues = (object: unknown, visitor: (value: unknown, key: string | number) => void) => {
133
+ if (Array.isArray(object)) {
134
+ object.forEach((item, index) => visitor(item, index));
135
+ } else if (typeof object === 'object' && object !== null) {
136
+ for (const [key, value] of Object.entries(object)) {
137
+ visitor(value, key);
138
+ }
139
+ }
140
+ };
package/src/object.ts ADDED
@@ -0,0 +1,7 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ export const keys = <R>(obj: R): (keyof R)[] => Object.keys(obj as any) as (keyof R)[];
6
+
7
+ export const entries = <R>(obj: R): [keyof R, R[keyof R]][] => Object.entries(obj as any) as [keyof R, any][];
package/src/position.ts CHANGED
@@ -2,9 +2,10 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
+ // TODO(burdon): This shouldn't be in this low-level util? Move to app-framework?
6
+
5
7
  /**
6
- * Determines priority order.
7
- *
8
+ * Determines priority order:
8
9
  * - `static` - Remain in natural order.
9
10
  * - `hoist` - Placed before `static`.
10
11
  * - `fallback` - Placed after `static`.
@@ -15,17 +16,17 @@ export type Position = 'static' | 'hoist' | 'fallback';
15
16
  /**
16
17
  * Sorting function for sorting by position.
17
18
  */
18
- export const byPosition = <T extends { position?: Position }>(a: T, b: T) => {
19
- const aPosition = a.position ?? 'static';
20
- const bPosition = b.position ?? 'static';
21
-
22
- if (aPosition === bPosition) {
19
+ export const byPosition = <T extends { position?: Position }>(
20
+ { position: a = 'static' }: T,
21
+ { position: b = 'static' }: T,
22
+ ) => {
23
+ if (a === b) {
23
24
  return 0;
24
- } else if (aPosition === 'hoist' || bPosition === 'fallback') {
25
+ } else if (a === 'hoist' || b === 'fallback') {
25
26
  return -1;
26
- } else if (bPosition === 'hoist' || aPosition === 'fallback') {
27
+ } else if (b === 'hoist' || a === 'fallback') {
27
28
  return 1;
29
+ } else {
30
+ return 0;
28
31
  }
29
-
30
- return 0;
31
32
  };
@@ -28,12 +28,12 @@ export class SlidingWindowSummary {
28
28
  }
29
29
  }
30
30
 
31
- public record(value: number) {
31
+ public record(value: number): void {
32
32
  const evicted = this._buffer.push(value);
33
33
  this._sum += value - (evicted ?? 0);
34
34
  }
35
35
 
36
- public average() {
36
+ public average(): number {
37
37
  return this._buffer.elementCount === 0 ? 0 : this._withPrecision(this._sum / this._buffer.elementCount);
38
38
  }
39
39
 
@@ -58,7 +58,7 @@ export class SlidingWindowSummary {
58
58
  return { mean, median, p90, stdDev, histogram };
59
59
  }
60
60
 
61
- private _withPrecision(value: number) {
61
+ private _withPrecision(value: number): number {
62
62
  if (this._precision == null) {
63
63
  return value;
64
64
  }
package/src/tracer.ts CHANGED
@@ -32,7 +32,7 @@ export class Tracer {
32
32
  return this._recording;
33
33
  }
34
34
 
35
- keys() {
35
+ keys(): string[] {
36
36
  return Array.from(this._events.keys());
37
37
  }
38
38
 
@@ -45,21 +45,21 @@ export class Tracer {
45
45
  return events;
46
46
  }
47
47
 
48
- clear() {
48
+ clear(): void {
49
49
  this._events.clear();
50
50
  }
51
51
 
52
- start() {
52
+ start(): this {
53
53
  this._recording = true;
54
54
  return this;
55
55
  }
56
56
 
57
- stop() {
57
+ stop(): this {
58
58
  this._recording = false;
59
59
  return this;
60
60
  }
61
61
 
62
- emit(id: string, value?: any) {
62
+ emit(id: string, value?: any): void {
63
63
  this._post(this._createEvent(id, value));
64
64
  }
65
65
 
@@ -84,7 +84,7 @@ export class Tracer {
84
84
  return event;
85
85
  }
86
86
 
87
- private _post(event: Event) {
87
+ private _post(event: Event): void {
88
88
  if (this._recording) {
89
89
  defaultMap(this._events, event.id, []).push(event);
90
90
  }
package/src/weak.ts CHANGED
@@ -96,7 +96,7 @@ export class WeakDictionary<K, V extends object> implements Map<K, V> {
96
96
  });
97
97
  }
98
98
 
99
- private _register(key: K, value: V) {
99
+ private _register(key: K, value: V): void {
100
100
  this._finalization.register(
101
101
  value,
102
102
  () => {
@@ -106,7 +106,7 @@ export class WeakDictionary<K, V extends object> implements Map<K, V> {
106
106
  );
107
107
  }
108
108
 
109
- private _unregister(value: V) {
109
+ private _unregister(value: V): void {
110
110
  this._finalization.unregister(value);
111
111
  }
112
112
  }