@dxos/util 0.7.5-main.9d2a38b → 0.7.5-main.b19bfc8

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 (40) hide show
  1. package/dist/lib/browser/index.mjs +70 -51
  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 +68 -50
  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 -51
  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.d.ts +7 -0
  11. package/dist/types/src/array.d.ts.map +1 -1
  12. package/dist/types/src/index.d.ts +5 -5
  13. package/dist/types/src/index.d.ts.map +1 -1
  14. package/dist/types/src/position.d.ts +15 -0
  15. package/dist/types/src/position.d.ts.map +1 -0
  16. package/dist/types/src/position.test.d.ts +2 -0
  17. package/dist/types/src/position.test.d.ts.map +1 -0
  18. package/dist/types/src/range.d.ts +4 -0
  19. package/dist/types/src/range.d.ts.map +1 -1
  20. package/dist/types/src/string.d.ts +5 -0
  21. package/dist/types/src/string.d.ts.map +1 -0
  22. package/dist/types/src/types.d.ts +15 -4
  23. package/dist/types/src/types.d.ts.map +1 -1
  24. package/package.json +7 -6
  25. package/src/array.ts +17 -0
  26. package/src/index.ts +6 -6
  27. package/src/instance-id.ts +1 -1
  28. package/src/position.test.ts +98 -0
  29. package/src/position.ts +30 -0
  30. package/src/range.ts +7 -0
  31. package/src/safe-parse.ts +1 -1
  32. package/src/string.ts +14 -0
  33. package/src/types.test.ts +2 -2
  34. package/src/types.ts +17 -7
  35. package/dist/types/src/disposition.d.ts +0 -15
  36. package/dist/types/src/disposition.d.ts.map +0 -1
  37. package/dist/types/src/disposition.test.d.ts +0 -2
  38. package/dist/types/src/disposition.test.d.ts.map +0 -1
  39. package/src/disposition.test.ts +0 -98
  40. package/src/disposition.ts +0 -30
package/src/types.ts CHANGED
@@ -10,8 +10,6 @@ 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>>;
14
-
15
13
  export type GuardedType<T> = T extends (value: any) => value is infer R ? R : never;
16
14
 
