@evolu/common 8.0.0-next.5 → 8.0.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.
Files changed (236) hide show
  1. package/dist/src/Array.d.ts +279 -173
  2. package/dist/src/Array.d.ts.map +1 -1
  3. package/dist/src/Array.js +105 -101
  4. package/dist/src/Assert.d.ts +39 -44
  5. package/dist/src/Assert.d.ts.map +1 -1
  6. package/dist/src/Assert.js +38 -31
  7. package/dist/src/BigInt.d.ts +6 -3
  8. package/dist/src/BigInt.d.ts.map +1 -1
  9. package/dist/src/BigInt.js +5 -2
  10. package/dist/src/Brand.d.ts +27 -40
  11. package/dist/src/Brand.d.ts.map +1 -1
  12. package/dist/src/Buffer.d.ts +27 -11
  13. package/dist/src/Buffer.d.ts.map +1 -1
  14. package/dist/src/Cache.d.ts +9 -4
  15. package/dist/src/Cache.d.ts.map +1 -1
  16. package/dist/src/Cache.js +8 -3
  17. package/dist/src/Callbacks.d.ts +30 -19
  18. package/dist/src/Callbacks.d.ts.map +1 -1
  19. package/dist/src/Console.d.ts +145 -66
  20. package/dist/src/Console.d.ts.map +1 -1
  21. package/dist/src/Console.js +91 -43
  22. package/dist/src/Crypto.d.ts +85 -42
  23. package/dist/src/Crypto.d.ts.map +1 -1
  24. package/dist/src/Crypto.js +44 -20
  25. package/dist/src/Eq.d.ts +87 -21
  26. package/dist/src/Eq.d.ts.map +1 -1
  27. package/dist/src/Eq.js +67 -19
  28. package/dist/src/Error.d.ts +3 -3
  29. package/dist/src/Error.d.ts.map +1 -1
  30. package/dist/src/Function.d.ts +119 -58
  31. package/dist/src/Function.d.ts.map +1 -1
  32. package/dist/src/Function.js +86 -43
  33. package/dist/src/Http.d.ts +394 -0
  34. package/dist/src/Http.d.ts.map +1 -0
  35. package/dist/src/Http.js +102 -0
  36. package/dist/src/Identicon.d.ts +16 -7
  37. package/dist/src/Identicon.d.ts.map +1 -1
  38. package/dist/src/Identicon.js +14 -5
  39. package/dist/src/LeakDetector.d.ts +88 -0
  40. package/dist/src/LeakDetector.d.ts.map +1 -0
  41. package/dist/src/LeakDetector.js +86 -0
  42. package/dist/src/LockManager.d.ts +18 -9
  43. package/dist/src/LockManager.d.ts.map +1 -1
  44. package/dist/src/LockManager.js +7 -8
  45. package/dist/src/Lookup.d.ts +43 -13
  46. package/dist/src/Lookup.d.ts.map +1 -1
  47. package/dist/src/Lookup.js +28 -8
  48. package/dist/src/Microtask.d.ts +1 -1
  49. package/dist/src/Microtask.d.ts.map +1 -1
  50. package/dist/src/Microtask.js +3 -7
  51. package/dist/src/Number.d.ts +49 -13
  52. package/dist/src/Number.d.ts.map +1 -1
  53. package/dist/src/Number.js +34 -5
  54. package/dist/src/Object.d.ts +160 -40
  55. package/dist/src/Object.d.ts.map +1 -1
  56. package/dist/src/Object.js +92 -46
  57. package/dist/src/Option.d.ts +10 -7
  58. package/dist/src/Option.d.ts.map +1 -1
  59. package/dist/src/Option.js +9 -6
  60. package/dist/src/Order.d.ts +21 -21
  61. package/dist/src/Order.d.ts.map +1 -1
  62. package/dist/src/Order.js +18 -21
  63. package/dist/src/Platform.d.ts +31 -0
  64. package/dist/src/Platform.d.ts.map +1 -1
  65. package/dist/src/Platform.js +98 -0
  66. package/dist/src/Random.d.ts +32 -17
  67. package/dist/src/Random.d.ts.map +1 -1
  68. package/dist/src/Random.js +12 -8
  69. package/dist/src/Redacted.d.ts +43 -29
  70. package/dist/src/Redacted.d.ts.map +1 -1
  71. package/dist/src/Redacted.js +14 -3
  72. package/dist/src/Ref.d.ts +11 -3
  73. package/dist/src/Ref.d.ts.map +1 -1
  74. package/dist/src/Ref.js +2 -2
  75. package/dist/src/RefCount.d.ts +2 -6
  76. package/dist/src/RefCount.d.ts.map +1 -1
  77. package/dist/src/RefCount.js +33 -90
  78. package/dist/src/Relation.d.ts +70 -5
  79. package/dist/src/Relation.d.ts.map +1 -1
  80. package/dist/src/Relation.js +77 -2
  81. package/dist/src/Resource.d.ts +590 -155
  82. package/dist/src/Resource.d.ts.map +1 -1
  83. package/dist/src/Resource.js +592 -343
  84. package/dist/src/Result.d.ts +838 -191
  85. package/dist/src/Result.d.ts.map +1 -1
  86. package/dist/src/Result.js +163 -102
  87. package/dist/src/Schedule.d.ts +684 -231
  88. package/dist/src/Schedule.d.ts.map +1 -1
  89. package/dist/src/Schedule.js +766 -295
  90. package/dist/src/Set.d.ts +89 -40
  91. package/dist/src/Set.d.ts.map +1 -1
  92. package/dist/src/Set.js +50 -29
  93. package/dist/src/Sqlite.d.ts +29 -72
  94. package/dist/src/Sqlite.d.ts.map +1 -1
  95. package/dist/src/Sqlite.js +39 -45
  96. package/dist/src/StackTrace.d.ts +40 -0
  97. package/dist/src/StackTrace.d.ts.map +1 -0
  98. package/dist/src/StackTrace.js +140 -0
  99. package/dist/src/Store.d.ts +5 -4
  100. package/dist/src/Store.d.ts.map +1 -1
  101. package/dist/src/Store.js +5 -4
  102. package/dist/src/String.d.ts +7 -1
  103. package/dist/src/String.d.ts.map +1 -1
  104. package/dist/src/String.js +20 -7
  105. package/dist/src/Task.d.ts +3428 -1629
  106. package/dist/src/Task.d.ts.map +1 -1
  107. package/dist/src/Task.js +1876 -1279
  108. package/dist/src/Test.d.ts +17 -124
  109. package/dist/src/Test.d.ts.map +1 -1
  110. package/dist/src/Test.js +17 -68
  111. package/dist/src/Time.d.ts +108 -49
  112. package/dist/src/Time.d.ts.map +1 -1
  113. package/dist/src/Time.js +172 -73
  114. package/dist/src/Type.d.ts +3542 -1926
  115. package/dist/src/Type.d.ts.map +1 -1
  116. package/dist/src/Type.js +3070 -1784
  117. package/dist/src/Types.d.ts +200 -100
  118. package/dist/src/Types.d.ts.map +1 -1
  119. package/dist/src/Types.js +39 -8
  120. package/dist/src/WebSocket.d.ts +48 -23
  121. package/dist/src/WebSocket.d.ts.map +1 -1
  122. package/dist/src/WebSocket.js +15 -6
  123. package/dist/src/Worker.d.ts +94 -36
  124. package/dist/src/Worker.d.ts.map +1 -1
  125. package/dist/src/Worker.js +58 -24
  126. package/dist/src/index.d.ts +55 -52
  127. package/dist/src/index.d.ts.map +1 -1
  128. package/dist/src/index.js +3 -1
  129. package/dist/src/intl/cs.d.ts +94 -0
  130. package/dist/src/intl/cs.d.ts.map +1 -0
  131. package/dist/src/intl/cs.js +220 -0
  132. package/dist/src/intl/index.d.ts +7 -0
  133. package/dist/src/intl/index.d.ts.map +1 -0
  134. package/dist/src/intl/index.js +6 -0
  135. package/dist/src/local-first/Db.d.ts +12 -8
  136. package/dist/src/local-first/Db.d.ts.map +1 -1
  137. package/dist/src/local-first/Db.js +107 -130
  138. package/dist/src/local-first/Error.d.ts +4 -4
  139. package/dist/src/local-first/Evolu.d.ts +338 -92
  140. package/dist/src/local-first/Evolu.d.ts.map +1 -1
  141. package/dist/src/local-first/Evolu.js +47 -55
  142. package/dist/src/local-first/LocalAuth.d.ts +4 -4
  143. package/dist/src/local-first/LocalAuth.d.ts.map +1 -1
  144. package/dist/src/local-first/Owner.d.ts +61 -24
  145. package/dist/src/local-first/Owner.d.ts.map +1 -1
  146. package/dist/src/local-first/Owner.js +45 -8
  147. package/dist/src/local-first/Protocol.d.ts +44 -31
  148. package/dist/src/local-first/Protocol.d.ts.map +1 -1
  149. package/dist/src/local-first/Protocol.js +36 -13
  150. package/dist/src/local-first/Query.d.ts +147 -62
  151. package/dist/src/local-first/Query.d.ts.map +1 -1
  152. package/dist/src/local-first/Query.js +112 -58
  153. package/dist/src/local-first/Relay.d.ts +44 -14
  154. package/dist/src/local-first/Relay.d.ts.map +1 -1
  155. package/dist/src/local-first/Schema.d.ts +92 -41
  156. package/dist/src/local-first/Schema.d.ts.map +1 -1
  157. package/dist/src/local-first/Schema.js +25 -8
  158. package/dist/src/local-first/Shared.d.ts +23 -21
  159. package/dist/src/local-first/Shared.d.ts.map +1 -1
  160. package/dist/src/local-first/Shared.js +85 -42
  161. package/dist/src/local-first/Storage.d.ts +53 -32
  162. package/dist/src/local-first/Storage.d.ts.map +1 -1
  163. package/dist/src/local-first/Storage.js +194 -234
  164. package/dist/src/local-first/Timestamp.d.ts +21 -21
  165. package/dist/src/local-first/Timestamp.d.ts.map +1 -1
  166. package/dist/src/local-first/Timestamp.js +2 -2
  167. package/dist/src/local-first/index.d.ts +11 -11
  168. package/package.json +14 -13
  169. package/src/Array.ts +306 -205
  170. package/src/Assert.ts +48 -68
  171. package/src/BigInt.ts +6 -3
  172. package/src/Brand.ts +27 -40
  173. package/src/Buffer.ts +28 -12
  174. package/src/Cache.ts +9 -4
  175. package/src/Callbacks.ts +30 -19
  176. package/src/Console.ts +156 -84
  177. package/src/Crypto.ts +83 -41
  178. package/src/Eq.ts +95 -22
  179. package/src/Error.ts +5 -2
  180. package/src/Function.ts +167 -59
  181. package/src/Http.ts +549 -0
  182. package/src/Identicon.ts +16 -7
  183. package/src/LeakDetector.ts +182 -0
  184. package/src/LockManager.ts +43 -39
  185. package/src/Lookup.ts +62 -28
  186. package/src/Microtask.ts +19 -23
  187. package/src/Number.ts +64 -12
  188. package/src/Object.ts +173 -49
  189. package/src/Option.ts +10 -7
  190. package/src/Order.ts +23 -21
  191. package/src/Platform.ts +136 -0
  192. package/src/Polyfills.ts +6 -12
  193. package/src/Random.ts +32 -19
  194. package/src/Redacted.ts +44 -30
  195. package/src/Ref.ts +14 -6
  196. package/src/RefCount.ts +38 -80
  197. package/src/Relation.ts +202 -10
  198. package/src/Resource.ts +1199 -591
  199. package/src/Result.ts +930 -271
  200. package/src/Schedule.ts +859 -343
  201. package/src/Set.ts +89 -40
  202. package/src/Sqlite.ts +142 -137
  203. package/src/StackTrace.ts +230 -0
  204. package/src/Store.ts +8 -7
  205. package/src/String.ts +22 -5
  206. package/src/Task.ts +5109 -2845
  207. package/src/Test.ts +20 -167
  208. package/src/Time.ts +293 -90
  209. package/src/Type.ts +10034 -4117
  210. package/src/Types.ts +227 -101
  211. package/src/WebSocket.ts +64 -36
  212. package/src/Worker.ts +180 -72
  213. package/src/index.ts +55 -52
  214. package/src/intl/cs.ts +388 -0
  215. package/src/intl/index.ts +7 -0
  216. package/src/local-first/Db.ts +161 -190
  217. package/src/local-first/Error.ts +4 -4
  218. package/src/local-first/Evolu.ts +495 -250
  219. package/src/local-first/LocalAuth.ts +6 -12
  220. package/src/local-first/Owner.ts +58 -21
  221. package/src/local-first/Protocol.ts +49 -31
  222. package/src/local-first/Query.ts +146 -61
  223. package/src/local-first/Relay.ts +51 -21
  224. package/src/local-first/Schema.ts +109 -51
  225. package/src/local-first/Shared.ts +253 -214
  226. package/src/local-first/Storage.ts +266 -264
  227. package/src/local-first/Timestamp.ts +25 -18
  228. package/src/local-first/index.ts +11 -11
  229. package/dist/src/Skiplist.d.ts +0 -28
  230. package/dist/src/Skiplist.d.ts.map +0 -1
  231. package/dist/src/Skiplist.js +0 -64
  232. package/dist/src/Tracer.d.ts +0 -48
  233. package/dist/src/Tracer.d.ts.map +0 -1
  234. package/dist/src/Tracer.js +0 -6
  235. package/src/Skiplist.ts +0 -108
  236. package/src/Tracer.ts +0 -54
