@evolu/common 5.4.7 → 6.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 (242) hide show
  1. package/README.md +30 -34
  2. package/dist/src/Array.d.ts +17 -0
  3. package/dist/src/Array.d.ts.map +1 -0
  4. package/dist/src/Array.js +12 -0
  5. package/dist/src/Assert.d.ts +68 -0
  6. package/dist/src/Assert.d.ts.map +1 -0
  7. package/dist/src/Assert.js +77 -0
  8. package/dist/src/BigInt.d.ts +20 -0
  9. package/dist/src/BigInt.d.ts.map +1 -0
  10. package/dist/src/BigInt.js +18 -0
  11. package/dist/src/Buffer.d.ts +92 -0
  12. package/dist/src/Buffer.d.ts.map +1 -0
  13. package/dist/src/Buffer.js +62 -0
  14. package/dist/src/Callbacks.d.ts +20 -0
  15. package/dist/src/Callbacks.d.ts.map +1 -0
  16. package/dist/src/Callbacks.js +18 -0
  17. package/dist/src/Console.d.ts +78 -0
  18. package/dist/src/Console.d.ts.map +1 -0
  19. package/dist/src/Console.js +103 -0
  20. package/dist/src/Crypto.d.ts +72 -39
  21. package/dist/src/Crypto.d.ts.map +1 -1
  22. package/dist/src/Crypto.js +89 -54
  23. package/dist/src/Eq.d.ts +97 -0
  24. package/dist/src/Eq.d.ts.map +1 -0
  25. package/dist/src/Eq.js +167 -0
  26. package/dist/src/Error.d.ts +14 -10
  27. package/dist/src/Error.d.ts.map +1 -1
  28. package/dist/src/Error.js +43 -11
  29. package/dist/src/Evolu/Config.d.ts +69 -0
  30. package/dist/src/Evolu/Config.d.ts.map +1 -0
  31. package/dist/src/Evolu/Config.js +9 -0
  32. package/dist/src/Evolu/Db.d.ts +126 -0
  33. package/dist/src/Evolu/Db.d.ts.map +1 -0
  34. package/dist/src/Evolu/Db.js +774 -0
  35. package/dist/src/Evolu/Diff.d.ts +43 -0
  36. package/dist/src/Evolu/Diff.d.ts.map +1 -0
  37. package/dist/src/Evolu/Diff.js +95 -0
  38. package/dist/src/Evolu/Evolu.d.ts +334 -0
  39. package/dist/src/Evolu/Evolu.d.ts.map +1 -0
  40. package/dist/src/Evolu/Evolu.js +434 -0
  41. package/dist/src/Evolu/Internal.d.ts +26 -0
  42. package/dist/src/Evolu/Internal.d.ts.map +1 -0
  43. package/dist/src/Evolu/Internal.js +25 -0
  44. package/dist/src/Evolu/Kysely.d.ts +6 -0
  45. package/dist/src/Evolu/Kysely.d.ts.map +1 -0
  46. package/dist/src/Evolu/Kysely.js +21 -0
  47. package/dist/src/Evolu/Owner.d.ts +155 -0
  48. package/dist/src/Evolu/Owner.d.ts.map +1 -0
  49. package/dist/src/Evolu/Owner.js +126 -0
  50. package/dist/src/Evolu/Platform.d.ts +23 -0
  51. package/dist/src/Evolu/Platform.d.ts.map +1 -0
  52. package/dist/src/Evolu/Platform.js +1 -0
  53. package/dist/src/Evolu/Protocol.d.ts +401 -0
  54. package/dist/src/Evolu/Protocol.d.ts.map +1 -0
  55. package/dist/src/Evolu/Protocol.js +1151 -0
  56. package/dist/src/Evolu/Public.d.ts +18 -0
  57. package/dist/src/Evolu/Public.d.ts.map +1 -0
  58. package/dist/src/Evolu/Public.js +11 -0
  59. package/dist/src/Evolu/PublicKysely.d.ts +148 -0
  60. package/dist/src/Evolu/PublicKysely.d.ts.map +1 -0
  61. package/dist/src/Evolu/PublicKysely.js +185 -0
  62. package/dist/src/Evolu/Query.d.ts +63 -0
  63. package/dist/src/Evolu/Query.d.ts.map +1 -0
  64. package/dist/src/Evolu/Query.js +61 -0
  65. package/dist/src/Evolu/Relay.d.ts +13 -0
  66. package/dist/src/Evolu/Relay.d.ts.map +1 -0
  67. package/dist/src/Evolu/Relay.js +109 -0
  68. package/dist/src/Evolu/Schema.d.ts +201 -0
  69. package/dist/src/Evolu/Schema.d.ts.map +1 -0
  70. package/dist/src/Evolu/Schema.js +150 -0
  71. package/dist/src/Evolu/Storage.d.ts +49 -0
  72. package/dist/src/Evolu/Storage.d.ts.map +1 -0
  73. package/dist/src/Evolu/Storage.js +1111 -0
  74. package/dist/src/Evolu/Sync.d.ts +59 -0
  75. package/dist/src/Evolu/Sync.d.ts.map +1 -0
  76. package/dist/src/Evolu/Sync.js +29 -0
  77. package/dist/src/Evolu/Timestamp.d.ts +106 -0
  78. package/dist/src/Evolu/Timestamp.d.ts.map +1 -0
  79. package/dist/src/Evolu/Timestamp.js +179 -0
  80. package/dist/src/Function.d.ts +54 -0
  81. package/dist/src/Function.d.ts.map +1 -0
  82. package/dist/src/Function.js +38 -0
  83. package/dist/src/ManyToManyMap.d.ts +26 -0
  84. package/dist/src/ManyToManyMap.d.ts.map +1 -0
  85. package/dist/src/ManyToManyMap.js +92 -0
  86. package/dist/src/NanoId.d.ts +27 -0
  87. package/dist/src/NanoId.d.ts.map +1 -0
  88. package/dist/src/NanoId.js +6 -0
  89. package/dist/src/Number.d.ts +42 -0
  90. package/dist/src/Number.d.ts.map +1 -0
  91. package/dist/src/Number.js +55 -0
  92. package/dist/src/Object.d.ts +35 -0
  93. package/dist/src/Object.d.ts.map +1 -0
  94. package/dist/src/Object.js +36 -0
  95. package/dist/src/Order.d.ts +90 -0
  96. package/dist/src/Order.d.ts.map +1 -0
  97. package/dist/src/Order.js +85 -0
  98. package/dist/src/Promise.d.ts +180 -0
  99. package/dist/src/Promise.d.ts.map +1 -0
  100. package/dist/src/Promise.js +176 -0
  101. package/dist/src/Random.d.ts +52 -0
  102. package/dist/src/Random.d.ts.map +1 -0
  103. package/dist/src/Random.js +29 -0
  104. package/dist/src/Ref.d.ts +40 -0
  105. package/dist/src/Ref.d.ts.map +1 -0
  106. package/dist/src/Ref.js +13 -0
  107. package/dist/src/Result.d.ts +421 -0
  108. package/dist/src/Result.d.ts.map +1 -0
  109. package/dist/src/Result.js +357 -0
  110. package/dist/src/Skiplist.d.ts +23 -0
  111. package/dist/src/Skiplist.d.ts.map +1 -0
  112. package/dist/src/Skiplist.js +58 -0
  113. package/dist/src/Sqlite.d.ts +116 -52
  114. package/dist/src/Sqlite.d.ts.map +1 -1
  115. package/dist/src/Sqlite.js +183 -67
  116. package/dist/src/Store.d.ts +45 -8
  117. package/dist/src/Store.d.ts.map +1 -1
  118. package/dist/src/Store.js +33 -17
  119. package/dist/src/String.d.ts +2 -0
  120. package/dist/src/String.d.ts.map +1 -0
  121. package/dist/src/String.js +14 -0
  122. package/dist/src/Time.d.ts +20 -0
  123. package/dist/src/Time.d.ts.map +1 -0
  124. package/dist/src/Time.js +25 -0
  125. package/dist/src/Type.d.ts +1937 -0
  126. package/dist/src/Type.d.ts.map +1 -0
  127. package/dist/src/Type.js +2002 -0
  128. package/dist/src/Types.d.ts +188 -0
  129. package/dist/src/Types.d.ts.map +1 -0
  130. package/dist/src/Types.js +6 -0
  131. package/dist/src/WebSocket.d.ts +112 -0
  132. package/dist/src/WebSocket.d.ts.map +1 -0
  133. package/dist/src/WebSocket.js +139 -0
  134. package/dist/src/Worker.d.ts +44 -0
  135. package/dist/src/Worker.d.ts.map +1 -0
  136. package/dist/src/Worker.js +66 -0
  137. package/dist/src/index.d.ts +24 -11
  138. package/dist/src/index.d.ts.map +1 -1
  139. package/dist/src/index.js +24 -11
  140. package/package.json +29 -38
  141. package/src/Array.ts +39 -0
  142. package/src/Assert.ts +116 -0
  143. package/src/BigInt.ts +29 -0
  144. package/src/Buffer.ts +175 -0
  145. package/src/Callbacks.ts +43 -0
  146. package/src/Console.ts +159 -0
  147. package/src/Crypto.ts +169 -115
  148. package/src/Eq.ts +204 -0
  149. package/src/Error.ts +57 -20
  150. package/src/Evolu/Config.ts +83 -0
  151. package/src/Evolu/Db.ts +1275 -0
  152. package/src/Evolu/Diff.ts +142 -0
  153. package/src/Evolu/Evolu.ts +947 -0
  154. package/src/Evolu/Internal.ts +26 -0
  155. package/src/Evolu/Kysely.ts +38 -0
  156. package/src/Evolu/Owner.ts +296 -0
  157. package/src/Evolu/Platform.ts +27 -0
  158. package/src/Evolu/Protocol.ts +1857 -0
  159. package/src/Evolu/Public.ts +43 -0
  160. package/src/Evolu/PublicKysely.ts +240 -0
  161. package/src/Evolu/Query.ts +167 -0
  162. package/src/Evolu/Relay.ts +142 -0
  163. package/src/Evolu/Schema.ts +417 -0
  164. package/src/Evolu/Storage.ts +1281 -0
  165. package/src/Evolu/Sync.ts +105 -0
  166. package/src/Evolu/Timestamp.ts +311 -0
  167. package/src/Function.ts +58 -0
  168. package/src/ManyToManyMap.ts +140 -0
  169. package/src/NanoId.ts +39 -0
  170. package/src/Number.ts +90 -0
  171. package/src/Object.ts +64 -0
  172. package/src/Order.ts +113 -0
  173. package/src/Promise.ts +295 -0
  174. package/src/Random.ts +68 -0
  175. package/src/Ref.ts +63 -0
  176. package/src/Result.ts +453 -0
  177. package/src/Skiplist.ts +102 -0
  178. package/src/Sqlite.ts +366 -153
  179. package/src/Store.ts +79 -36
  180. package/src/String.ts +10 -0
  181. package/src/Time.ts +36 -0
  182. package/src/Type.ts +3978 -0
  183. package/src/Types.ts +209 -0
  184. package/src/WebSocket.ts +273 -0
  185. package/src/Worker.ts +129 -0
  186. package/src/index.ts +24 -11
  187. package/dist/src/Config.d.ts +0 -56
  188. package/dist/src/Config.d.ts.map +0 -1
  189. package/dist/src/Config.js +0 -39
  190. package/dist/src/Crdt.d.ts +0 -89
  191. package/dist/src/Crdt.d.ts.map +0 -1
  192. package/dist/src/Crdt.js +0 -181
  193. package/dist/src/Db.d.ts +0 -107
  194. package/dist/src/Db.d.ts.map +0 -1
  195. package/dist/src/Db.js +0 -443
  196. package/dist/src/Diff.d.ts +0 -27
  197. package/dist/src/Diff.d.ts.map +0 -1
  198. package/dist/src/Diff.js +0 -84
  199. package/dist/src/Evolu.d.ts +0 -426
  200. package/dist/src/Evolu.d.ts.map +0 -1
  201. package/dist/src/Evolu.js +0 -333
  202. package/dist/src/Model.d.ts +0 -141
  203. package/dist/src/Model.d.ts.map +0 -1
  204. package/dist/src/Model.js +0 -125
  205. package/dist/src/Murmurhash.d.ts +0 -2
  206. package/dist/src/Murmurhash.d.ts.map +0 -1
  207. package/dist/src/Murmurhash.js +0 -60
  208. package/dist/src/Owner.d.ts +0 -33
  209. package/dist/src/Owner.d.ts.map +0 -1
  210. package/dist/src/Owner.js +0 -26
  211. package/dist/src/Platform.d.ts +0 -37
  212. package/dist/src/Platform.d.ts.map +0 -1
  213. package/dist/src/Platform.js +0 -11
  214. package/dist/src/Protobuf.d.ts +0 -81
  215. package/dist/src/Protobuf.d.ts.map +0 -1
  216. package/dist/src/Protobuf.js +0 -92
  217. package/dist/src/Public.d.ts +0 -13
  218. package/dist/src/Public.d.ts.map +0 -1
  219. package/dist/src/Public.js +0 -6
  220. package/dist/src/Socket.d.ts +0 -8
  221. package/dist/src/Socket.d.ts.map +0 -1
  222. package/dist/src/Socket.js +0 -51
  223. package/dist/src/Sql.d.ts +0 -12
  224. package/dist/src/Sql.d.ts.map +0 -1
  225. package/dist/src/Sql.js +0 -30
  226. package/dist/src/Sync.d.ts +0 -70
  227. package/dist/src/Sync.d.ts.map +0 -1
  228. package/dist/src/Sync.js +0 -127
  229. package/src/Config.ts +0 -119
  230. package/src/Crdt.ts +0 -361
  231. package/src/Db.ts +0 -955
  232. package/src/Diff.ts +0 -114
  233. package/src/Evolu.ts +0 -1016
  234. package/src/Model.ts +0 -233
  235. package/src/Murmurhash.ts +0 -70
  236. package/src/Owner.ts +0 -69
  237. package/src/Platform.ts +0 -47
  238. package/src/Protobuf.ts +0 -155
  239. package/src/Public.ts +0 -12
  240. package/src/Socket.ts +0 -83
  241. package/src/Sql.ts +0 -41
  242. package/src/Sync.ts +0 -315