17
15
  export type DeepReadonly<T> = {
@@ -24,6 +22,22 @@ export type DeepReadonly<T> = {
24
22
 
25
23
  export type DeepWriteable<T> = { -readonly [K in keyof T]: T[K] extends object ? DeepWriteable<T[K]> : T[K] };
26
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> = Omit<T, K> & Partial<Pick<T, K>>;
40
+
27
41
  /**
28
42
  * All types that evaluate to false when cast to a boolean.
29
43
  */
@@ -34,12 +48,8 @@ export type Falsy = false | 0 | '' | null | undefined;
34
48
  * NOTE: To filter by type:
35
49
  * items.filter((item: any): item is RangeSet<Decoration> => item instanceof RangeSet)
36
50
  */
37
- // TODO(burdon): Reconcile names and variants.
38
51
  export const isNotFalsy = <T>(value: T): value is Exclude<T, Falsy> => !!value;
39
- export const nonNullable = <T>(value: T): value is NonNullable<T> => value !== null && value !== undefined;
40
- export const isNotNullOrUndefined = <T>(value: T): value is Exclude<T, null | undefined> => value != null;
41
- // export const isNotNullish = <T>(value: T | null | undefined): value is T => value !== undefined && value !== null;
42
- export const boolGuard = <T>(value: T | null | undefined): value is T => Boolean(value);
52
+ export const isNonNullable = <T>(value: T | null | undefined): value is T => value != null;
43
53
 
44
54
  // TODO(burdon): Replace use of setTimeout everywhere?
45
55
  // Would remove the need to cancel (and associated errors), but would change the operation of the code
@@ -1,15 +0,0 @@
1
- /**
2
- * Determines priority order.
3
- *
4
- * - `static` - Remain in natural order.
5
- * - `hoist` - Placed before `static`.
6
- * - `fallback` - Placed after `static`.
7
- */
8
- export type Disposition = 'static' | 'hoist' | 'fallback';
9
- /**
10
- * Sorting function for sorting by disposition.
11
- */
12
- export declare const byDisposition: <T extends {
13
- disposition?: Disposition;
14
- }>(a: T, b: T) => 0 | 1 | -1;
15
- //# sourceMappingURL=disposition.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"disposition.d.ts","sourceRoot":"","sources":["../../../src/disposition.ts"],"names":[],"mappings":"AAIA;;;;;;GAMG;AACH,MAAM,MAAM,WAAW,GAAG,QAAQ,GAAG,OAAO,GAAG,UAAU,CAAC;AAE1D;;GAEG;AACH,eAAO,MAAM,aAAa,GAAI,CAAC,SAAS;IAAE,WAAW,CAAC,EAAE,WAAW,CAAA;CAAE,KAAK,CAAC,KAAK,CAAC,eAahF,CAAC"}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=disposition.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"disposition.test.d.ts","sourceRoot":"","sources":["../../../src/disposition.test.ts"],"names":[],"mappings":""}
@@ -1,98 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import { describe, it, expect } from 'vitest';
6
-
7
- import { byDisposition, type Disposition } from './disposition';
8
-
9
- type TestItem = {
10
- id: number;
11
- disposition?: Disposition;
12
- };
13
-
14
- describe('byDisposition', () => {
15
- it('should keep items with same disposition in their original order', () => {
16
- const items: TestItem[] = [
17
- { id: 1, disposition: 'static' },
18
- { id: 2, disposition: 'static' },
19
- { id: 3, disposition: 'hoist' },
20
- { id: 4, disposition: 'hoist' },
21
- { id: 5, disposition: 'fallback' },
22
- { id: 6, disposition: 'fallback' },
23
- ];
24
-
25
- const sorted = [...items].sort(byDisposition);
26
-
27
- // Check that items with the same disposition maintain relative order
28
- expect(sorted.findIndex((item) => item.id === 3)).toBeLessThan(sorted.findIndex((item) => item.id === 4));
29
- expect(sorted.findIndex((item) => item.id === 1)).toBeLessThan(sorted.findIndex((item) => item.id === 2));
30
- expect(sorted.findIndex((item) => item.id === 5)).toBeLessThan(sorted.findIndex((item) => item.id === 6));
31
- });
32
-
33
- it('should place "hoist" items before "static" items', () => {
34
- const items: TestItem[] = [
35
- { id: 1, disposition: 'static' },
36
- { id: 2, disposition: 'hoist' },
37
- ];
38
-
39
- const sorted = [...items].sort(byDisposition);
40
- expect(sorted[0].disposition).toBe('hoist');
41
- expect(sorted[1].disposition).toBe('static');
42
- });
43
-
44
- it('should place "fallback" items after "static" items', () => {
45
- const items: TestItem[] = [
46
- { id: 1, disposition: 'fallback' },
47
- { id: 2, disposition: 'static' },
48
- ];
49
-
50
- const sorted = [...items].sort(byDisposition);
51
- expect(sorted[0].disposition).toBe('static');
52
- expect(sorted[1].disposition).toBe('fallback');
53
- });
54
-
55
- it('should treat items without disposition as "static"', () => {
56
- const items: TestItem[] = [{ id: 1 }, { id: 2, disposition: 'hoist' }, { id: 3, disposition: 'fallback' }];
57
-
58
- const sorted = [...items].sort(byDisposition);
59
- expect(sorted[0].disposition).toBe('hoist');
60
- expect(sorted[1].disposition).toBeUndefined();
61
- expect(sorted[2].disposition).toBe('fallback');
62
- });
63
-
64
- it('should correctly sort mixed dispositions', () => {
65
- const items: TestItem[] = [
66
- { id: 1, disposition: 'fallback' },
67
- { id: 2, disposition: 'static' },
68
- { id: 3, disposition: 'hoist' },
69
- { id: 4 }, // implicit static
70
- { id: 5, disposition: 'hoist' },
71
- { id: 6, disposition: 'fallback' },
72
- ];
73
-
74
- const sorted = [...items].sort(byDisposition);
75
-
76
- // All hoisted items should come first
77
- expect(sorted[0].disposition).toBe('hoist');
78
- expect(sorted[1].disposition).toBe('hoist');
79
-
80
- // Static items (including undefined) should be in the middle
81
- expect(sorted[2].disposition).toBe('static');
82
- expect(sorted[3].disposition).toBeUndefined();
83
-
84
- // Fallback items should be last
85
- expect(sorted[4].disposition).toBe('fallback');
86
- expect(sorted[5].disposition).toBe('fallback');
87
- });
88
-
89
- it('should handle empty arrays', () => {
90
- const items: TestItem[] = [];
91
- expect(() => [...items].sort(byDisposition)).not.toThrow();
92
- });
93
-
94
- it('should handle single item arrays', () => {
95
- const items: TestItem[] = [{ id: 1, disposition: 'static' }];
96
- expect(() => [...items].sort(byDisposition)).not.toThrow();
97
- });
98
- });
@@ -1,30 +0,0 @@
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
- };