package/dist/src/Eq.d.ts CHANGED
@@ -3,22 +3,70 @@
3
3
  *
4
4
  * @module
5
5
  */
6
- import type { Order } from "./Order.js";
7
- import type { JsonValue, JsonValueInput } from "./Type.js";
6
+ import type { Order } from "./Order.ts";
7
+ import type { JsonValue, JsonValueInput } from "./Type.ts";
8
8
  /**
9
9
  * Compares two values of the same type `A` for equality.
10
10
  *
11
11
  * Equality functions start with an 'eq' prefix, e.g., `eqString`.
12
12
  *
13
- * TODO: Explain, examples (composition etc.)
13
+ * An `Eq` must define an equivalence relation over its intended domain:
14
+ *
15
+ * - **Reflexive**: `eq(a, a)` is `true`.
16
+ * - **Symmetric**: `eq(a, b)` equals `eq(b, a)`.
17
+ * - **Transitive**: if `eq(a, b)` and `eq(b, c)` are `true`, then `eq(a, c)` is
18
+ * `true`.
19
+ *
20
+ * Use {@link eqFromOrder} to derive equality from an {@link Order}.
21
+ *
22
+ * ### Example
23
+ *
24
+ * ```ts
25
+ * import { createEqObject, eqNumber } from "@evolu/common";
26
+ *
27
+ * const eqPoint = createEqObject({ x: eqNumber, y: eqNumber });
28
+ *
29
+ * expect(eqPoint({ x: 1, y: 2 }, { x: 1, y: 2 })).toBe(true);
30
+ * expect(eqPoint({ x: 1, y: 2 }, { x: 2, y: 1 })).toBe(false);
31
+ * ```
14
32
  */