@@ -0,0 +1,42 @@
1
+ import { NonEmptyReadonlyArray } from "./Array.js";
2
+ import { Result } from "./Result.js";
3
+ import { NonNegativeInt, PositiveInt } from "./Type.js";
4
+ import { IsBranded, Predicate, WidenLiteral } from "./Types.js";
5
+ export declare const increment: (n: number) => number;
6
+ export declare const decrement: (n: number) => number;
7
+ /** Clamps a number within a given range. */
8
+ export declare const clamp: (min: number, max: number) => (n: number) => number;
9
+ /**
10
+ * Creates a predicate that checks if a number is within a range, inclusive.
11
+ *
12
+ * ### Example
13
+ *
14
+ * ```ts
15
+ * const isBetween10And20 = isBetween(10, 20);
16
+ * console.log(isBetween10And20(15)); // true
17
+ * console.log(isBetween10And20(25)); // false
18
+ * ```
19
+ */
20
+ export declare const isBetween: (min: number, max: number) => Predicate<number>;
21
+ /** Returns the minimum value, preserving branded type if applicable. */
22
+ export declare const min: <T extends number>(...values: [T, ...ReadonlyArray<T>]) => IsBranded<T> extends true ? T : WidenLiteral<T>;
23
+ /** Returns the maximum value, preserving branded type if applicable. */
24
+ export declare const max: <T extends number>(...values: [T, ...ReadonlyArray<T>]) => IsBranded<T> extends true ? T : WidenLiteral<T>;
25
+ /**
26
+ * Divides items into buckets as evenly as possible, ensuring each bucket has at
27
+ * least the minimum number of items. Returns a success result if the minimum is
28
+ * met, or an error result with the required number of items if not.
29
+ *
30
+ * ### Example
31
+ *
32
+ * ```ts
33
+ * computeBalancedBuckets(10, 3, 2); // Returns ok([4, 7, 10])
34
+ * computeBalancedBuckets(5, 3, 2); // Returns err(6)
35
+ * ```
36
+ */
37
+ export declare const computeBalancedBuckets: (numberOfItems: NonNegativeInt,
38
+ /** Default: 16 */
39
+ numberOfBuckets?: PositiveInt,
40
+ /** Default: 2 */
41
+ minNumberOfItemsPerBucket?: PositiveInt) => Result<NonEmptyReadonlyArray<PositiveInt>, PositiveInt>;
42
+ //# sourceMappingURL=Number.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Number.d.ts","sourceRoot":"","sources":["../../src/Number.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAEnD,OAAO,EAAW,MAAM,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AACxD,OAAO,EAEL,SAAS,EACT,SAAS,EACT,YAAY,EACb,MAAM,YAAY,CAAC;AAEpB,eAAO,MAAM,SAAS,GAAI,GAAG,MAAM,KAAG,MAAe,CAAC;AAEtD,eAAO,MAAM,SAAS,GAAI,GAAG,MAAM,KAAG,MAAe,CAAC;AAEtD,4CAA4C;AAC5C,eAAO,MAAM,KAAK,GACf,KAAK,MAAM,EAAE,KAAK,MAAM,MACxB,GAAG,MAAM,KAAG,MACoB,CAAC;AAEpC;;;;;;;;;;GAUG;AACH,eAAO,MAAM,SAAS,GACnB,KAAK,MAAM,EAAE,KAAK,MAAM,KAAG,SAAS,CAAC,MAAM,CAEd,CAAC;AAEjC,wEAAwE;AACxE,eAAO,MAAM,GAAG,GAAI,CAAC,SAAS,MAAM,EAClC,GAAG,QAAQ,CAAC,CAAC,EAAE,GAAG,aAAa,CAAC,CAAC,CAAC,CAAC,KAClC,SAAS,CAAC,CAAC,CAAC,SAAS,IAAI,GAAG,CAAC,GAAG,YAAY,CAAC,CAAC,CACa,CAAC;AAE/D,wEAAwE;AACxE,eAAO,MAAM,GAAG,GAAI,CAAC,SAAS,MAAM,EAClC,GAAG,QAAQ,CAAC,CAAC,EAAE,GAAG,aAAa,CAAC,CAAC,CAAC,CAAC,KAClC,SAAS,CAAC,CAAC,CAAC,SAAS,IAAI,GAAG,CAAC,GAAG,YAAY,CAAC,CAAC,CACa,CAAC;AAE/D;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,sBAAsB,GACjC,eAAe,cAAc;AAE7B,kBAAkB;AAClB,kBAAwB,WAAW;AAEnC,iBAAiB;AACjB,4BAAiC,WAAW,KAC3C,MAAM,CAAC,qBAAqB,CAAC,WAAW,CAAC,EAAE,WAAW,CAoBxD,CAAC"}
@@ -0,0 +1,55 @@
1
+ import { assertNonEmptyReadonlyArray } from "./Assert.js";
2
+ import { err, ok } from "./Result.js";
3
+ export const increment = (n) => n + 1;
4
+ export const decrement = (n) => n - 1;
5
+ /** Clamps a number within a given range. */
6
+ export const clamp = (min, max) => (n) => Math.min(Math.max(n, min), max);
7
+ /**
8
+ * Creates a predicate that checks if a number is within a range, inclusive.
9
+ *
10
+ * ### Example
11
+ *
12
+ * ```ts
13
+ * const isBetween10And20 = isBetween(10, 20);
14
+ * console.log(isBetween10And20(15)); // true
15
+ * console.log(isBetween10And20(25)); // false
16
+ * ```
17
+ */
18
+ export const isBetween = (min, max) => (value) => value >= min && value <= max;
19
+ /** Returns the minimum value, preserving branded type if applicable. */
20
+ export const min = (...values) => values.reduce((a, b) => (a < b ? a : b));
21
+ /** Returns the maximum value, preserving branded type if applicable. */
22
+ export const max = (...values) => values.reduce((a, b) => (a > b ? a : b));
23
+ /**
24
+ * Divides items into buckets as evenly as possible, ensuring each bucket has at
25
+ * least the minimum number of items. Returns a success result if the minimum is
26
+ * met, or an error result with the required number of items if not.
27
+ *
28
+ * ### Example
29
+ *
30
+ * ```ts
31
+ * computeBalancedBuckets(10, 3, 2); // Returns ok([4, 7, 10])
32
+ * computeBalancedBuckets(5, 3, 2); // Returns err(6)
33
+ * ```
34
+ */
35
+ export const computeBalancedBuckets = (numberOfItems,
36
+ /** Default: 16 */
37
+ numberOfBuckets = 16,
38
+ /** Default: 2 */
39
+ minNumberOfItemsPerBucket = 2) => {
40
+ const minRequiredItems = numberOfBuckets * minNumberOfItemsPerBucket;
41
+ if (numberOfItems < minRequiredItems)
42
+ return err(minRequiredItems);
43
+ const indexes = [];
44
+ const itemsPerBucket = Math.floor(numberOfItems / numberOfBuckets);
45
+ const extraItems = numberOfItems % numberOfBuckets;
46
+ let bucketBoundary = 0;
47
+ for (let i = 0; i < numberOfBuckets; i++) {
48
+ const hasExtraItem = i < extraItems;
49
+ const itemsInThisBucket = itemsPerBucket + (hasExtraItem ? 1 : 0);
50
+ bucketBoundary += itemsInThisBucket;
51
+ indexes.push(bucketBoundary);
52
+ }
53
+ assertNonEmptyReadonlyArray(indexes);
54
+ return ok(indexes);
55
+ };
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Checks if a value is a plain object (e.g., created with `{}` or `Object`).
3
+ *
4
+ * ### Example
5
+ *
6
+ * ```ts
7
+ * isPlainObject({}); // true
8
+ * isPlainObject(new Date()); // false
9
+ * isPlainObject([]); // false
10
+ * isPlainObject(null); // false
11
+ * ```
12
+ */
13
+ export declare const isPlainObject: (value: unknown) => value is Record<string, unknown>;
14
+ /**
15
+ * A read-only `Record<K, V>` with `K extends keyof any` to preserve branded key
16
+ * types (e.g., in {@link mapObject}).
17
+ */
18
+ export type ReadonlyRecord<K extends keyof any, V> = Readonly<Record<K, V>>;
19
+ type StringKeyOf<T> = Extract<keyof T, string>;
20
+ /**
21
+ * Converts a record to entries, preserving branded string key types (e.g.,
22
+ * `type Id = 'id' & string`) via `StringKeyOf<T>`, unlike `Object.entries`
23
+ * which widens keys to `string`.
24
+ */
25
+ export declare const objectToEntries: <T extends Record<string, any>>(record: T) => Array<[StringKeyOf<T>, T[StringKeyOf<T>]]>;
26
+ /**
27
+ * Maps a `ReadonlyRecord<K, V>` to a new `ReadonlyRecord<K, U>`, preserving
28
+ * branded key types (e.g., `type Id = 'id' & string`) lost by `Object.entries`.
29
+ * Uses `K extends string` for precision.
30
+ */
31
+ export declare const mapObject: <K extends string, V, U>(record: ReadonlyRecord<K, V>, fn: (value: V, key: K) => U) => ReadonlyRecord<K, U>;
32
+ /** Conditionally excludes a property from an object. */
33
+ export declare const excludeProp: <T extends object, K extends keyof T>(obj: T, prop: K, condition?: boolean) => typeof condition extends true ? T : Omit<T, K>;
34
+ export {};
35
+ //# sourceMappingURL=Object.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Object.d.ts","sourceRoot":"","sources":["../../src/Object.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,aAAa,GACxB,OAAO,OAAO,KACb,KAAK,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAC2B,CAAC;AAE9D;;;GAGG;AACH,MAAM,MAAM,cAAc,CAAC,CAAC,SAAS,MAAM,GAAG,EAAE,CAAC,IAAI,QAAQ,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AAG5E,KAAK,WAAW,CAAC,CAAC,IAAI,OAAO,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,CAAC;AAE/C;;;;GAIG;AACH,eAAO,MAAM,eAAe,GAAI,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC3D,QAAQ,CAAC,KACR,KAAK,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,CAC0B,CAAC;AAEvE;;;;GAIG;AACH,eAAO,MAAM,SAAS,GAAI,CAAC,SAAS,MAAM,EAAE,CAAC,EAAE,CAAC,EAC9C,QAAQ,cAAc,CAAC,CAAC,EAAE,CAAC,CAAC,EAC5B,IAAI,CAAC,KAAK,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,KAC1B,cAAc,CAAC,CAAC,EAAE,CAAC,CAMK,CAAC;AAE5B,wDAAwD;AACxD,eAAO,MAAM,WAAW,GAAI,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,MAAM,CAAC,EAC7D,KAAK,CAAC,EACN,MAAM,CAAC,EACP,YAAY,OAAO,KAClB,OAAO,SAAS,SAAS,IAAI,GAAG,CAAC,GAAG,IAAI,CAAC,CAAC,EAAE,CAAC,CAM/C,CAAC"}
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Checks if a value is a plain object (e.g., created with `{}` or `Object`).
3
+ *
4
+ * ### Example
5
+ *
6
+ * ```ts
7
+ * isPlainObject({}); // true
8
+ * isPlainObject(new Date()); // false
9
+ * isPlainObject([]); // false
10
+ * isPlainObject(null); // false
11
+ * ```
12
+ */
13
+ export const isPlainObject = (value) => Object.prototype.toString.call(value) === "[object Object]";
14
+ /**
15
+ * Converts a record to entries, preserving branded string key types (e.g.,
16
+ * `type Id = 'id' & string`) via `StringKeyOf<T>`, unlike `Object.entries`
17
+ * which widens keys to `string`.
18
+ */
19
+ export const objectToEntries = (record) => Object.entries(record);
20
+ /**
21
+ * Maps a `ReadonlyRecord<K, V>` to a new `ReadonlyRecord<K, U>`, preserving
22
+ * branded key types (e.g., `type Id = 'id' & string`) lost by `Object.entries`.
23
+ * Uses `K extends string` for precision.
24
+ */
25
+ export const mapObject = (record, fn) => Object.fromEntries(Object.entries(record).map(([key, value]) => [
26
+ key,
27
+ fn(value, key),
28
+ ]));
29
+ /** Conditionally excludes a property from an object. */
30
+ export const excludeProp = (obj, prop, condition) => {
31
+ if (condition) {
32
+ return { ...obj };
33
+ }
34
+ const { [prop]: _, ...rest } = obj;
35
+ return rest;
36
+ };
@@ -0,0 +1,90 @@
1
+ /**
2
+ * 🔢
3
+ *
4
+ * @module
5
+ */
6
+ /**
7
+ * Compares two values of type `A` and returns their ordering.
8
+ *
9
+ * Ordering functions start with an 'order' prefix, e.g., `orderNumber`.
10
+ *
11
+ * - Returns `-1` if `x` is less than `y`.
12
+ * - Returns `0` if `x` is equal to `y`.
13
+ * - Returns `1` if `x` is greater than `y`.
14
+ */
15
+ export type Order<in A> = (x: A, y: A) => Ordering;
16
+ /**
17
+ * A type representing the result of an ordering operation.
18
+ *
19
+ * Compatible with the return values expected by `Array.prototype.sort`.
20
+ */
21
+ export type Ordering = -1 | 0 | 1;
22
+ /**
23
+ * Creates an ordering function from a "less than" comparator.
24
+ *
25
+ * ### Example
26
+ *
27
+ * ```ts
28
+ * const orderNumber = createOrder<number>((x, y) => x < y);
29
+ * expect(orderNumber(1, 2)).toEqual(-1);
30
+ * expect(orderNumber(2, 1)).toEqual(1);
31
+ * expect(orderNumber(1, 1)).toEqual(0);
32
+ * ```
33
+ */
34
+ export declare const createOrder: <A>(isLessThan: (x: A, y: A) => boolean) => Order<A>;
35
+ /**
36
+ * Returns an order that reverses the order of the given order.
37
+ *
38
+ * ### Example
39
+ *
40
+ * ```ts
41
+ * reverseOrder(orderNumber)(1, 2); // 1
42
+ * reverseOrder(orderNumber)(2, 1); // -1
43
+ * reverseOrder(orderNumber)(1, 1); // 0
44
+ * ```
45
+ */
46
+ export declare const reverseOrder: <A>(order: Order<A>) => Order<A>;
47
+ /**
48
+ * An order for `string` values in ascending order.
49
+ *
50
+ * ### Example
51
+ *
52
+ * ```ts
53
+ * orderString("a", "b"); // -1
54
+ * orderString("b", "a"); // 1
55
+ * orderString("a", "a"); // 0
56
+ * ["c", "b", "a"].toSorted(orderString); // ["a", "b", "c"]
57
+ * ```
58
+ */
59
+ export declare const orderString: Order<string>;
60
+ /**
61
+ * An order for numbers in ascending order.
62
+ *
63
+ * ### Example
64
+ *
65
+ * ```ts
66
+ * orderNumber(1, 2); // -1
67
+ * orderNumber(2, 1); // 1
68
+ * orderNumber(1, 1); // 0
69
+ * [2, 1, 3].toSorted(orderNumber); // [1, 2, 3]
70
+ * reverseOrder(orderNumber)(1, 2); // 1
71
+ * reverseOrder(orderNumber)(2, 1); // -1
72
+ * reverseOrder(orderNumber)(1, 1); // 0
73
+ * ```
74
+ */
75
+ export declare const orderNumber: Order<number>;
76
+ /**
77
+ * An order for bigints in ascending order.
78
+ *
79
+ * ### Example
80
+ *
81
+ * ```ts
82
+ * orderBigInt(1n, 2n); // -1
83
+ * orderBigInt(2n, 1n); // 1
84
+ * orderBigInt(1n, 1n); // 0
85
+ * [2n, 1n, 3n].toSorted(orderBigInt); // [1n, 2n, 3n]
86
+ * ```
87
+ */
88
+ export declare const orderBigInt: Order<bigint>;
89
+ export declare const orderUint8Array: Order<globalThis.Uint8Array>;
90
+ //# sourceMappingURL=Order.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Order.d.ts","sourceRoot":"","sources":["../../src/Order.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;;;;;;GAQG;AACH,MAAM,MAAM,KAAK,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,KAAK,QAAQ,CAAC;AAEnD;;;;GAIG;AACH,MAAM,MAAM,QAAQ,GAAG,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;AAElC;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,WAAW,GACrB,CAAC,EAAE,YAAY,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,KAAK,OAAO,KAAG,KAAK,CAAC,CAAC,CAER,CAAC;AAE5C;;;;;;;;;;GAUG;AACH,eAAO,MAAM,YAAY,GACtB,CAAC,EAAE,OAAO,KAAK,CAAC,CAAC,CAAC,KAAG,KAAK,CAAC,CAAC,CAEhB,CAAC;AAEhB;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,WAAW,EAAE,KAAK,CAAC,MAAM,CAAgC,CAAC;AAEvE;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,WAAW,eAAuC,CAAC;AAEhE;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,WAAW,eAAuC,CAAC;AAEhE,eAAO,MAAM,eAAe,EAAE,KAAK,CAAC,UAAU,CAAC,UAAU,CAUxD,CAAC"}
@@ -0,0 +1,85 @@
1
+ /**
2
+ * 🔢
3
+ *
4
+ * @module
5
+ */
6
+ /**
7
+ * Creates an ordering function from a "less than" comparator.
8
+ *
9
+ * ### Example
10
+ *
11
+ * ```ts
12
+ * const orderNumber = createOrder<number>((x, y) => x < y);
13
+ * expect(orderNumber(1, 2)).toEqual(-1);
14
+ * expect(orderNumber(2, 1)).toEqual(1);
15
+ * expect(orderNumber(1, 1)).toEqual(0);
16
+ * ```
17
+ */
18
+ export const createOrder = (isLessThan) => (x, y) => x === y ? 0 : isLessThan(x, y) ? -1 : 1;
19
+ /**
20
+ * Returns an order that reverses the order of the given order.
21
+ *
22
+ * ### Example
23
+ *
24
+ * ```ts
25
+ * reverseOrder(orderNumber)(1, 2); // 1
26
+ * reverseOrder(orderNumber)(2, 1); // -1
27
+ * reverseOrder(orderNumber)(1, 1); // 0
28
+ * ```
29
+ */
30
+ export const reverseOrder = (order) => (a, b) => order(b, a);
31
+ /**
32
+ * An order for `string` values in ascending order.
33
+ *
34
+ * ### Example
35
+ *
36
+ * ```ts
37
+ * orderString("a", "b"); // -1
38
+ * orderString("b", "a"); // 1
39
+ * orderString("a", "a"); // 0
40
+ * ["c", "b", "a"].toSorted(orderString); // ["a", "b", "c"]
41
+ * ```
42
+ */
43
+ export const orderString = createOrder((a, b) => a < b);
44
+ /**
45
+ * An order for numbers in ascending order.
46
+ *
47
+ * ### Example
48
+ *
49
+ * ```ts
50
+ * orderNumber(1, 2); // -1
51
+ * orderNumber(2, 1); // 1
52
+ * orderNumber(1, 1); // 0
53
+ * [2, 1, 3].toSorted(orderNumber); // [1, 2, 3]
54
+ * reverseOrder(orderNumber)(1, 2); // 1
55
+ * reverseOrder(orderNumber)(2, 1); // -1
56
+ * reverseOrder(orderNumber)(1, 1); // 0
57
+ * ```
58
+ */
59
+ export const orderNumber = createOrder((a, b) => a < b);
60
+ /**
61
+ * An order for bigints in ascending order.
62
+ *
63
+ * ### Example
64
+ *
65
+ * ```ts
66
+ * orderBigInt(1n, 2n); // -1
67
+ * orderBigInt(2n, 1n); // 1
68
+ * orderBigInt(1n, 1n); // 0
69
+ * [2n, 1n, 3n].toSorted(orderBigInt); // [1n, 2n, 3n]
70
+ * ```
71
+ */
72
+ export const orderBigInt = createOrder((a, b) => a < b);
73
+ export const orderUint8Array = (a, b) => {
74
+ if (a.byteLength > b.byteLength)
75
+ return 1;
76
+ if (a.byteLength < b.byteLength)
77
+ return -1;
78
+ for (let i = 0; i < a.byteLength; i++) {
79
+ if (a[i] < b[i])
80
+ return -1;
81
+ if (a[i] > b[i])
82
+ return 1;
83
+ }
84
+ return 0;
85
+ };
@@ -0,0 +1,180 @@
1
+ import { Result } from "./Result.js";
2
+ import { Predicate } from "./Types.js";
3
+ /**
4
+ * Helper function to delay execution for a specified number of milliseconds.
5
+ *
6
+ * ### Example
7
+ *
8
+ * ```ts
9
+ * await wait(10);
10
+ * ```
11
+ */
12
+ export declare const wait: (ms: number) => Promise<Result<void, never>>;
13
+ /** Options for configuring retry behavior. */
14
+ export interface RetryOptions<E> {
15
+ /**
16
+ * Maximum number of retry attempts after the initial attempt (default: 3).
17
+ * For example, with maxRetries = 3, the function will be called up to 4 times
18
+ * (1 initial attempt + 3 retries).
19
+ */
20
+ maxRetries?: number;
21
+ /**
22
+ * Initial delay between retry attempts in milliseconds (default: 100). This
23
+ * is the delay after the first failed attempt. Subsequent delays increase
24
+ * exponentially according to the factor option.
25
+ */
26
+ initialDelay?: number;
27
+ /**
28
+ * Maximum delay between retry attempts in milliseconds (default: 10000). This
29
+ * caps the exponential backoff to prevent extremely long delays after many
30
+ * retries.
31
+ */
32
+ maxDelay?: number;
33
+ /**
34
+ * Multiplier that determines how quickly the delay increases (default: 2).
35
+ * With the default value, each successive delay is twice as long as the
36
+ * previous one (e.g., 100ms, 200ms, 400ms, 800ms, etc).
37
+ */
38
+ factor?: number;
39
+ /**
40
+ * Random jitter factor between 0 and 1 (default: 0.1). Adds randomness to
41
+ * delay times to prevent retry storms in distributed systems.
42
+ */
43
+ jitter?: number;
44
+ /**
45
+ * Optional AbortSignal to cancel retries. If the signal is aborted, the retry
46
+ * operation stops and returns a RetryAbortError.
47
+ */
48
+ signal?: AbortSignal;
49
+ /**
50
+ * Optional predicate to determine if an error should be retried. Returns true
51
+ * if the error is retryable, false otherwise. This allows selectively
52
+ * retrying only certain types of errors. By default, all errors are
53
+ * considered retryable.
54
+ */
55
+ retryable?: Predicate<E>;
56
+ /**
57
+ * Optional callback called before each retry attempt. Receives the error that
58
+ * caused the retry, the current attempt number (starting at 1), and the delay
59
+ * in milliseconds before the next attempt.
60
+ */
61
+ onRetry?: (error: E, attempt: number, delay: number) => void;
62
+ }
63
+ /** Error representing a retry operation that failed after multiple attempts. */
64
+ export interface RetryError<E> {
65
+ readonly type: "RetryError";
66
+ /** The original error that caused the retry to fail */
67
+ readonly cause: E;
68
+ /** Number of retry attempts made */
69
+ readonly attempts: number;
70
+ }
71
+ /** Error representing a retry operation that was aborted. */
72
+ export interface RetryAbortError {
73
+ readonly type: "RetryAbortError";
74
+ readonly abortedBeforeExecution: boolean;
75
+ }
76
+ /**
77
+ * Executes a function with retry logic using exponential backoff and jitter.
78
+ *
79
+ * ### Example with Result-based API
80
+ *
81
+ * ```ts
82
+ * interface ApiError {
83
+ * type: "ApiError";
84
+ * statusCode: number;
85
+ * }
86
+ *
87
+ * const fetchData = async (
88
+ * url: string,
89
+ * ): Promise<Result<Data, ApiError>> => {
90
+ * // Implementation that returns Result
91
+ * };
92
+ *
93
+ * const result = await retry(
94
+ * async () => fetchData("https://api.example.com/data"),
95
+ * {
96
+ * maxRetries: 5,
97
+ * initialDelay: 200,
98
+ * // Only retry on specific status codes
99
+ * retryable: (error) =>
100
+ * error.type === "ApiError" && [429, 503].includes(error.statusCode),
101
+ * },
102
+ * );
103
+ *
104
+ * if (!result.ok) {
105
+ * if (result.error.type === "RetryAbortError") {
106
+ * console.log("Operation was aborted");
107
+ * } else {
108
+ * console.log(`Failed after ${result.error.attempts} attempts`);
109
+ * }
110
+ * return;
111
+ * }
112
+ *
113
+ * // Use result.value
114
+ * ```
115
+ *
116
+ * ### Example with tryAsync for exception-based API
117
+ *
118
+ * ```ts
119
+ * interface FetchError {
120
+ * type: "FetchError";
121
+ * message: string;
122
+ * }
123
+ *
124
+ * const controller = new AbortController();
125
+ *
126
+ * const result = await retry(
127
+ * async () =>
128
+ * tryAsync(
129
+ * async () => {
130
+ * const response = await fetch("https://api.example.com/data", {
131
+ * signal: controller.signal,
132
+ * });
133
+ *
134
+ * if (!response.ok) {
135
+ * throw new Error(`HTTP error ${response.status}`);
136
+ * }
137
+ *
138
+ * return await response.json();
139
+ * },
140
+ * (error): FetchError => ({
141
+ * type: "FetchError",
142
+ * message: String(error),
143
+ * }),
144
+ * ),
145
+ * {
146
+ * maxRetries: 3,
147
+ * signal: controller.signal,
148
+ * },
149
+ * );
150
+ * ```
151
+ *
152
+ * ## HTTP Request Recommendations
153
+ *
154
+ * For HTTP requests, configure the `retryable` option to only retry on
155
+ * appropriate errors:
156
+ *
157
+ * - **DO retry**: 429 (Too Many Requests), 503 (Service Unavailable), network
158
+ * errors
159
+ * - **DON'T retry**: 4xx client errors (except 429), most 5xx server errors
160
+ */
161
+ export declare const retry: <T, E>(fn: () => Promise<Result<T, E>>, options?: RetryOptions<E>) => Promise<Result<T, RetryError<E> | RetryAbortError>>;
162
+ export interface TimeoutError {
163
+ readonly type: "TimeoutError";
164
+ readonly timeoutMs: number;
165
+ }
166
+ /**
167
+ * Wraps an async function with a timeout, returning {@link Result} that fails
168
+ * with {@link TimeoutError} if the timeout is exceeded. The provided function
169
+ * must accept an AbortSignal and return a Result.
170
+ *
171
+ * ### Example
172
+ *
173
+ * ```ts
174
+ * const fetchWithTimeout = () =>
175
+ * withTimeout((signal) => fetch("url", signal), 5000);
176
+ * const result = await retry(fetchWithTimeout, { maxRetries: 3 });
177
+ * ```
178
+ */
179
+ export declare const withTimeout: <T, E>(fn: (signal: AbortSignal) => Promise<Result<T, E>>, timeoutMs: number) => Promise<Result<T, E | TimeoutError>>;
180
+ //# sourceMappingURL=Promise.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Promise.d.ts","sourceRoot":"","sources":["../../src/Promise.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAW,MAAM,aAAa,CAAC;AAC9C,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC;;;;;;;;GAQG;AACH,eAAO,MAAM,IAAI,GAAI,IAAI,MAAM,KAAG,OAAO,CAAC,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,CAK1D,CAAC;AAEJ,8CAA8C;AAC9C,MAAM,WAAW,YAAY,CAAC,CAAC;IAC7B;;;;OAIG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB;;;;OAIG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IAEtB;;;;OAIG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB;;;;OAIG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB;;;OAGG;IACH,MAAM,CAAC,EAAE,WAAW,CAAC;IAErB;;;;;OAKG;IACH,SAAS,CAAC,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC;IAEzB;;;;OAIG;IACH,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;CAC9D;AAED,gFAAgF;AAChF,MAAM,WAAW,UAAU,CAAC,CAAC;IAC3B,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAC;IAC5B,uDAAuD;IACvD,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC;IAClB,oCAAoC;IACpC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;CAC3B;AAED,6DAA6D;AAC7D,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,IAAI,EAAE,iBAAiB,CAAC;IACjC,QAAQ,CAAC,sBAAsB,EAAE,OAAO,CAAC;CAC1C;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoFG;AACH,eAAO,MAAM,KAAK,GAAU,CAAC,EAAE,CAAC,EAC9B,IAAI,MAAM,OAAO,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAC/B,UAAS,YAAY,CAAC,CAAC,CAAM,KAC5B,OAAO,CAAC,MAAM,CAAC,CAAC,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,eAAe,CAAC,CA2EpD,CAAC;AAEF,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,IAAI,EAAE,cAAc,CAAC;IAC9B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;CAC5B;AAED;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,WAAW,GAAU,CAAC,EAAE,CAAC,EACpC,IAAI,CAAC,MAAM,EAAE,WAAW,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAClD,WAAW,MAAM,KAChB,OAAO,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,GAAG,YAAY,CAAC,CAerC,CAAC"}