@evolu/common 6.0.1-preview.8 → 7.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.
- package/README.md +2 -2
- package/dist/src/Array.d.ts +256 -12
- package/dist/src/Array.d.ts.map +1 -1
- package/dist/src/Array.js +215 -9
- package/dist/src/Assert.d.ts +0 -13
- package/dist/src/Assert.d.ts.map +1 -1
- package/dist/src/Assert.js +0 -15
- package/dist/src/Brand.d.ts +75 -0
- package/dist/src/Brand.d.ts.map +1 -0
- package/dist/src/Brand.js +1 -0
- package/dist/src/Buffer.d.ts +1 -1
- package/dist/src/Buffer.d.ts.map +1 -1
- package/dist/src/Buffer.js +8 -7
- package/dist/src/Cache.d.ts +44 -0
- package/dist/src/Cache.d.ts.map +1 -0
- package/dist/src/Cache.js +52 -0
- package/dist/src/Callbacks.d.ts +45 -12
- package/dist/src/Callbacks.d.ts.map +1 -1
- package/dist/src/Callbacks.js +14 -7
- package/dist/src/Console.d.ts +31 -6
- package/dist/src/Console.d.ts.map +1 -1
- package/dist/src/Console.js +72 -9
- package/dist/src/Crypto.d.ts +56 -42
- package/dist/src/Crypto.d.ts.map +1 -1
- package/dist/src/Crypto.js +40 -53
- package/dist/src/Evolu/Db.d.ts +162 -74
- package/dist/src/Evolu/Db.d.ts.map +1 -1
- package/dist/src/Evolu/Db.js +284 -702
- package/dist/src/Evolu/Diff.d.ts +3 -3
- package/dist/src/Evolu/Diff.d.ts.map +1 -1
- package/dist/src/Evolu/Diff.js +7 -5
- package/dist/src/Evolu/Evolu.d.ts +214 -134
- package/dist/src/Evolu/Evolu.d.ts.map +1 -1
- package/dist/src/Evolu/Evolu.js +189 -180
- package/dist/src/Evolu/Internal.d.ts +0 -2
- package/dist/src/Evolu/Internal.d.ts.map +1 -1
- package/dist/src/Evolu/Internal.js +0 -2
- package/dist/src/Evolu/LocalAuth.d.ts +150 -0
- package/dist/src/Evolu/LocalAuth.d.ts.map +1 -0
- package/dist/src/Evolu/LocalAuth.js +174 -0
- package/dist/src/Evolu/Owner.d.ts +273 -120
- package/dist/src/Evolu/Owner.d.ts.map +1 -1
- package/dist/src/Evolu/Owner.js +130 -104
- package/dist/src/Evolu/Platform.d.ts +9 -7
- package/dist/src/Evolu/Platform.d.ts.map +1 -1
- package/dist/src/Evolu/Protocol.d.ts +268 -240
- package/dist/src/Evolu/Protocol.d.ts.map +1 -1
- package/dist/src/Evolu/Protocol.js +600 -454
- package/dist/src/Evolu/Public.d.ts +6 -8
- package/dist/src/Evolu/Public.d.ts.map +1 -1
- package/dist/src/Evolu/Public.js +2 -3
- package/dist/src/Evolu/PublicKysely.d.ts.map +1 -1
- package/dist/src/Evolu/PublicKysely.js +3 -4
- package/dist/src/Evolu/Query.d.ts +2 -1
- package/dist/src/Evolu/Query.d.ts.map +1 -1
- package/dist/src/Evolu/Query.js +1 -1
- package/dist/src/Evolu/Relay.d.ts +91 -8
- package/dist/src/Evolu/Relay.d.ts.map +1 -1
- package/dist/src/Evolu/Relay.js +214 -88
- package/dist/src/Evolu/Schema.d.ts +125 -47
- package/dist/src/Evolu/Schema.d.ts.map +1 -1
- package/dist/src/Evolu/Schema.js +175 -31
- package/dist/src/Evolu/Storage.d.ts +249 -27
- package/dist/src/Evolu/Storage.d.ts.map +1 -1
- package/dist/src/Evolu/Storage.js +198 -92
- package/dist/src/Evolu/Sync.d.ts +68 -13
- package/dist/src/Evolu/Sync.d.ts.map +1 -1
- package/dist/src/Evolu/Sync.js +469 -20
- package/dist/src/Evolu/Timestamp.d.ts +83 -30
- package/dist/src/Evolu/Timestamp.d.ts.map +1 -1
- package/dist/src/Evolu/Timestamp.js +79 -34
- package/dist/src/Identicon.d.ts +35 -0
- package/dist/src/Identicon.d.ts.map +1 -0
- package/dist/src/Identicon.js +143 -0
- package/dist/src/Instances.d.ts +34 -0
- package/dist/src/Instances.d.ts.map +1 -0
- package/dist/src/Instances.js +44 -0
- package/dist/src/Number.d.ts +4 -3
- package/dist/src/Number.d.ts.map +1 -1
- package/dist/src/Number.js +5 -4
- package/dist/src/Object.d.ts +10 -4
- package/dist/src/Object.d.ts.map +1 -1
- package/dist/src/Object.js +9 -3
- package/dist/src/Platform.d.ts +20 -0
- package/dist/src/Platform.d.ts.map +1 -0
- package/dist/src/Platform.js +22 -0
- package/dist/src/Random.d.ts +3 -2
- package/dist/src/Random.d.ts.map +1 -1
- package/dist/src/Relation.d.ts +79 -0
- package/dist/src/Relation.d.ts.map +1 -0
- package/dist/src/Relation.js +127 -0
- package/dist/src/Resources.d.ts +118 -0
- package/dist/src/Resources.d.ts.map +1 -0
- package/dist/src/Resources.js +197 -0
- package/dist/src/Result.d.ts +184 -52
- package/dist/src/Result.d.ts.map +1 -1
- package/dist/src/Result.js +30 -241
- package/dist/src/Skiplist.js +2 -1
- package/dist/src/Sqlite.d.ts +89 -5
- package/dist/src/Sqlite.d.ts.map +1 -1
- package/dist/src/Sqlite.js +136 -9
- package/dist/src/Task.d.ts +586 -0
- package/dist/src/Task.d.ts.map +1 -0
- package/dist/src/Task.js +469 -0
- package/dist/src/Time.d.ts +66 -1
- package/dist/src/Time.d.ts.map +1 -1
- package/dist/src/Time.js +99 -5
- package/dist/src/Type.d.ts +676 -343
- package/dist/src/Type.d.ts.map +1 -1
- package/dist/src/Type.js +718 -467
- package/dist/src/Types.d.ts +1 -75
- package/dist/src/Types.d.ts.map +1 -1
- package/dist/src/WebSocket.d.ts +5 -2
- package/dist/src/WebSocket.d.ts.map +1 -1
- package/dist/src/WebSocket.js +12 -18
- package/dist/src/Worker.d.ts +39 -11
- package/dist/src/Worker.d.ts.map +1 -1
- package/dist/src/Worker.js +22 -4
- package/dist/src/index.d.ts +8 -3
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +8 -3
- package/package.json +15 -14
- package/src/Array.ts +301 -19
- package/src/Assert.ts +0 -21
- package/src/Brand.ts +75 -0
- package/src/Buffer.ts +7 -7
- package/src/Cache.ts +85 -0
- package/src/Callbacks.ts +62 -22
- package/src/Console.ts +91 -11
- package/src/Crypto.ts +94 -90
- package/src/Evolu/Db.ts +519 -1026
- package/src/Evolu/Diff.ts +7 -5
- package/src/Evolu/Evolu.ts +477 -361
- package/src/Evolu/Internal.ts +0 -2
- package/src/Evolu/LocalAuth.ts +463 -0
- package/src/Evolu/Owner.ts +355 -228
- package/src/Evolu/Platform.ts +9 -9
- package/src/Evolu/Protocol.ts +842 -742
- package/src/Evolu/Public.ts +7 -14
- package/src/Evolu/PublicKysely.ts +4 -5
- package/src/Evolu/Query.ts +3 -2
- package/src/Evolu/Relay.ts +406 -103
- package/src/Evolu/Schema.ts +323 -91
- package/src/Evolu/Storage.ts +559 -137
- package/src/Evolu/Sync.ts +819 -36
- package/src/Evolu/Timestamp.ts +90 -58
- package/src/Identicon.ts +197 -0
- package/src/Instances.ts +90 -0
- package/src/Number.ts +6 -10
- package/src/Object.ts +13 -5
- package/src/Platform.ts +26 -0
- package/src/Random.ts +3 -2
- package/src/Relation.ts +234 -0
- package/src/Resources.ts +367 -0
- package/src/Result.ts +191 -54
- package/src/Skiplist.ts +1 -1
- package/src/Sqlite.ts +152 -17
- package/src/Task.ts +901 -0
- package/src/Time.ts +180 -5
- package/src/Type.ts +1135 -730
- package/src/Types.ts +1 -77
- package/src/WebSocket.ts +27 -25
- package/src/Worker.ts +72 -23
- package/src/index.ts +8 -3
- package/dist/src/Evolu/Config.d.ts +0 -69
- package/dist/src/Evolu/Config.d.ts.map +0 -1
- package/dist/src/Evolu/Config.js +0 -9
- package/dist/src/Evolu/Kysely.d.ts +0 -6
- package/dist/src/Evolu/Kysely.d.ts.map +0 -1
- package/dist/src/Evolu/Kysely.js +0 -21
- package/dist/src/ManyToManyMap.d.ts +0 -26
- package/dist/src/ManyToManyMap.d.ts.map +0 -1
- package/dist/src/ManyToManyMap.js +0 -92
- package/dist/src/NanoId.d.ts +0 -27
- package/dist/src/NanoId.d.ts.map +0 -1
- package/dist/src/NanoId.js +0 -6
- package/dist/src/Promise.d.ts +0 -180
- package/dist/src/Promise.d.ts.map +0 -1
- package/dist/src/Promise.js +0 -176
- package/src/Evolu/Config.ts +0 -83
- package/src/Evolu/Kysely.ts +0 -38
- package/src/ManyToManyMap.ts +0 -140
- package/src/NanoId.ts +0 -39
- package/src/Promise.ts +0 -295
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Evolu
|
|
2
2
|
|
|
3
|
-
Evolu is TypeScript library and local-first
|
|
3
|
+
Evolu is TypeScript library and local-first platform.
|
|
4
4
|
|
|
5
5
|
## Documentation
|
|
6
6
|
|
|
@@ -24,7 +24,7 @@ The Evolu Relay source along with the Docker files can be in the [/apps/relay](/
|
|
|
24
24
|
|
|
25
25
|
Alternatively, a pre-built image `evoluhq/relay:latest` is hosted on [Docker Hub](https://hub.docker.com/r/evoluhq/relay).
|
|
26
26
|
|
|
27
|
-
For more information, reference the [Evolu Relay](https://www.evolu.dev/docs/
|
|
27
|
+
For more information, reference the [Evolu Relay](https://www.evolu.dev/docs/relay) documentation.
|
|
28
28
|
|
|
29
29
|
## Developing
|
|
30
30
|
|
package/dist/src/Array.d.ts
CHANGED
|
@@ -1,17 +1,261 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/**
|
|
2
|
+
* 🔒 Type-safe array helpers that do not mutate
|
|
3
|
+
*
|
|
4
|
+
* Array types, guards, operations, transformations, accessors, and (rare)
|
|
5
|
+
* mutations.
|
|
6
|
+
*
|
|
7
|
+
* Functions are intentionally data-first to be prepared for the upcoming
|
|
8
|
+
* JavaScript pipe operator.
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* // Data-first is natural for single operations.
|
|
12
|
+
* const timestamps = mapArray(messages, (m) => m.timestamp);
|
|
13
|
+
*
|
|
14
|
+
* // But data-first can be hard to read for nested calls.
|
|
15
|
+
* const result = firstInArray(
|
|
16
|
+
* mapArray(dedupeArray(appendToArray(value, 2)), (x) => x * 2),
|
|
17
|
+
* );
|
|
18
|
+
*
|
|
19
|
+
* // With the upcoming pipe operator, it's clear.
|
|
20
|
+
* // const result = value
|
|
21
|
+
* // |> appendToArray(%, 2)
|
|
22
|
+
* // |> dedupeArray(%)
|
|
23
|
+
* // |> mapArray(%, (x) => x * 2)
|
|
24
|
+
* // |> firstInArray(%);
|
|
25
|
+
*
|
|
26
|
+
* // Until the pipe operator lands, use nested calls or name each step:
|
|
27
|
+
* const appended = appendToArray(value, 2);
|
|
28
|
+
* const deduped = dedupeArray(appended);
|
|
29
|
+
* const mapped = mapArray(deduped, (x) => x * 2);
|
|
30
|
+
* const result = firstInArray(mapped);
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* ### Why data-first?
|
|
34
|
+
*
|
|
35
|
+
* Evolu optimizes for consistent code style. We can't have both data-first
|
|
36
|
+
* single operations and curried data-last helpers without sacrificing
|
|
37
|
+
* consistency. We chose data-first because:
|
|
38
|
+
*
|
|
39
|
+
* - It's natural for single operations (for example `mapArray(messages, (m) =>
|
|
40
|
+
* m.timestamp)`).
|
|
41
|
+
* - It aligns with the upcoming JavaScript pipe operator.
|
|
42
|
+
*
|
|
43
|
+
* **Note**: Feel free to use Array instance methods (mutation) if you think
|
|
44
|
+
* it's better (performance, local scope, etc.).
|
|
45
|
+
*
|
|
46
|
+
* ### Example
|
|
47
|
+
*
|
|
48
|
+
* ```ts
|
|
49
|
+
* // Types - compile-time guarantee of at least one element
|
|
50
|
+
* const _valid: NonEmptyReadonlyArray<number> = [1, 2, 3];
|
|
51
|
+
* // ts-expect-error - empty array is not a valid NonEmptyReadonlyArray
|
|
52
|
+
* const _invalid: NonEmptyReadonlyArray<number> = [];
|
|
53
|
+
*
|
|
54
|
+
* // Guards
|
|
55
|
+
* const arr: ReadonlyArray<number> = [1, 2, 3];
|
|
56
|
+
* if (isNonEmptyReadonlyArray(arr)) {
|
|
57
|
+
* firstInArray(arr);
|
|
58
|
+
* }
|
|
59
|
+
*
|
|
60
|
+
* // Operations
|
|
61
|
+
* const appended = appendToArray([1, 2, 3], 4); // [1, 2, 3, 4]
|
|
62
|
+
* const prepended = prependToArray([2, 3], 1); // [1, 2, 3]
|
|
63
|
+
*
|
|
64
|
+
* // Transformations
|
|
65
|
+
* const readonly: ReadonlyArray<number> = [1, 2, 3];
|
|
66
|
+
* const mapped = mapArray(readonly, (x) => x * 2); // [2, 4, 6]
|
|
67
|
+
* const filtered = filterArray(readonly, (x) => x > 1); // [2, 3]
|
|
68
|
+
* const deduped = dedupeArray([1, 2, 1, 3, 2]); // [1, 2, 3]
|
|
69
|
+
*
|
|
70
|
+
* // Accessors
|
|
71
|
+
* const first = firstInArray(["a", "b", "c"]); // "a"
|
|
72
|
+
* const last = lastInArray(["a", "b", "c"]); // "c"
|
|
73
|
+
*
|
|
74
|
+
* // Mutations
|
|
75
|
+
* const mutable: NonEmptyArray<number> = [1, 2, 3];
|
|
76
|
+
* shiftArray(mutable); // 1 (guaranteed to exist)
|
|
77
|
+
* mutable; // [2, 3]
|
|
78
|
+
* ```
|
|
79
|
+
*
|
|
80
|
+
* @module
|
|
81
|
+
*/
|
|
82
|
+
/**
|
|
83
|
+
* An array with at least one element.
|
|
84
|
+
*
|
|
85
|
+
* @category Types
|
|
86
|
+
*/
|
|
2
87
|
export type NonEmptyArray<T> = [T, ...Array<T>];
|
|
3
|
-
/**
|
|
4
|
-
|
|
5
|
-
|
|
88
|
+
/**
|
|
89
|
+
* A readonly array with at least one element.
|
|
90
|
+
*
|
|
91
|
+
* @category Types
|
|
92
|
+
*/
|
|
6
93
|
export type NonEmptyReadonlyArray<T> = readonly [T, ...ReadonlyArray<T>];
|
|
7
|
-
/**
|
|
94
|
+
/**
|
|
95
|
+
* Checks if an array is non-empty and narrows its type to {@link NonEmptyArray}.
|
|
96
|
+
*
|
|
97
|
+
* Use `if (!isNonEmptyArray(arr))` for empty checks.
|
|
98
|
+
*
|
|
99
|
+
* ### Example
|
|
100
|
+
*
|
|
101
|
+
* ```ts
|
|
102
|
+
* const arr: Array<number> = [1, 2, 3];
|
|
103
|
+
* if (isNonEmptyArray(arr)) {
|
|
104
|
+
* firstInArray(arr); // arr is NonEmptyArray<number>
|
|
105
|
+
* }
|
|
106
|
+
* ```
|
|
107
|
+
*
|
|
108
|
+
* @category Type Guards
|
|
109
|
+
*/
|
|
110
|
+
export declare const isNonEmptyArray: <T>(array: Array<T>) => array is NonEmptyArray<T>;
|
|
111
|
+
/**
|
|
112
|
+
* Checks if a readonly array is non-empty and narrows its type to
|
|
113
|
+
* {@link NonEmptyReadonlyArray}.
|
|
114
|
+
*
|
|
115
|
+
* Use `if (!isNonEmptyReadonlyArray(arr))` for empty checks.
|
|
116
|
+
*
|
|
117
|
+
* ### Example
|
|
118
|
+
*
|
|
119
|
+
* ```ts
|
|
120
|
+
* const arr: ReadonlyArray<number> = [1, 2, 3];
|
|
121
|
+
* if (isNonEmptyReadonlyArray(arr)) {
|
|
122
|
+
* firstInArray(arr); // arr is NonEmptyReadonlyArray<number>
|
|
123
|
+
* }
|
|
124
|
+
* ```
|
|
125
|
+
*
|
|
126
|
+
* @category Type Guards
|
|
127
|
+
*/
|
|
8
128
|
export declare const isNonEmptyReadonlyArray: <T>(array: ReadonlyArray<T>) => array is NonEmptyReadonlyArray<T>;
|
|
9
|
-
/**
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
129
|
+
/**
|
|
130
|
+
* Appends an item to an array, returning a new non-empty readonly array.
|
|
131
|
+
*
|
|
132
|
+
* Accepts both mutable and readonly arrays. Does not mutate the original array.
|
|
133
|
+
*
|
|
134
|
+
* ### Example
|
|
135
|
+
*
|
|
136
|
+
* ```ts
|
|
137
|
+
* appendToArray([1, 2, 3], 4); // [1, 2, 3, 4]
|
|
138
|
+
* ```
|
|
139
|
+
*
|
|
140
|
+
* @category Operations
|
|
141
|
+
*/
|
|
142
|
+
export declare const appendToArray: <T>(array: ReadonlyArray<T>, item: T) => NonEmptyReadonlyArray<T>;
|
|
143
|
+
/**
|
|
144
|
+
* Prepends an item to an array, returning a new non-empty readonly array.
|
|
145
|
+
*
|
|
146
|
+
* Accepts both mutable and readonly arrays. Does not mutate the original array.
|
|
147
|
+
*
|
|
148
|
+
* ### Example
|
|
149
|
+
*
|
|
150
|
+
* ```ts
|
|
151
|
+
* prependToArray([2, 3], 1); // [1, 2, 3]
|
|
152
|
+
* ```
|
|
153
|
+
*
|
|
154
|
+
* @category Operations
|
|
155
|
+
*/
|
|
156
|
+
export declare const prependToArray: <T>(array: ReadonlyArray<T>, item: T) => NonEmptyReadonlyArray<T>;
|
|
157
|
+
/**
|
|
158
|
+
* Maps an array using a mapper function.
|
|
159
|
+
*
|
|
160
|
+
* Accepts both mutable and readonly arrays. Does not mutate the original array.
|
|
161
|
+
* Preserves non-empty type.
|
|
162
|
+
*
|
|
163
|
+
* ### Example
|
|
164
|
+
*
|
|
165
|
+
* ```ts
|
|
166
|
+
* mapArray([1, 2, 3], (x) => x * 2); // [2, 4, 6]
|
|
167
|
+
* ```
|
|
168
|
+
*
|
|
169
|
+
* @category Transformations
|
|
170
|
+
*/
|
|
171
|
+
export declare function mapArray<T, U>(array: NonEmptyReadonlyArray<T> | NonEmptyArray<T>, mapper: (item: T, index: number) => U): NonEmptyReadonlyArray<U>;
|
|
172
|
+
export declare function mapArray<T, U>(array: ReadonlyArray<T> | Array<T>, mapper: (item: T, index: number) => U): ReadonlyArray<U>;
|
|
173
|
+
/**
|
|
174
|
+
* Filters an array using a predicate function, returning a new readonly array.
|
|
175
|
+
*
|
|
176
|
+
* Accepts both mutable and readonly arrays. Does not mutate the original array.
|
|
177
|
+
*
|
|
178
|
+
* ### Example
|
|
179
|
+
*
|
|
180
|
+
* ```ts
|
|
181
|
+
* filterArray([1, 2, 3, 4, 5], (x) => x % 2 === 0); // [2, 4]
|
|
182
|
+
* ```
|
|
183
|
+
*
|
|
184
|
+
* @category Transformations
|
|
185
|
+
*/
|
|
186
|
+
export declare const filterArray: <T>(array: ReadonlyArray<T>, predicate: (item: T, index: number) => boolean) => ReadonlyArray<T>;
|
|
187
|
+
/**
|
|
188
|
+
* Returns a new readonly array with duplicate items removed. If `by` is
|
|
189
|
+
* provided, it will be used to derive the key for uniqueness; otherwise values
|
|
190
|
+
* are used directly. Dedupes by reference equality of values (or extracted keys
|
|
191
|
+
* when `by` is used).
|
|
192
|
+
*
|
|
193
|
+
* Accepts both mutable and readonly arrays. Does not mutate the original array.
|
|
194
|
+
* Preserves non-empty type.
|
|
195
|
+
*
|
|
196
|
+
* ### Example
|
|
197
|
+
*
|
|
198
|
+
* ```ts
|
|
199
|
+
* // Dedupe primitives by value
|
|
200
|
+
* dedupeArray([1, 2, 1, 3, 2]); // [1, 2, 3]
|
|
201
|
+
*
|
|
202
|
+
* // Dedupe objects by property
|
|
203
|
+
* dedupeArray(
|
|
204
|
+
* [
|
|
205
|
+
* { id: 1, name: "Alice" },
|
|
206
|
+
* { id: 2, name: "Bob" },
|
|
207
|
+
* { id: 1, name: "Alice 2" },
|
|
208
|
+
* ],
|
|
209
|
+
* (item) => item.id,
|
|
210
|
+
* ); // [{ id: 1, name: "Alice" }, { id: 2, name: "Bob" }]
|
|
211
|
+
* ```
|
|
212
|
+
*
|
|
213
|
+
* @category Transformations
|
|
214
|
+
*/
|
|
215
|
+
export declare function dedupeArray<T>(array: NonEmptyReadonlyArray<T> | NonEmptyArray<T>, by?: (item: T) => unknown): NonEmptyReadonlyArray<T>;
|
|
216
|
+
export declare function dedupeArray<T>(array: ReadonlyArray<T> | Array<T>, by?: (item: T) => unknown): ReadonlyArray<T>;
|
|
217
|
+
/**
|
|
218
|
+
* Returns the first element of a non-empty array.
|
|
219
|
+
*
|
|
220
|
+
* Accepts both mutable and readonly arrays. Does not mutate the original array.
|
|
221
|
+
*
|
|
222
|
+
* ### Example
|
|
223
|
+
*
|
|
224
|
+
* ```ts
|
|
225
|
+
* firstInArray(["a", "b", "c"]); // "a"
|
|
226
|
+
* ```
|
|
227
|
+
*
|
|
228
|
+
* @category Accessors
|
|
229
|
+
*/
|
|
230
|
+
export declare const firstInArray: <T>(array: NonEmptyReadonlyArray<T>) => T;
|
|
231
|
+
/**
|
|
232
|
+
* Returns the last element of a non-empty array.
|
|
233
|
+
*
|
|
234
|
+
* Accepts both mutable and readonly arrays. Does not mutate the original array.
|
|
235
|
+
*
|
|
236
|
+
* ### Example
|
|
237
|
+
*
|
|
238
|
+
* ```ts
|
|
239
|
+
* lastInArray(["a", "b", "c"]); // "c"
|
|
240
|
+
* ```
|
|
241
|
+
*
|
|
242
|
+
* @category Accessors
|
|
243
|
+
*/
|
|
244
|
+
export declare const lastInArray: <T>(array: NonEmptyReadonlyArray<T>) => T;
|
|
245
|
+
/**
|
|
246
|
+
* Shifts an item from a non-empty mutable array, guaranteed to return T.
|
|
247
|
+
*
|
|
248
|
+
* **Mutates** the original array.
|
|
249
|
+
*
|
|
250
|
+
* ### Example
|
|
251
|
+
*
|
|
252
|
+
* ```ts
|
|
253
|
+
* const arr: NonEmptyArray<number> = [1, 2, 3];
|
|
254
|
+
* shiftArray(arr); // 1
|
|
255
|
+
* arr; // [2, 3]
|
|
256
|
+
* ```
|
|
257
|
+
*
|
|
258
|
+
* @category Mutations
|
|
259
|
+
*/
|
|
16
260
|
export declare const shiftArray: <T>(array: NonEmptyArray<T>) => T;
|
|
17
261
|
//# sourceMappingURL=Array.d.ts.map
|
package/dist/src/Array.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Array.d.ts","sourceRoot":"","sources":["../../src/Array.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"Array.d.ts","sourceRoot":"","sources":["../../src/Array.ts"],"names":[],"mappings":"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"}
|
package/dist/src/Array.js
CHANGED
|
@@ -1,12 +1,218 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/**
|
|
2
|
+
* 🔒 Type-safe array helpers that do not mutate
|
|
3
|
+
*
|
|
4
|
+
* Array types, guards, operations, transformations, accessors, and (rare)
|
|
5
|
+
* mutations.
|
|
6
|
+
*
|
|
7
|
+
* Functions are intentionally data-first to be prepared for the upcoming
|
|
8
|
+
* JavaScript pipe operator.
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* // Data-first is natural for single operations.
|
|
12
|
+
* const timestamps = mapArray(messages, (m) => m.timestamp);
|
|
13
|
+
*
|
|
14
|
+
* // But data-first can be hard to read for nested calls.
|
|
15
|
+
* const result = firstInArray(
|
|
16
|
+
* mapArray(dedupeArray(appendToArray(value, 2)), (x) => x * 2),
|
|
17
|
+
* );
|
|
18
|
+
*
|
|
19
|
+
* // With the upcoming pipe operator, it's clear.
|
|
20
|
+
* // const result = value
|
|
21
|
+
* // |> appendToArray(%, 2)
|
|
22
|
+
* // |> dedupeArray(%)
|
|
23
|
+
* // |> mapArray(%, (x) => x * 2)
|
|
24
|
+
* // |> firstInArray(%);
|
|
25
|
+
*
|
|
26
|
+
* // Until the pipe operator lands, use nested calls or name each step:
|
|
27
|
+
* const appended = appendToArray(value, 2);
|
|
28
|
+
* const deduped = dedupeArray(appended);
|
|
29
|
+
* const mapped = mapArray(deduped, (x) => x * 2);
|
|
30
|
+
* const result = firstInArray(mapped);
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* ### Why data-first?
|
|
34
|
+
*
|
|
35
|
+
* Evolu optimizes for consistent code style. We can't have both data-first
|
|
36
|
+
* single operations and curried data-last helpers without sacrificing
|
|
37
|
+
* consistency. We chose data-first because:
|
|
38
|
+
*
|
|
39
|
+
* - It's natural for single operations (for example `mapArray(messages, (m) =>
|
|
40
|
+
* m.timestamp)`).
|
|
41
|
+
* - It aligns with the upcoming JavaScript pipe operator.
|
|
42
|
+
*
|
|
43
|
+
* **Note**: Feel free to use Array instance methods (mutation) if you think
|
|
44
|
+
* it's better (performance, local scope, etc.).
|
|
45
|
+
*
|
|
46
|
+
* ### Example
|
|
47
|
+
*
|
|
48
|
+
* ```ts
|
|
49
|
+
* // Types - compile-time guarantee of at least one element
|
|
50
|
+
* const _valid: NonEmptyReadonlyArray<number> = [1, 2, 3];
|
|
51
|
+
* // ts-expect-error - empty array is not a valid NonEmptyReadonlyArray
|
|
52
|
+
* const _invalid: NonEmptyReadonlyArray<number> = [];
|
|
53
|
+
*
|
|
54
|
+
* // Guards
|
|
55
|
+
* const arr: ReadonlyArray<number> = [1, 2, 3];
|
|
56
|
+
* if (isNonEmptyReadonlyArray(arr)) {
|
|
57
|
+
* firstInArray(arr);
|
|
58
|
+
* }
|
|
59
|
+
*
|
|
60
|
+
* // Operations
|
|
61
|
+
* const appended = appendToArray([1, 2, 3], 4); // [1, 2, 3, 4]
|
|
62
|
+
* const prepended = prependToArray([2, 3], 1); // [1, 2, 3]
|
|
63
|
+
*
|
|
64
|
+
* // Transformations
|
|
65
|
+
* const readonly: ReadonlyArray<number> = [1, 2, 3];
|
|
66
|
+
* const mapped = mapArray(readonly, (x) => x * 2); // [2, 4, 6]
|
|
67
|
+
* const filtered = filterArray(readonly, (x) => x > 1); // [2, 3]
|
|
68
|
+
* const deduped = dedupeArray([1, 2, 1, 3, 2]); // [1, 2, 3]
|
|
69
|
+
*
|
|
70
|
+
* // Accessors
|
|
71
|
+
* const first = firstInArray(["a", "b", "c"]); // "a"
|
|
72
|
+
* const last = lastInArray(["a", "b", "c"]); // "c"
|
|
73
|
+
*
|
|
74
|
+
* // Mutations
|
|
75
|
+
* const mutable: NonEmptyArray<number> = [1, 2, 3];
|
|
76
|
+
* shiftArray(mutable); // 1 (guaranteed to exist)
|
|
77
|
+
* mutable; // [2, 3]
|
|
78
|
+
* ```
|
|
79
|
+
*
|
|
80
|
+
* @module
|
|
81
|
+
*/
|
|
82
|
+
/**
|
|
83
|
+
* Checks if an array is non-empty and narrows its type to {@link NonEmptyArray}.
|
|
84
|
+
*
|
|
85
|
+
* Use `if (!isNonEmptyArray(arr))` for empty checks.
|
|
86
|
+
*
|
|
87
|
+
* ### Example
|
|
88
|
+
*
|
|
89
|
+
* ```ts
|
|
90
|
+
* const arr: Array<number> = [1, 2, 3];
|
|
91
|
+
* if (isNonEmptyArray(arr)) {
|
|
92
|
+
* firstInArray(arr); // arr is NonEmptyArray<number>
|
|
93
|
+
* }
|
|
94
|
+
* ```
|
|
95
|
+
*
|
|
96
|
+
* @category Type Guards
|
|
97
|
+
*/
|
|
2
98
|
export const isNonEmptyArray = (array) => array.length > 0;
|
|
3
|
-
/**
|
|
99
|
+
/**
|
|
100
|
+
* Checks if a readonly array is non-empty and narrows its type to
|
|
101
|
+
* {@link NonEmptyReadonlyArray}.
|
|
102
|
+
*
|
|
103
|
+
* Use `if (!isNonEmptyReadonlyArray(arr))` for empty checks.
|
|
104
|
+
*
|
|
105
|
+
* ### Example
|
|
106
|
+
*
|
|
107
|
+
* ```ts
|
|
108
|
+
* const arr: ReadonlyArray<number> = [1, 2, 3];
|
|
109
|
+
* if (isNonEmptyReadonlyArray(arr)) {
|
|
110
|
+
* firstInArray(arr); // arr is NonEmptyReadonlyArray<number>
|
|
111
|
+
* }
|
|
112
|
+
* ```
|
|
113
|
+
*
|
|
114
|
+
* @category Type Guards
|
|
115
|
+
*/
|
|
4
116
|
export const isNonEmptyReadonlyArray = (array) => array.length > 0;
|
|
5
|
-
/**
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
117
|
+
/**
|
|
118
|
+
* Appends an item to an array, returning a new non-empty readonly array.
|
|
119
|
+
*
|
|
120
|
+
* Accepts both mutable and readonly arrays. Does not mutate the original array.
|
|
121
|
+
*
|
|
122
|
+
* ### Example
|
|
123
|
+
*
|
|
124
|
+
* ```ts
|
|
125
|
+
* appendToArray([1, 2, 3], 4); // [1, 2, 3, 4]
|
|
126
|
+
* ```
|
|
127
|
+
*
|
|
128
|
+
* @category Operations
|
|
129
|
+
*/
|
|
130
|
+
export const appendToArray = (array, item) => [...array, item];
|
|
131
|
+
/**
|
|
132
|
+
* Prepends an item to an array, returning a new non-empty readonly array.
|
|
133
|
+
*
|
|
134
|
+
* Accepts both mutable and readonly arrays. Does not mutate the original array.
|
|
135
|
+
*
|
|
136
|
+
* ### Example
|
|
137
|
+
*
|
|
138
|
+
* ```ts
|
|
139
|
+
* prependToArray([2, 3], 1); // [1, 2, 3]
|
|
140
|
+
* ```
|
|
141
|
+
*
|
|
142
|
+
* @category Operations
|
|
143
|
+
*/
|
|
144
|
+
export const prependToArray = (array, item) => [item, ...array];
|
|
145
|
+
export function mapArray(array, mapper) {
|
|
146
|
+
return array.map(mapper);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Filters an array using a predicate function, returning a new readonly array.
|
|
150
|
+
*
|
|
151
|
+
* Accepts both mutable and readonly arrays. Does not mutate the original array.
|
|
152
|
+
*
|
|
153
|
+
* ### Example
|
|
154
|
+
*
|
|
155
|
+
* ```ts
|
|
156
|
+
* filterArray([1, 2, 3, 4, 5], (x) => x % 2 === 0); // [2, 4]
|
|
157
|
+
* ```
|
|
158
|
+
*
|
|
159
|
+
* @category Transformations
|
|
160
|
+
*/
|
|
161
|
+
export const filterArray = (array, predicate) => array.filter(predicate);
|
|
162
|
+
export function dedupeArray(array, by) {
|
|
163
|
+
if (by == null) {
|
|
164
|
+
return Array.from(new Set(array));
|
|
165
|
+
}
|
|
166
|
+
const seen = new Set();
|
|
167
|
+
return array.filter((item) => {
|
|
168
|
+
const key = by(item);
|
|
169
|
+
if (seen.has(key))
|
|
170
|
+
return false;
|
|
171
|
+
seen.add(key);
|
|
172
|
+
return true;
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Returns the first element of a non-empty array.
|
|
177
|
+
*
|
|
178
|
+
* Accepts both mutable and readonly arrays. Does not mutate the original array.
|
|
179
|
+
*
|
|
180
|
+
* ### Example
|
|
181
|
+
*
|
|
182
|
+
* ```ts
|
|
183
|
+
* firstInArray(["a", "b", "c"]); // "a"
|
|
184
|
+
* ```
|
|
185
|
+
*
|
|
186
|
+
* @category Accessors
|
|
187
|
+
*/
|
|
188
|
+
export const firstInArray = (array) => array[0];
|
|
189
|
+
/**
|
|
190
|
+
* Returns the last element of a non-empty array.
|
|
191
|
+
*
|
|
192
|
+
* Accepts both mutable and readonly arrays. Does not mutate the original array.
|
|
193
|
+
*
|
|
194
|
+
* ### Example
|
|
195
|
+
*
|
|
196
|
+
* ```ts
|
|
197
|
+
* lastInArray(["a", "b", "c"]); // "c"
|
|
198
|
+
* ```
|
|
199
|
+
*
|
|
200
|
+
* @category Accessors
|
|
201
|
+
*/
|
|
202
|
+
export const lastInArray = (array) => array[array.length - 1];
|
|
203
|
+
/**
|
|
204
|
+
* Shifts an item from a non-empty mutable array, guaranteed to return T.
|
|
205
|
+
*
|
|
206
|
+
* **Mutates** the original array.
|
|
207
|
+
*
|
|
208
|
+
* ### Example
|
|
209
|
+
*
|
|
210
|
+
* ```ts
|
|
211
|
+
* const arr: NonEmptyArray<number> = [1, 2, 3];
|
|
212
|
+
* shiftArray(arr); // 1
|
|
213
|
+
* arr; // [2, 3]
|
|
214
|
+
* ```
|
|
215
|
+
*
|
|
216
|
+
* @category Mutations
|
|
217
|
+
*/
|
|
12
218
|
export const shiftArray = (array) => array.shift();
|
package/dist/src/Assert.d.ts
CHANGED
|
@@ -55,17 +55,4 @@ export declare const assertNonEmptyArray: <T>(arr: Array<T>, message?: string) =
|
|
|
55
55
|
* ```
|
|
56
56
|
*/
|
|
57
57
|
export declare const assertNonEmptyReadonlyArray: <T>(arr: ReadonlyArray<T>, message?: string) => asserts arr is readonly [T, ...Array<T>];
|
|
58
|
-
/**
|
|
59
|
-
* Asserts no error reaches a `.catch` block, throwing a developer error if it
|
|
60
|
-
* does. Used in Promise chains where errors indicate bugs to be fixed.
|
|
61
|
-
*
|
|
62
|
-
* ### Example
|
|
63
|
-
*
|
|
64
|
-
* ```ts
|
|
65
|
-
* Promise.reject("test").catch((e) =>
|
|
66
|
-
* assertNoErrorInCatch("WebSocket retry", e),
|
|
67
|
-
* );
|
|
68
|
-
* ```
|
|
69
|
-
*/
|
|
70
|
-
export declare function assertNoErrorInCatch(context: string, error: unknown): never;
|
|
71
58
|
//# sourceMappingURL=Assert.d.ts.map
|
package/dist/src/Assert.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Assert.d.ts","sourceRoot":"","sources":["../../src/Assert.ts"],"names":[],"mappings":"AAgBA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,eAAO,MAAM,MAAM,EAAE,CACnB,SAAS,EAAE,OAAO,EAClB,OAAO,EAAE,MAAM,KACZ,OAAO,CAAC,SAIZ,CAAC;AAEF;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,mBAAmB,EAAE,CAAC,CAAC,EAClC,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC,EACb,OAAO,CAAC,EAAE,MAAM,KACb,OAAO,CAAC,GAAG,IAAI,CAAC,CAAC,EAAE,GAAG,KAAK,CAAC,CAAC,CAAC,CAKlC,CAAC;AAEF;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,2BAA2B,EAAE,CAAC,CAAC,EAC1C,GAAG,EAAE,aAAa,CAAC,CAAC,CAAC,EACrB,OAAO,CAAC,EAAE,MAAM,KACb,OAAO,CAAC,GAAG,IAAI,SAAS,CAAC,CAAC,EAAE,GAAG,KAAK,CAAC,CAAC,CAAC,CAK3C,CAAC
|
|
1
|
+
{"version":3,"file":"Assert.d.ts","sourceRoot":"","sources":["../../src/Assert.ts"],"names":[],"mappings":"AAgBA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,eAAO,MAAM,MAAM,EAAE,CACnB,SAAS,EAAE,OAAO,EAClB,OAAO,EAAE,MAAM,KACZ,OAAO,CAAC,SAIZ,CAAC;AAEF;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,mBAAmB,EAAE,CAAC,CAAC,EAClC,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC,EACb,OAAO,CAAC,EAAE,MAAM,KACb,OAAO,CAAC,GAAG,IAAI,CAAC,CAAC,EAAE,GAAG,KAAK,CAAC,CAAC,CAAC,CAKlC,CAAC;AAEF;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,2BAA2B,EAAE,CAAC,CAAC,EAC1C,GAAG,EAAE,aAAa,CAAC,CAAC,CAAC,EACrB,OAAO,CAAC,EAAE,MAAM,KACb,OAAO,CAAC,GAAG,IAAI,SAAS,CAAC,CAAC,EAAE,GAAG,KAAK,CAAC,CAAC,CAAC,CAK3C,CAAC"}
|
package/dist/src/Assert.js
CHANGED
|
@@ -63,18 +63,3 @@ export const assertNonEmptyArray = (arr, message = "Expected a non-empty array."
|
|
|
63
63
|
export const assertNonEmptyReadonlyArray = (arr, message = "Expected a non-empty readonly array.") => {
|
|
64
64
|
assert(arr.length > 0, message);
|
|
65
65
|
};
|
|
66
|
-
/**
|
|
67
|
-
* Asserts no error reaches a `.catch` block, throwing a developer error if it
|
|
68
|
-
* does. Used in Promise chains where errors indicate bugs to be fixed.
|
|
69
|
-
*
|
|
70
|
-
* ### Example
|
|
71
|
-
*
|
|
72
|
-
* ```ts
|
|
73
|
-
* Promise.reject("test").catch((e) =>
|
|
74
|
-
* assertNoErrorInCatch("WebSocket retry", e),
|
|
75
|
-
* );
|
|
76
|
-
* ```
|
|
77
|
-
*/
|
|
78
|
-
export function assertNoErrorInCatch(context, error) {
|
|
79
|
-
throw Object.assign(new Error(`Error in ${context}: an unexpected error reached a catch block and requires a fix`), { cause: error });
|
|
80
|
-
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A utility interface for creating branded types.
|
|
3
|
+
*
|
|
4
|
+
* Branded types enhance type safety by differentiating otherwise identical base
|
|
5
|
+
* types, such as `number` or `string`, to enforce stricter type checks.
|
|
6
|
+
*
|
|
7
|
+
* Supports multiple brands, allowing types to act like flags.
|
|
8
|
+
*
|
|
9
|
+
* ### Example 1: Single Brand
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* // A branded type definition
|
|
13
|
+
* type UserId = number & Brand<"UserId">;
|
|
14
|
+
*
|
|
15
|
+
* // A function that creates `UserId` values.
|
|
16
|
+
* // Casting with `as UserId` is unsafe, so `createUserId` must be unit-tested.
|
|
17
|
+
* const createUserId = (): UserId => {
|
|
18
|
+
* return 123 as UserId; // Unsafe casting
|
|
19
|
+
* };
|
|
20
|
+
*
|
|
21
|
+
* const userId = createUserId();
|
|
22
|
+
*
|
|
23
|
+
* // A function that accepts only `UserId`.
|
|
24
|
+
* const getUser = (id: UserId) => {
|
|
25
|
+
* // Implementation
|
|
26
|
+
* };
|
|
27
|
+
*
|
|
28
|
+
* getUser(userId); // ✅ Valid
|
|
29
|
+
* getUser(123); // ❌ TypeScript error
|
|
30
|
+
* getUser("123"); // ❌ TypeScript error
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* ### Example 2: Multiple Brands
|
|
34
|
+
*
|
|
35
|
+
* ```ts
|
|
36
|
+
* // Define branded types
|
|
37
|
+
* type Min1 = string & Brand<"Min1">;
|
|
38
|
+
* type Max100 = string & Brand<"Max100">;
|
|
39
|
+
* type Min1Max100 = string & Brand<"Min1" | "Max100">;
|
|
40
|
+
*
|
|
41
|
+
* // Functions requiring specific brands
|
|
42
|
+
* const requiresMin1 = (value: Min1): void => {};
|
|
43
|
+
* const requiresMax100 = (value: Max100): void => {};
|
|
44
|
+
*
|
|
45
|
+
* // Values with single brands
|
|
46
|
+
* const min1Value: Min1 = "hello" as Min1;
|
|
47
|
+
* const max100Value: Max100 = "world" as Max100;
|
|
48
|
+
*
|
|
49
|
+
* // Value with multiple brands
|
|
50
|
+
* const min1Max100Value: Min1Max100 = "typescript" as Min1Max100;
|
|
51
|
+
*
|
|
52
|
+
* // Valid cases
|
|
53
|
+
* requiresMin1(min1Value); // ✅ Valid
|
|
54
|
+
* requiresMax100(max100Value); // ✅ Valid
|
|
55
|
+
* requiresMin1(min1Max100Value); // ✅ Valid: Min1Max100 satisfies Min1
|
|
56
|
+
* requiresMax100(min1Max100Value); // ✅ Valid: Min1Max100 satisfies Max100
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
export interface Brand<B extends string> {
|
|
60
|
+
readonly [__brand]: Readonly<Record<B, true>>;
|
|
61
|
+
}
|
|
62
|
+
declare const __brand: unique symbol;
|
|
63
|
+
/**
|
|
64
|
+
* Determines whether a type `T` is a branded type.
|
|
65
|
+
*
|
|
66
|
+
* Works with any base type intersected with a `Brand`.
|
|
67
|
+
*
|
|
68
|
+
* ### Examples
|
|
69
|
+
*
|
|
70
|
+
* - `IsBranded<string>` -> false
|
|
71
|
+
* - `IsBranded<string & Brand<"X">>` -> true
|
|
72
|
+
*/
|
|
73
|
+
export type IsBranded<T> = T extends Brand<string> ? true : false;
|
|
74
|
+
export {};
|
|
75
|
+
//# sourceMappingURL=Brand.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Brand.d.ts","sourceRoot":"","sources":["../../src/Brand.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyDG;AACH,MAAM,WAAW,KAAK,CAAC,CAAC,SAAS,MAAM;IACrC,QAAQ,CAAC,CAAC,OAAO,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC;CAC/C;AAED,OAAO,CAAC,MAAM,OAAO,EAAE,OAAO,MAAM,CAAC;AAErC;;;;;;;;;GASG;AACH,MAAM,MAAM,SAAS,CAAC,CAAC,IAAI,CAAC,SAAS,KAAK,CAAC,MAAM,CAAC,GAAG,IAAI,GAAG,KAAK,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/src/Buffer.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { NonNegativeInt } from "./Type.js";
|
|
2
|
-
export { bytesToHex, bytesToUtf8, concatBytes, hexToBytes, utf8ToBytes, } from "@noble/ciphers/utils";
|
|
2
|
+
export { bytesToHex, bytesToUtf8, concatBytes, hexToBytes, utf8ToBytes, } from "@noble/ciphers/utils.js";
|
|
3
3
|
/**
|
|
4
4
|
* Custom error for {@link Buffer}-related failures like premature end of data.
|
|
5
5
|
* Provides better stack traces for debugging binary protocol issues.
|
package/dist/src/Buffer.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Buffer.d.ts","sourceRoot":"","sources":["../../src/Buffer.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,EACL,UAAU,EACV,WAAW,EACX,WAAW,EACX,UAAU,EACV,WAAW,GACZ,MAAM,
|
|
1
|
+
{"version":3,"file":"Buffer.d.ts","sourceRoot":"","sources":["../../src/Buffer.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,EACL,UAAU,EACV,WAAW,EACX,WAAW,EACX,UAAU,EACV,WAAW,GACZ,MAAM,yBAAyB,CAAC;AAEjC;;;GAGG;AACH,qBAAa,WAAY,SAAQ,KAAK;gBACxB,OAAO,EAAE,MAAM;CAM5B;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,MAAM,WAAW,MAAM;IACrB,sDAAsD;IACtD,WAAW,EAAE,MAAM,cAAc,CAAC;IAElC,gEAAgE;IAChE,SAAS,EAAE,MAAM,cAAc,CAAC;IAEhC;;;OAGG;IACH,MAAM,EAAE,CAAC,GAAG,EAAE,UAAU,GAAG,SAAS,CAAC,MAAM,CAAC,KAAK,IAAI,CAAC;IAEtD;;;OAGG;IACH,KAAK,EAAE,MAAM,cAAc,CAAC;IAE5B;;;OAGG;IACH,MAAM,EAAE,CAAC,CAAC,EAAE,cAAc,KAAK,UAAU,CAAC;IAE1C;;;OAGG;IACH,QAAQ,EAAE,CAAC,MAAM,EAAE,cAAc,KAAK,IAAI,CAAC;IAE3C;;;;;;OAMG;IACH,KAAK,EAAE,MAAM,IAAI,CAAC;IAElB;;;;OAIG;IACH,MAAM,EAAE,MAAM,UAAU,CAAC;CAC1B;AAED,eAAO,MAAM,YAAY,GACvB,YAAY,UAAU,GAAG,SAAS,CAAC,MAAM,CAAC,KACzC,MA4DF,CAAC"}
|