15
33
  export type Eq<in A> = (x: A, y: A) => boolean;
34
+ /**
35
+ * Compares two values with strict equality (`===`).
36
+ *
37
+ * Strict equality considers `NaN` unequal to itself. Use {@link eqSameValueZero}
38
+ * when values can contain `NaN` and equality must be reflexive.
39
+ */
16
40
  export declare const eqStrict: <A>(x: A, y: A) => boolean;
41
+ /**
42
+ * Compares two values using SameValueZero equality.
43
+ *
44
+ * SameValueZero is the standard equality algorithm used by `Map`, `Set`, and
45
+ * `Array.prototype.includes`. It behaves like strict equality except that `NaN`
46
+ * equals itself. Both algorithms consider `0` and `-0` equal.
47
+ *
48
+ * ### Example
49
+ *
50
+ * ```ts
51
+ * import { eqSameValueZero } from "@evolu/common";
52
+ *
53
+ * expect(eqSameValueZero(NaN, NaN)).toBe(true);
54
+ * expect(eqSameValueZero(0, -0)).toBe(true);
55
+ * expect(eqSameValueZero({}, {})).toBe(false);
56
+ * ```
57
+ */
58
+ export declare const eqSameValueZero: <A>(x: A, y: A) => boolean;
59
+ /** An {@link Eq} for strings using strict equality. */
17
60
  export declare const eqString: Eq<string>;
61
+ /** An {@link Eq} for numbers using {@link eqSameValueZero}. */
18
62
  export declare const eqNumber: Eq<number>;
63
+ /** An {@link Eq} for bigints using strict equality. */
19
64
  export declare const eqBigInt: Eq<bigint>;
65
+ /** An {@link Eq} for booleans using strict equality. */
20
66
  export declare const eqBoolean: Eq<boolean>;
67
+ /** An {@link Eq} for `undefined`. */
21
68
  export declare const eqUndefined: Eq<undefined>;
69
+ /** An {@link Eq} for `null`. */
22
70
  export declare const eqNull: Eq<null>;
23
71
  /** Derives an {@link Eq} from an {@link Order}. */
24
72
  export declare const eqFromOrder: <A>(order: Order<A>) => Eq<A>;
@@ -29,10 +77,15 @@ export declare const eqFromOrder: <A>(order: Order<A>) => Eq<A>;
29
77
  * ### Example
30
78
  *
31
79
  * ```ts
80
+ * import { createEqArrayLike, eqNumber } from "@evolu/common";
81
+ *
32
82
  * const eqArrayNumber = createEqArrayLike(eqNumber);
33
- * eqArrayNumber([1, 2, 3], [1, 2, 3]); // true (works with regular arrays)
34
- * eqArrayNumber(new Uint8Array([1, 2, 3]), new Uint8Array([1, 2, 3])); // true (works with Uint8Array)
35
- * eqArrayNumber([1, 2, 3], [1, 2, 4]); // false
83
+ *
84
+ * expect(eqArrayNumber([1, 2, 3], [1, 2, 3])).toBe(true);
85
+ * expect(
86
+ * eqArrayNumber(new Uint8Array([1, 2, 3]), new Uint8Array([1, 2, 3])),
87
+ * ).toBe(true);
88
+ * expect(eqArrayNumber([1, 2, 3], [1, 2, 4])).toBe(false);
36
89
  * ```
37
90
  */
38
91
  export declare const createEqArrayLike: <A>(item: Eq<A>) => Eq<ArrayLike<A>>;
@@ -44,10 +97,12 @@ export declare const createEqArrayLike: <A>(item: Eq<A>) => Eq<ArrayLike<A>>;
44
97
  * ### Example
