@dxos/util 0.7.4 → 0.7.5-feature-compute.4d9d99a

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 (43) hide show
  1. package/dist/lib/browser/index.mjs +70 -17
  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 +72 -18
  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 +70 -17
  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/array-to-hex.d.ts +1 -1
  11. package/dist/types/src/array-to-hex.d.ts.map +1 -1
  12. package/dist/types/src/array.d.ts +12 -0
  13. package/dist/types/src/array.d.ts.map +1 -1
  14. package/dist/types/src/bitfield.d.ts +1 -1
  15. package/dist/types/src/deep.d.ts.map +1 -1
  16. package/dist/types/src/disposition.d.ts +15 -0
  17. package/dist/types/src/disposition.d.ts.map +1 -0
  18. package/dist/types/src/disposition.test.d.ts +2 -0
  19. package/dist/types/src/disposition.test.d.ts.map +1 -0
  20. package/dist/types/src/index.d.ts +3 -1
  21. package/dist/types/src/index.d.ts.map +1 -1
  22. package/dist/types/src/no-infer.d.ts +2 -0
  23. package/dist/types/src/no-infer.d.ts.map +1 -0
  24. package/dist/types/src/safe-parse.d.ts +7 -0
  25. package/dist/types/src/safe-parse.d.ts.map +1 -0
  26. package/dist/types/src/types.d.ts +22 -4
  27. package/dist/types/src/types.d.ts.map +1 -1
  28. package/dist/types/tsconfig.tsbuildinfo +1 -0
  29. package/package.json +6 -6
  30. package/src/array-to-hex.ts +1 -1
  31. package/src/array.test.ts +83 -1
  32. package/src/array.ts +29 -0
  33. package/src/deep.ts +5 -3
  34. package/src/disposition.test.ts +98 -0
  35. package/src/disposition.ts +30 -0
  36. package/src/index.ts +3 -1
  37. package/src/instance-id.ts +1 -1
  38. package/src/no-infer.ts +5 -0
  39. package/src/safe-parse.ts +32 -0
  40. package/src/types.ts +31 -13
  41. package/dist/types/src/safe-parse-json.d.ts +0 -5
  42. package/dist/types/src/safe-parse-json.d.ts.map +0 -1
  43. package/src/safe-parse-json.ts +0 -15
@@ -0,0 +1,30 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ /**
6
+ * Determines priority order.
7
+ *
8
+ * - `static` - Remain in natural order.
9
+ * - `hoist` - Placed before `static`.
10
+ * - `fallback` - Placed after `static`.
11
+ */
12
+ export type Disposition = 'static' | 'hoist' | 'fallback';
13
+
14
+ /**
15
+ * Sorting function for sorting by disposition.
16
+ */
17
+ export const byDisposition = <T extends { disposition?: Disposition }>(a: T, b: T) => {
18
+ const aDisposition = a.disposition ?? 'static';
19
+ const bDisposition = b.disposition ?? 'static';
20
+
21
+ if (aDisposition === bDisposition) {
22
+ return 0;
23
+ } else if (aDisposition === 'hoist' || bDisposition === 'fallback') {
24
+ return -1;
25
+ } else if (bDisposition === 'hoist' || aDisposition === 'fallback') {
26
+ return 1;
27
+ }
28
+
29
+ return 0;
30
+ };
package/src/index.ts CHANGED
@@ -14,6 +14,7 @@ export * from './complex';
14
14
  export * from './defer';
15
15
  export * from './defer-function';
16
16
  export * from './deep';
17
+ export * from './disposition';
17
18
  export * from './entry';
18
19
  export * from './for-each-async';
19
20
  export * from './human-hash';
@@ -31,7 +32,7 @@ export * from './range';
31
32
  export * from './reducers';
32
33
  export * from './safe-await';
33
34
  export * from './safe-instanceof';
34
- export * from './safe-parse-json';
35
+ export * from './safe-parse';
35
36
  export * from './sliding-window-summary';
36
37
  export * from './sort';
37
38
  export * from './sum';
@@ -47,3 +48,4 @@ export * from './chunk-array';
47
48
  export * from './array-to-hex';
48
49
  export * from './remove-undefined-keys';
49
50
  export * from './order-keys';
51
+ export * from './no-infer';
@@ -42,5 +42,5 @@ export const getDebugName = (instance: any): string => {
42
42
  }
43
43
 
44
44
  const prototype = Object.getPrototypeOf(instance);
45
- return `${prototype.constructor.name}#${getPrototypeSpecificInstanceId(instance)}`;
45
+ return `${prototype.constructor?.name ?? 'Object'}#${getPrototypeSpecificInstanceId(instance)}`;
46
46
  };
@@ -0,0 +1,5 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ export type NoInfer<A> = [A][A extends any ? 0 : never];
@@ -0,0 +1,32 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ export const safeParseInt = (value: string | undefined, defaultValue?: number) => {
6
+ try {
7
+ const n = parseInt(value ?? '');
8
+ return isNaN(n) ? defaultValue : n;
9
+ } catch (err) {
10
+ return defaultValue;
11
+ }
12
+ };
13
+
14
+ export const safeParseFloat = (str: string, defaultValue?: number): number | undefined => {
15
+ try {
16
+ return parseFloat(str);
17
+ } catch {
18
+ return defaultValue ?? 0;
19
+ }
20
+ };
21
+
22
+ export const safeParseJson: {
23
+ <T extends object>(data: string | undefined | null, defaultValue: T): T;
24
+ <T extends object>(data: string | undefined | null): T | undefined;
25
+ } = <T extends object>(data: string | undefined | null, defaultValue?: T) => {
26
+ if (data) {
27
+ try {
28
+ return JSON.parse(data);
29
+ } catch (err) {}
30
+ }
31
+ return defaultValue;
32
+ };
package/src/types.ts CHANGED
@@ -10,7 +10,34 @@ export type MaybeProvider<T, V = void> = T | ((arg: V) => T);
10
10
 