45
98
  *
46
99
  * ```ts
100
+ * import { eqArrayStrict } from "@evolu/common";
101
+ *
47
102
  * const a = { x: 1 };
48
103
  * const b = { x: 1 };
49
- * eqArrayStrict([a, a], [a, a]); // true (same references)
50
- * eqArrayStrict([a], [b]); // false (different references, even if equal values)
104
+ * expect(eqArrayStrict([a, a], [a, a])).toBe(true);
105
+ * expect(eqArrayStrict([a], [b])).toBe(false);
51
106
  * ```
52
107
  */
53
108
  export declare const eqArrayStrict: <A>(x: ArrayLike<A>, y: ArrayLike<A>) => boolean;
@@ -57,9 +112,9 @@ export declare const eqArrayStrict: <A>(x: ArrayLike<A>, y: ArrayLike<A>) => boo
57
112
  * ### Example
58
113
  *
59
114
  * ```ts
60
- * eqArrayNumber([1, 2, 3], [1, 2, 3]); // true (works with regular arrays)
61
- * eqArrayNumber(new Uint8Array([1, 2, 3]), new Uint8Array([1, 2, 3])); // true (works with Uint8Array)
62
- * eqArrayNumber([1, 2, 3], [1, 2, 4]); // false
115
+ * import { eqArrayNumber } from "@evolu/common";
116
+ *
117
+ * expect(eqArrayNumber([1, NaN], [1, NaN])).toBe(true);
63
118
  * ```
64
119
  */
65
120
  export declare const eqArrayNumber: Eq<ArrayLike<number>>;
@@ -70,9 +125,12 @@ export declare const eqArrayNumber: Eq<ArrayLike<number>>;
70
125
  * ### Example
71
126
  *
72
127
  * ```ts
128
+ * import { createEqObject, eqNumber } from "@evolu/common";
129
+ *
73
130
  * const eqObjectNumber = createEqObject({ a: eqNumber });
74
- * eqObjectNumber({ a: 1 }, { a: 1 }); // true
75
- * eqObjectNumber({ a: 1 }, { a: 2 }); // false
131
+ *
132
+ * expect(eqObjectNumber({ a: 1 }, { a: 1 })).toBe(true);
133
+ * expect(eqObjectNumber({ a: 1 }, { a: 2 })).toBe(false);
76
134
  * ```
77
135
  */
78
136
  export declare const createEqObject: <A>(eqs: { [K in keyof A]: Eq<A[K]>; }) => Eq<{ readonly [K in keyof A]: A[K]; }>;
@@ -81,7 +139,8 @@ export declare const createEqObject: <A>(eqs: { [K in keyof A]: Eq<A[K]>; }) =>
81
139
  *
82
140
  * - Uses an iterative approach with a stack to handle large or deeply nested
83
141
  * objects without risking stack overflow.
84
- * - Handles circular references with a WeakMap to prevent infinite loops.
142
+ * - Defensively handles circular references in runtime values without looping,
143
+ * although cyclic values are not valid JSON.
85
144
  * - Unlike JSON.stringify, this function directly compares values, avoiding
86
145
  * serialization overhead and leveraging short-circuit evaluation for faster
87
146
  * failure on mismatched structures.
@@ -89,9 +148,12 @@ export declare const createEqObject: <A>(eqs: { [K in keyof A]: Eq<A[K]>; }) =>
89
148
  * ### Example
90
149
  *
91
150
  * ```ts
92
- * const obj1: Json = { name: "Alice", hobbies: ["reading", "hiking"] };
93
- * const obj2: Json = { name: "Alice", hobbies: ["reading", "hiking"] };
94
- * console.log(eqJson(obj1, obj2)); // true
151
+ * import { eqJsonValue, type JsonValue } from "@evolu/common";
152
+ *
153
+ * const first: JsonValue = { profile: { name: "Ada" } };
154
+ * const second: JsonValue = { profile: { name: "Ada" } };
155
+ *
156
+ * expect(eqJsonValue(first, second)).toBe(true);
95
157
  * ```
96
158
  */
97
159
  export declare const eqJsonValue: (a: JsonValue, b: JsonValue) => boolean;
@@ -100,7 +162,8 @@ export declare const eqJsonValue: (a: JsonValue, b: JsonValue) => boolean;
100
162
  *
101
163
  * - Uses an iterative approach with a stack to handle large or deeply nested
102
164
  * objects without risking stack overflow.
103
- * - Handles circular references with a WeakMap to prevent infinite loops.
165
+ * - Defensively handles circular references in runtime values without looping,
166
+ * although cyclic values are not valid JSON.
104
167
  * - Unlike JSON.stringify, this function directly compares values, avoiding
105
168
  * serialization overhead and leveraging short-circuit evaluation for faster
106
169
  * failure on mismatched structures.
@@ -108,9 +171,12 @@ export declare const eqJsonValue: (a: JsonValue, b: JsonValue) => boolean;
108
171
  * ### Example
109
172
  *
110
173
  * ```ts
111
- * const obj1: Json = { name: "Alice", hobbies: ["reading", "hiking"] };
112
- * const obj2: Json = { name: "Alice", hobbies: ["reading", "hiking"] };
113
- * console.log(eqJson(obj1, obj2)); // true
174
+ * import { eqJsonValueInput, type JsonValueInput } from "@evolu/common";
175
+ *
176
+ * const first: JsonValueInput = { profile: { name: "Ada" } };
177
+ * const second: JsonValueInput = { profile: { name: "Ada" } };
178
+ *
179
+ * expect(eqJsonValueInput(first, second)).toBe(true);
114
180
  * ```
115
181
  */
116
182
  export declare const eqJsonValueInput: (a: JsonValueInput, b: JsonValueInput) => boolean;