11
11
  export type MaybePromise<T> = T | Promise<T>;
12
12
 
13
- export type MakeOptional<Type, Key extends keyof Type> = Omit<Type, Key> & Partial<Pick<Type, Key>>;
13
+ export type GuardedType<T> = T extends (value: any) => value is infer R ? R : never;
14
+
15
+ export type DeepReadonly<T> = {
16
+ readonly [P in keyof T]: T[P] extends Record<string, any>
17
+ ? DeepReadonly<T[P]>
18
+ : T[P] extends Array<infer U>
19
+ ? ReadonlyArray<DeepReadonly<U>>
20
+ : T[P];
21
+ };
22
+
23
+ export type DeepWriteable<T> = { -readonly [K in keyof T]: T[K] extends object ? DeepWriteable<T[K]> : T[K] };
24
+
25
+ /**
26
+ * Simplifies type (copied from effect).
27
+ */
28
+ export type Simplify<A> = { [K in keyof A]: A[K] } extends infer B ? B : never;
29
+
30
+ /**
31
+ * Replace types of specified keys.
32
+ */
33
+ export type Specialize<T, U> = Simplify<Omit<T, keyof U> & U>;
34
+
35
+ /**
36
+ * Make specified keys optional.
37
+ */
38
+ // TODO(burdon): Wrapping with Simplify fails.
39
+ // export type MakeOptional<T, K extends keyof T> = Simplify<Omit<T, K> & Partial<Pick<T, K>>>;
40
+ export type MakeOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
14
41
 
15
42
  /**
16
43
  * All types that evaluate to false when cast to a boolean.
@@ -27,7 +54,7 @@ export const isNotFalsy = <T>(value: T): value is Exclude<T, Falsy> => !!value;
27
54
  export const nonNullable = <T>(value: T): value is NonNullable<T> => value !== null && value !== undefined;
28
55
  export const isNotNullOrUndefined = <T>(value: T): value is Exclude<T, null | undefined> => value != null;
29
56
  // export const isNotNullish = <T>(value: T | null | undefined): value is T => value !== undefined && value !== null;
30
- export const boolGuard = <T>(value: T | null | undefined): value is T => Boolean(value);
57
+ // export const boolGuard = <T>(value: T | null | undefined): value is T => Boolean(value);
31
58
 
32
59
  // TODO(burdon): Replace use of setTimeout everywhere?
33
60
  // Would remove the need to cancel (and associated errors), but would change the operation of the code
@@ -55,14 +82,14 @@ export const getAsyncProviderValue = <T, V = void>(
55
82
  /**
56
83
  * Remove keys with undefined values.
57
84
  */
58
- export const stripUndefinedValues = <T extends { [index: string]: any }>(obj: T): T => {
85
+ export const stripUndefined = <T extends { [index: string]: any }>(obj: T): T => {
59
86
  if (typeof obj === 'object') {
60
87
  Object.keys(obj).forEach((key) => {
61
88
  const value = obj[key];
62
89
  if (value === undefined) {
63
90
  delete obj[key];
64
91
  } else if (value !== null && typeof value === 'object') {
65
- stripUndefinedValues(value); // TODO(burdon): Test recursion.
92
+ stripUndefined(value); // TODO(burdon): Test recursion.
66
93
  }
67
94
  });
68
95
  }
@@ -88,12 +115,3 @@ export const arrayMove = <T>(array: T[], from: number, to: number): Array<T> =>
88
115
  array.splice(to < 0 ? array.length + to : to, 0, array.splice(from, 1)[0]);
89
116
  return array;
90
117
  };
91
-
92
- export const safeParseInt = (value: string | undefined, defaultValue?: number) => {
93
- try {
94
- const n = parseInt(value ?? '');
95
- return isNaN(n) ? defaultValue : n;
96
- } catch (err) {
97
- return defaultValue;
98
- }
99
- };
@@ -1,5 +0,0 @@
1
- export declare const safeParseJson: {
2
- <T extends object>(data: string | undefined | null, defaultValue: T): T;
3
- <T extends object>(data: string | undefined | null): T | undefined;
4
- };
5
- //# sourceMappingURL=safe-parse-json.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"safe-parse-json.d.ts","sourceRoot":"","sources":["../../../src/safe-parse-json.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,aAAa,EAAE;IAC1B,CAAC,CAAC,SAAS,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,EAAE,YAAY,EAAE,CAAC,GAAG,CAAC,CAAC;IACxE,CAAC,CAAC,SAAS,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,GAAG,CAAC,GAAG,SAAS,CAAC;CAQpE,CAAC"}
@@ -1,15 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- export const safeParseJson: {
6
- <T extends object>(data: string | undefined | null, defaultValue: T): T;
7
- <T extends object>(data: string | undefined | null): T | undefined;
8
- } = <T extends object>(data: string | undefined | null, defaultValue?: T) => {
9
- if (data) {
10
- try {
11
- return JSON.parse(data);
12
- } catch (err) {}
13
- }
14
- return defaultValue;
15
- };