@@ -1 +1 @@
1
- {"version":3,"file":"Eq.d.ts","sourceRoot":"","sources":["../../src/Eq.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,KAAK,EAGV,SAAS,EACT,cAAc,EACf,MAAM,WAAW,CAAC;AAEn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,CAAY,CAAC;AAEnD,gCAAgC;AAChC,eAAO,MAAM,MAAM,EAAE,EAAE,CAAC,IAAI,CAAY,CAAC;AAEzC,mDAAmD;AACnD,eAAO,MAAM,WAAW,GACrB,CAAC,SAAS,KAAK,CAAC,CAAC,CAAC,KAAG,EAAE,CAAC,CAAC,CAEP,CAAC;AAEtB;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,iBAAiB,GAC3B,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,KAAG,EAAE,CAAC,SAAS,CAAC,CAAC,CAAC,CAUhC,CAAC;AAEJ;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,aAAa,GA7FD,CAAC,uCARa,OAqG+B,CAAC;AAEvE;;;;;;;;;;GAUG;AACH,eAAO,MAAM,aAAa,uBAA4C,CAAC;AAEvE;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,cAAc,GACxB,CAAC,OAAO,GAAG,CAAC,IAAI,MAAM,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAE,KAAG,EAAE,CAAC,EACzC,QAAQ,EAAE,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAC9B,CASA,CAAC;AAEJ;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,eAAO,MAAM,WAAW,MAAO,SAAS,KAAK,SAAS,KAAG,OAsExD,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,eAAO,MAAM,gBAAgB,MACxB,cAAc,KACd,cAAc,KAChB,OAAsD,CAAC"}
package/dist/src/Eq.js CHANGED
@@ -3,12 +3,42 @@
3
3
  *
4
4
  * @module
5
5
  */
6
+ /**
7
+ * Compares two values with strict equality (`===`).
8
+ *
9
+ * Strict equality considers `NaN` unequal to itself. Use {@link eqSameValueZero}
10
+ * when values can contain `NaN` and equality must be reflexive.
11
+ */
6
12
  export const eqStrict = (x, y) => x === y;
13
+ /**
14
+ * Compares two values using SameValueZero equality.
15
+ *
16
+ * SameValueZero is the standard equality algorithm used by `Map`, `Set`, and
17
+ * `Array.prototype.includes`. It behaves like strict equality except that `NaN`
18
+ * equals itself. Both algorithms consider `0` and `-0` equal.
19
+ *
20
+ * ### Example
21
+ *
22
+ * ```ts
23
+ * import { eqSameValueZero } from "@evolu/common";
24
+ *
25
+ * expect(eqSameValueZero(NaN, NaN)).toBe(true);
26
+ * expect(eqSameValueZero(0, -0)).toBe(true);
27
+ * expect(eqSameValueZero({}, {})).toBe(false);
28
+ * ```
29
+ */
30
+ export const eqSameValueZero = (x, y) => x === y || Object.is(x, y);
31
+ /** An {@link Eq} for strings using strict equality. */
7
32
  export const eqString = eqStrict;
8
- export const eqNumber = eqStrict;
33
+ /** An {@link Eq} for numbers using {@link eqSameValueZero}. */
34
+ export const eqNumber = eqSameValueZero;
35
+ /** An {@link Eq} for bigints using strict equality. */
9
36
  export const eqBigInt = eqStrict;
37
+ /** An {@link Eq} for booleans using strict equality. */
10
38
  export const eqBoolean = eqStrict;
39
+ /** An {@link Eq} for `undefined`. */
11
40
  export const eqUndefined = eqStrict;
41
+ /** An {@link Eq} for `null`. */
12
42
  export const eqNull = eqStrict;
13
43
  /** Derives an {@link Eq} from an {@link Order}. */
14
44
  export const eqFromOrder = (order) => (x, y) => order(x, y) === 0;
@@ -19,10 +49,15 @@ export const eqFromOrder = (order) => (x, y) => order(x, y) === 0;
19
49
  * ### Example
20
50
  *
21
51
  * ```ts
52
+ * import { createEqArrayLike, eqNumber } from "@evolu/common";
53
+ *
22
54
  * const eqArrayNumber = createEqArrayLike(eqNumber);
23
- * eqArrayNumber([1, 2, 3], [1, 2, 3]); // true (works with regular arrays)
24
- * eqArrayNumber(new Uint8Array([1, 2, 3]), new Uint8Array([1, 2, 3])); // true (works with Uint8Array)
25
- * eqArrayNumber([1, 2, 3], [1, 2, 4]); // false
55
+ *
56
+ * expect(eqArrayNumber([1, 2, 3], [1, 2, 3])).toBe(true);
57
+ * expect(
58
+ * eqArrayNumber(new Uint8Array([1, 2, 3]), new Uint8Array([1, 2, 3])),
59
+ * ).toBe(true);
60
+ * expect(eqArrayNumber([1, 2, 3], [1, 2, 4])).toBe(false);
26
61
  * ```
27
62
  */
28
63
  export const createEqArrayLike = (item) => (x, y) => {
@@ -44,10 +79,12 @@ export const createEqArrayLike = (item) => (x, y) => {
44
79
  * ### Example
45
80
  *
46
81
  * ```ts
82
+ * import { eqArrayStrict } from "@evolu/common";
83
+ *
47
84
  * const a = { x: 1 };
48
85
  * const b = { x: 1 };
49
- * eqArrayStrict([a, a], [a, a]); // true (same references)
50
- * eqArrayStrict([a], [b]); // false (different references, even if equal values)
86
+ * expect(eqArrayStrict([a, a], [a, a])).toBe(true);
87
+ * expect(eqArrayStrict([a], [b])).toBe(false);
51
88
  * ```
52
89
  */
53
90
  export const eqArrayStrict = /*#__PURE__*/ createEqArrayLike(eqStrict);
@@ -57,9 +94,9 @@ export const eqArrayStrict = /*#__PURE__*/ createEqArrayLike(eqStrict);
57
94
  * ### Example
58
95
  *
59
96
  * ```ts
60
- * eqArrayNumber([1, 2, 3], [1, 2, 3]); // true (works with regular arrays)
61
- * eqArrayNumber(new Uint8Array([1, 2, 3]), new Uint8Array([1, 2, 3])); // true (works with Uint8Array)
62
- * eqArrayNumber([1, 2, 3], [1, 2, 4]); // false
97
+ * import { eqArrayNumber } from "@evolu/common";
98
+ *
99
+ * expect(eqArrayNumber([1, NaN], [1, NaN])).toBe(true);
63
100
  * ```
64
101
  */
65
102
  export const eqArrayNumber = /*#__PURE__*/ createEqArrayLike(eqNumber);
@@ -70,9 +107,12 @@ export const eqArrayNumber = /*#__PURE__*/ createEqArrayLike(eqNumber);
70
107
  * ### Example
71
108
  *
72
109
  * ```ts
110
+ * import { createEqObject, eqNumber } from "@evolu/common";
111
+ *
73
112
  * const eqObjectNumber = createEqObject({ a: eqNumber });
74
- * eqObjectNumber({ a: 1 }, { a: 1 }); // true
75
- * eqObjectNumber({ a: 1 }, { a: 2 }); // false
113
+ *
114
+ * expect(eqObjectNumber({ a: 1 }, { a: 1 })).toBe(true);
115
+ * expect(eqObjectNumber({ a: 1 }, { a: 2 })).toBe(false);
76
116
  * ```
77
117
  */
78
118
  export const createEqObject = (eqs) => (x, y) => {
@@ -90,7 +130,8 @@ export const createEqObject = (eqs) => (x, y) => {
90
130
  *
91
131
  * - Uses an iterative approach with a stack to handle large or deeply nested
92
132
  * objects without risking stack overflow.
93
- * - Handles circular references with a WeakMap to prevent infinite loops.
133
+ * - Defensively handles circular references in runtime values without looping,
134
+ * although cyclic values are not valid JSON.
94
135
  * - Unlike JSON.stringify, this function directly compares values, avoiding
95
136
  * serialization overhead and leveraging short-circuit evaluation for faster
96
137
  * failure on mismatched structures.
@@ -98,9 +139,12 @@ export const createEqObject = (eqs) => (x, y) => {
98
139
  * ### Example
99
140
  *
100
141
  * ```ts
101
- * const obj1: Json = { name: "Alice", hobbies: ["reading", "hiking"] };
102
- * const obj2: Json = { name: "Alice", hobbies: ["reading", "hiking"] };
103
- * console.log(eqJson(obj1, obj2)); // true
142
+ * import { eqJsonValue, type JsonValue } from "@evolu/common";
143
+ *
144
+ * const first: JsonValue = { profile: { name: "Ada" } };
145
+ * const second: JsonValue = { profile: { name: "Ada" } };
146
+ *
147
+ * expect(eqJsonValue(first, second)).toBe(true);
104
148
  * ```
105
149
  */
106
150
  export const eqJsonValue = (a, b) => {
@@ -171,7 +215,8 @@ export const eqJsonValue = (a, b) => {
171
215
  *
172
216
  * - Uses an iterative approach with a stack to handle large or deeply nested
173
217
  * objects without risking stack overflow.
174
- * - Handles circular references with a WeakMap to prevent infinite loops.
218
+ * - Defensively handles circular references in runtime values without looping,
219
+ * although cyclic values are not valid JSON.
175
220
  * - Unlike JSON.stringify, this function directly compares values, avoiding
176
221
  * serialization overhead and leveraging short-circuit evaluation for faster
177
222
  * failure on mismatched structures.
@@ -179,9 +224,12 @@ export const eqJsonValue = (a, b) => {
179
224
  * ### Example
180
225
  *
181
226
  * ```ts
182
- * const obj1: Json = { name: "Alice", hobbies: ["reading", "hiking"] };
183
- * const obj2: Json = { name: "Alice", hobbies: ["reading", "hiking"] };
184
- * console.log(eqJson(obj1, obj2)); // true
227
+ * import { eqJsonValueInput, type JsonValueInput } from "@evolu/common";
228
+ *
229
+ * const first: JsonValueInput = { profile: { name: "Ada" } };
230
+ * const second: JsonValueInput = { profile: { name: "Ada" } };
231
+ *
232
+ * expect(eqJsonValueInput(first, second)).toBe(true);
185
233
  * ```
186
234
  */
187
235
  export const eqJsonValueInput = (a, b) => eqJsonValue(a, b);
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * @module
5
5
  */
6
- import { type InferType } from "./Type.js";
6
+ import { type InferType, type TypedType, Unknown } from "./Type.ts";
7
7
  /**
8
8
  * A wrapper for unknown errors caught at runtime.
9
9
  *
@@ -16,8 +16,8 @@ import { type InferType } from "./Type.js";
16
16
  *
17
17
  * Use {@link createUnknownError} to create instances.
18
18
  */
19
- export declare const UnknownError: import("./Type.js").TypedType<"UnknownError", {
20
- error: import("./Type.js").Type<"Unknown", unknown, unknown, never, unknown, never>;
19
+ export declare const UnknownError: TypedType<"UnknownError", {
20
+ readonly error: typeof Unknown;
21
21
  }>;
22
22
  export interface UnknownError extends InferType<typeof UnknownError> {
23
23
  }
@@ -1 +1 @@
1
- {"version":3,"file":"Error.d.ts","sourceRoot":"","sources":["../../src/Error.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,KAAK,SAAS,EAAkB,MAAM,WAAW,CAAC;AAE3D;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,YAAY;;EAEvB,CAAC;AACH,MAAM,WAAW,YAAa,SAAQ,SAAS,CAAC,OAAO,YAAY,CAAC;CAAG;AAEvE;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,GAAI,OAAO,OAAO,KAAG,YAkDnD,CAAC"}
1
+ {"version":3,"file":"Error.d.ts","sourceRoot":"","sources":["../../src/Error.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,KAAK,SAAS,EAAS,KAAK,SAAS,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAE3E;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,YAAY,EAAE,SAAS,CAClC,cAAc,EACd;IAAE,QAAQ,CAAC,KAAK,EAAE,OAAO,OAAO,CAAA;CAAE,CAGlC,CAAC;AACH,MAAM,WAAW,YAAa,SAAQ,SAAS,CAAC,OAAO,YAAY,CAAC;CAAG;AAEvE;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,UAAW,OAAO,KAAG,YAkDnD,CAAC"}
@@ -10,32 +10,36 @@
10
10
  * ### Example
11
11
  *
12
12
  * ```ts
13
+ * import { exhaustiveCheck } from "@evolu/common";
14
+ *
13
15
  * type Color = "red" | "green" | "blue";
16
+ * const handled: Array<string> = [];
14
17
  *
15
18
  * const handleColor = (color: Color): void => {
16
19
  * switch (color) {
17
20
  * case "red":
18
- * console.log("Handling red");
21
+ * handled.push("Handling red");
19
22
  * break;
20
23
  * case "green":
21
- * console.log("Handling green");
24
+ * handled.push("Handling green");
22
25
  * break;
23
26
  * case "blue":
24
- * console.log("Handling blue");
27
+ * handled.push("Handling blue");
25
28
  * break;
26
29
  * default:
27
- * exhaustiveCheck(color); // Ensures all cases are handled
30
+ * exhaustiveCheck(color);
28
31
  * }
29
32
  * };
33
+ *
34
+ * handleColor("blue");
35
+ * expect(handled).toEqual(["Handling blue"]);
30
36
  * ```
31
37
  *
32
38
  * Use this primarily in side-effect switches (`void` branches). For
33
39
  * value-producing switches, TypeScript can enforce exhaustiveness without a
34
- * `default` branch.
35
- *
36
- * ### Example
40
+ * `default` branch in either of the following styles.
37
41
  *
38
- * Return from each case for value-producing switches.
42
+ * ### Return from every case
39
43
  *
40
44
  * ```ts
41
45
  * type Color = "red" | "green" | "blue";
@@ -50,11 +54,11 @@
50
54
  * return "#0000ff";
51
55
  * }
52
56
  * };
53
- * ```
54
57
  *
55
- * ### Example
58
+ * expect(colorToHex("green")).toBe("#00ff00");
59
+ * ```
56
60
  *
57
- * Use assignment + no `default` to get exhaustiveness by definite assignment.
61
+ * ### Assign in every case
58
62
  *
59
63
  * ```ts
60
64
  * type Input =
@@ -62,7 +66,7 @@
62
66
  * | { readonly type: "Query" }
63
67
  * | { readonly type: "Export" };
64
68
  *
65
- * const onInput = (input: Input): void => {
69
+ * const inputToKind = (input: Input): "A" | "B" | "C" => {
66
70
  * let result: "A" | "B" | "C";
67
71
  *
68
72
  * switch (input.type) {
@@ -77,12 +81,10 @@
77
81
  * break;
78
82
  * }
79
83
  *
80
- * handleKind(result);
84
+ * return result;
81
85
  * };
82
86
  *
83
- * const handleKind = (kind: "A" | "B" | "C"): void => {
84
- * console.log(kind);
85
- * };
87
+ * expect(inputToKind({ type: "Query" })).toBe("B");
86
88
  * ```
87
89
  */
88
90
  export declare const exhaustiveCheck: (value: never) => never;
@@ -95,71 +97,128 @@ export declare const exhaustiveCheck: (value: never) => never;
95
97
  * ### Example
96
98
  *
97
99
  * ```ts
98
- * const values = [1, 2, 3];
99
- * const same = values.map(identity); // [1, 2, 3]
100
+ * import { identity } from "@evolu/common";
100
101
  *
102
+ * const values = [1, 2, 3];
103
+ * const object = { value: 1 };
101
104
  * const getTransform = (shouldDouble: boolean) =>
102
- * shouldDouble ? (x: number) => x * 2 : identity;
105
+ * shouldDouble ? (value: number) => value * 2 : identity;
106
+ *
107
+ * expect(values.map(identity)).toEqual([1, 2, 3]);
108
+ * expect(identity(object)).toBe(object);
109
+ * expect(getTransform(false)(2)).toBe(2);
103
110
  * ```
104
111
  */
105
112
  export declare const identity: <A>(a: A) => A;
106
113
  /**
107
- * A function that takes no arguments and returns a value of type T. Also known
108
- * as a thunk.
114
+ * Creates an object that follows JavaScript disposal semantics.
115
+ *
116
+ * The first argument is the object to make disposable. The returned object gets
117
+ * a disposal method and its functions are wrapped with a disposal guard. This
118
+ * is the JavaScript equivalent of .NET `ObjectDisposedException`: once an
119
+ * object has been disposed, calling its methods is a programmer error and
120
+ * should throw immediately instead of continuing with invalid state. Evolu
121
+ * asserts this invariant with the "Cannot use a disposed object." message.
122
+ *
123
+ * The second argument is an optional disposer. When provided, it is
124
+ * [moved](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DisposableStack/move)
125
+ * into the returned object, and the returned object's disposal method disposes
126
+ * it. Omit it when the object has no cleanup resources but still must become
127
+ * unusable after disposal, such as with reference count helpers where disposal
128
+ * enforces correct ownership tracking.
129
+ *
130
+ * ### Example
131
+ *
132
+ * ```ts
133
+ * import { disposable } from "@evolu/common";
134
+ *
135
+ * let cleaned = false;
136
+ * const createResource = () => {
137
+ * using disposer = new DisposableStack();
138
+ * disposer.defer(() => {
139
+ * cleaned = true;
140
+ * });
141
+ * return disposable({ read: () => "ready" }, disposer);
142
+ * };
143
+ *
144
+ * const resource = createResource();
145
+ * expect(resource.read()).toBe("ready");
146
+ * resource[Symbol.dispose]();
147
+ *
148
+ * expect(cleaned).toBe(true);
149
+ * expect(() => resource.read()).toThrow("Cannot use a disposed object.");
150
+ * ```
151
+ */
152
+ export declare function disposable<T extends object>(value: T extends Disposable ? Omit<T, typeof Symbol.dispose> : T, disposer?: DisposableStack): T extends Disposable ? T : T & Disposable;
153
+ export declare function disposable<T extends object>(value: T extends AsyncDisposable ? Omit<T, typeof Symbol.asyncDispose> : T, disposer: AsyncDisposableStack): T extends AsyncDisposable ? T : T & AsyncDisposable;
154
+ export declare const isDisposable: (value: unknown) => value is Disposable | AsyncDisposable;
155
+ /**
156
+ * A function that takes no arguments and returns a value.
109
157
  *
110
158
  * Useful for:
111
159
  *
112
- * - Providing default callbacks (see {@link lazyVoid}, {@link lazyTrue}, etc.)
160
+ * - Providing default callbacks (see {@link constVoid}, {@link constTrue}, etc.)
113
161
  * - Delaying expensive operations until actually needed
114
162
  * - Deferring side effects so the callee controls when they run
115
163
  *
116
164
  * ### Example
117
165
  *
118
166
  * ```ts
119
- * // Default callback
120
- * const notify = (onDone: Lazy<void> = lazyVoid) => {
121
- * onDone();
122
- * };
167
+ * import { constVoid, type Thunk } from "@evolu/common";
123
168
  *
124
- * // Delay computation
125
- * const getData: Lazy<Data> = () => compute();
126
- * const data = getData();
169
+ * const notify = (onDone: Thunk<void> = constVoid) => onDone();
170
+ * notify();
127
171
  *
128
- * // Defer side effects
129
- * const schedule = (job: Lazy<void>) => {
130
- * queueMicrotask(job);
172
+ * let value = 0;
173
+ * const compute: Thunk<number> = () => ++value;
174
+ * const jobs: Array<Thunk<void>> = [];
175
+ * const schedule = (job: Thunk<void>): void => {
176
+ * jobs.push(job);
131
177
  * };
132
- * schedule(() => logMetric("loaded"));
178
+ * schedule(() => {
179
+ * value += 10;
180
+ * });
181
+ *
182
+ * const computed = compute();
183
+ * jobs.shift()?.();
184
+ * expect(computed).toBe(1);
185
+ * expect(value).toBe(11);
133
186
  * ```
134
187
  */
135
- export type Lazy<T> = () => T;
188
+ export type Thunk<T> = () => T;
136
189
  /**
137
- * Creates a {@link Lazy} from a precomputed value.
190
+ * Creates a {@link Thunk} that always returns a precomputed value.
138
191
  *
139
- * Use when the value is expensive to compute and want to compute it once at
192
+ * Use when the value is expensive to compute and you want to compute it once at
140
193
  * definition time rather than on every call.
141
194
  *
142
195
  * ### Example
143
196
  *
144
197
  * ```ts
145
- * // Computed once at definition, returned on every call
146
- * const getConfig = lazy(parseConfig(rawConfig));
198
+ * import { constant } from "@evolu/common";
199
+ *
200
+ * let version = 0;
201
+ * const readConfig = () => ({ version: ++version });
202
+ * const getConstantConfig = constant(readConfig());
203
+ * const getFreshConfig = () => readConfig();
147
204
  *
148
- * // vs. computed on every call
149
- * const getConfig = () => parseConfig(rawConfig);
205
+ * expect(getConstantConfig()).toBe(getConstantConfig());
206
+ * expect(getConstantConfig().version).toBe(1);
207
+ * expect(getFreshConfig().version).toBe(2);
208
+ * expect(getFreshConfig().version).toBe(3);
150
209
  * ```
151
210
  */
152
- export declare const lazy: <T>(value: T) => Lazy<T>;
153
- /** A {@link Lazy} that returns `true`. */
154
- export declare const lazyTrue: Lazy<true>;
155
- /** A {@link Lazy} that returns `false`. */
156
- export declare const lazyFalse: Lazy<false>;
157
- /** A {@link Lazy} that returns `null`. */
158
- export declare const lazyNull: Lazy<null>;
159
- /** A {@link Lazy} that returns `undefined`. */
160
- export declare const lazyUndefined: Lazy<undefined>;
161
- /** A {@link Lazy} that returns `undefined` for void callbacks. */
162
- export declare const lazyVoid: Lazy<void>;
211
+ export declare const constant: <T>(value: T) => Thunk<T>;
212
+ /** A {@link Thunk} that returns `true`. */
213
+ export declare const constTrue: Thunk<true>;
214
+ /** A {@link Thunk} that returns `false`. */
215
+ export declare const constFalse: Thunk<false>;
216
+ /** A {@link Thunk} that returns `null`. */
217
+ export declare const constNull: Thunk<null>;
218
+ /** A {@link Thunk} that returns `undefined`. */
219
+ export declare const constUndefined: Thunk<undefined>;
220
+ /** A {@link Thunk} that returns `undefined` for void callbacks. */
221
+ export declare const constVoid: Thunk<void>;
163
222
  /**
164
223
  * Development placeholder that always throws.
165
224
  *
@@ -170,17 +229,19 @@ export declare const lazyVoid: Lazy<void>;
170
229
  * ### Example
171
230
  *
172
231
  * ```ts
173
- * // Type inferred from return type annotation
174
- * const fetchUser = (id: UserId): Result<User, FetchError> => todo();
232
+ * import { todo } from "@evolu/common";
175
233
  *
176
- * expectTypeOf(fetchUser).returns.toEqualTypeOf<
177
- * Result<User, FetchError>
178
- * >();
234
+ * interface Config {
235
+ * readonly theme: string;
236
+ * }
179
237
  *
180
- * // Explicit generic when no return type
238
+ * const getCount = (): number => todo();
181
239
  * const getConfig = () => todo<Config>();
182
240
  *
183
- * expectTypeOf(getConfig).returns.toEqualTypeOf<Config>();
241
+ * expectTypeOf<
242
+ * [ReturnType<typeof getCount>, ReturnType<typeof getConfig>]
243
+ * >().toEqualTypeOf<[number, Config]>();
244
+ * expect(getCount).toThrow("not yet implemented");
184
245
  * ```
185
246
  */
186
247
  export declare const todo: <T>() => T;
@@ -1 +1 @@
1
- {"version":3,"file":"Function.d.ts","sourceRoot":"","sources":["../../src/Function.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiFG;AACH,eAAO,MAAM,eAAe,GAAI,OAAO,KAAK,KAAG,KAE9C,CAAC;AAEF;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,QAAQ,GAAI,CAAC,EAAE,GAAG,CAAC,KAAG,CAAM,CAAC;AAE1C;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAM,MAAM,IAAI,CAAC,CAAC,IAAI,MAAM,CAAC,CAAC;AAE9B;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,IAAI,GACd,CAAC,EAAE,OAAO,CAAC,KAAG,IAAI,CAAC,CAAC,CAEd,CAAC;AAEV,0CAA0C;AAC1C,eAAO,MAAM,QAAQ,EAAE,IAAI,CAAC,IAAI,CAA4B,CAAC;AAE7D,2CAA2C;AAC3C,eAAO,MAAM,SAAS,EAAE,IAAI,CAAC,KAAK,CAA6B,CAAC;AAEhE,0CAA0C;AAC1C,eAAO,MAAM,QAAQ,EAAE,IAAI,CAAC,IAAI,CAA4B,CAAC;AAE7D,+CAA+C;AAC/C,eAAO,MAAM,aAAa,EAAE,IAAI,CAAC,SAAS,CAAiC,CAAC;AAE5E,kEAAkE;AAClE,eAAO,MAAM,QAAQ,EAAE,IAAI,CAAC,IAAI,CAAiB,CAAC;AAElD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,eAAO,MAAM,IAAI,GAAI,CAAC,OAAK,CAE1B,CAAC"}
1
+ {"version":3,"file":"Function.d.ts","sourceRoot":"","sources":["../../src/Function.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAMH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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