@evolu/common 7.4.0 → 8.0.0-next.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 +4 -61
- package/dist/src/Array.d.ts +343 -102
- package/dist/src/Array.d.ts.map +1 -1
- package/dist/src/Array.js +181 -85
- package/dist/src/Assert.d.ts +57 -11
- package/dist/src/Assert.d.ts.map +1 -1
- package/dist/src/Assert.js +47 -11
- package/dist/src/BigInt.d.ts +6 -1
- package/dist/src/BigInt.d.ts.map +1 -1
- package/dist/src/BigInt.js +5 -0
- package/dist/src/Brand.d.ts +40 -12
- package/dist/src/Brand.d.ts.map +1 -1
- package/dist/src/Brand.js +5 -0
- package/dist/src/Buffer.d.ts +6 -1
- package/dist/src/Buffer.d.ts.map +1 -1
- package/dist/src/Buffer.js +7 -2
- package/dist/src/Cache.d.ts +10 -2
- package/dist/src/Cache.d.ts.map +1 -1
- package/dist/src/Cache.js +6 -0
- package/dist/src/Callbacks.d.ts +10 -7
- package/dist/src/Callbacks.d.ts.map +1 -1
- package/dist/src/Callbacks.js +23 -13
- package/dist/src/Console.d.ts +361 -69
- package/dist/src/Console.d.ts.map +1 -1
- package/dist/src/Console.js +217 -117
- package/dist/src/Crypto.d.ts +70 -21
- package/dist/src/Crypto.d.ts.map +1 -1
- package/dist/src/Crypto.js +72 -27
- package/dist/src/Eq.d.ts +22 -2
- package/dist/src/Eq.d.ts.map +1 -1
- package/dist/src/Eq.js +21 -1
- package/dist/src/Error.d.ts +23 -12
- package/dist/src/Error.d.ts.map +1 -1
- package/dist/src/Error.js +27 -11
- package/dist/src/Function.d.ts +128 -56
- package/dist/src/Function.d.ts.map +1 -1
- package/dist/src/Function.js +118 -14
- package/dist/src/Identicon.d.ts +5 -0
- package/dist/src/Identicon.d.ts.map +1 -1
- package/dist/src/Identicon.js +6 -1
- package/dist/src/Lookup.d.ts +160 -0
- package/dist/src/Lookup.d.ts.map +1 -0
- package/dist/src/Lookup.js +192 -0
- package/dist/src/Microtask.d.ts +21 -0
- package/dist/src/Microtask.d.ts.map +1 -0
- package/dist/src/Microtask.js +37 -0
- package/dist/src/Number.d.ts +18 -4
- package/dist/src/Number.d.ts.map +1 -1
- package/dist/src/Number.js +23 -1
- package/dist/src/Object.d.ts +110 -5
- package/dist/src/Object.d.ts.map +1 -1
- package/dist/src/Object.js +108 -5
- package/dist/src/Option.d.ts +58 -0
- package/dist/src/Option.d.ts.map +1 -0
- package/dist/src/Option.js +43 -0
- package/dist/src/Order.d.ts +5 -0
- package/dist/src/Order.d.ts.map +1 -1
- package/dist/src/Order.js +8 -3
- package/dist/src/Platform.d.ts +36 -2
- package/dist/src/Platform.d.ts.map +1 -1
- package/dist/src/Platform.js +10 -6
- package/dist/src/Polyfills.d.ts +27 -0
- package/dist/src/Polyfills.d.ts.map +1 -0
- package/dist/src/Polyfills.js +299 -0
- package/dist/src/Random.d.ts +18 -7
- package/dist/src/Random.d.ts.map +1 -1
- package/dist/src/Random.js +9 -7
- package/dist/src/Redacted.d.ts +7 -4
- package/dist/src/Redacted.d.ts.map +1 -1
- package/dist/src/Redacted.js +5 -0
- package/dist/src/Ref.d.ts +31 -16
- package/dist/src/Ref.d.ts.map +1 -1
- package/dist/src/Ref.js +35 -7
- package/dist/src/RefCount.d.ts +62 -0
- package/dist/src/RefCount.d.ts.map +1 -0
- package/dist/src/RefCount.js +83 -0
- package/dist/src/Relation.d.ts +84 -29
- package/dist/src/Relation.d.ts.map +1 -1
- package/dist/src/Relation.js +83 -105
- package/dist/src/Resource.d.ts +263 -0
- package/dist/src/Resource.d.ts.map +1 -0
- package/dist/src/Resource.js +389 -0
- package/dist/src/Result.d.ts +390 -374
- package/dist/src/Result.d.ts.map +1 -1
- package/dist/src/Result.js +142 -70
- package/dist/src/Schedule.d.ts +953 -0
- package/dist/src/Schedule.d.ts.map +1 -0
- package/dist/src/Schedule.js +1199 -0
- package/dist/src/Set.d.ts +181 -0
- package/dist/src/Set.d.ts.map +1 -0
- package/dist/src/Set.js +137 -0
- package/dist/src/Skiplist.d.ts +6 -1
- package/dist/src/Skiplist.d.ts.map +1 -1
- package/dist/src/Skiplist.js +5 -0
- package/dist/src/Sqlite.d.ts +160 -60
- package/dist/src/Sqlite.d.ts.map +1 -1
- package/dist/src/Sqlite.js +290 -190
- package/dist/src/Store.d.ts +36 -28
- package/dist/src/Store.d.ts.map +1 -1
- package/dist/src/Store.js +55 -15
- package/dist/src/String.d.ts +5 -0
- package/dist/src/String.d.ts.map +1 -1
- package/dist/src/String.js +5 -0
- package/dist/src/Task.d.ts +2263 -423
- package/dist/src/Task.d.ts.map +1 -1
- package/dist/src/Task.js +1488 -348
- package/dist/src/Test.d.ts +122 -0
- package/dist/src/Test.d.ts.map +1 -0
- package/dist/src/Test.js +66 -0
- package/dist/src/Time.d.ts +201 -48
- package/dist/src/Time.d.ts.map +1 -1
- package/dist/src/Time.js +190 -86
- package/dist/src/Tracer.d.ts +48 -0
- package/dist/src/Tracer.d.ts.map +1 -0
- package/dist/src/Tracer.js +6 -0
- package/dist/src/Type.d.ts +582 -209
- package/dist/src/Type.d.ts.map +1 -1
- package/dist/src/Type.js +362 -240
- package/dist/src/Types.d.ts +160 -6
- package/dist/src/Types.d.ts.map +1 -1
- package/dist/src/Types.js +22 -2
- package/dist/src/WebSocket.d.ts +113 -58
- package/dist/src/WebSocket.d.ts.map +1 -1
- package/dist/src/WebSocket.js +210 -118
- package/dist/src/Worker.d.ts +245 -60
- package/dist/src/Worker.d.ts.map +1 -1
- package/dist/src/Worker.js +220 -64
- package/dist/src/index.d.ts +25 -3
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +19 -3
- package/dist/src/local-first/Db.d.ts +24 -213
- package/dist/src/local-first/Db.d.ts.map +1 -1
- package/dist/src/local-first/Db.js +622 -293
- package/dist/src/local-first/Error.d.ts +12 -0
- package/dist/src/local-first/Error.d.ts.map +1 -0
- package/dist/src/local-first/Error.js +6 -0
- package/dist/src/local-first/Evolu.d.ts +330 -285
- package/dist/src/local-first/Evolu.d.ts.map +1 -1
- package/dist/src/local-first/Evolu.js +472 -402
- package/dist/src/local-first/LocalAuth.d.ts +10 -10
- package/dist/src/local-first/LocalAuth.d.ts.map +1 -1
- package/dist/src/local-first/LocalAuth.js +6 -1
- package/dist/src/local-first/Owner.d.ts +70 -27
- package/dist/src/local-first/Owner.d.ts.map +1 -1
- package/dist/src/local-first/Owner.js +27 -14
- package/dist/src/local-first/Protocol.d.ts +58 -50
- package/dist/src/local-first/Protocol.d.ts.map +1 -1
- package/dist/src/local-first/Protocol.js +253 -155
- package/dist/src/local-first/Query.d.ts +133 -43
- package/dist/src/local-first/Query.d.ts.map +1 -1
- package/dist/src/local-first/Query.js +139 -90
- package/dist/src/local-first/Relay.d.ts +18 -38
- package/dist/src/local-first/Relay.d.ts.map +1 -1
- package/dist/src/local-first/Relay.js +59 -171
- package/dist/src/local-first/Schema.d.ts +139 -144
- package/dist/src/local-first/Schema.d.ts.map +1 -1
- package/dist/src/local-first/Schema.js +78 -163
- package/dist/src/local-first/Shared.d.ts +125 -0
- package/dist/src/local-first/Shared.d.ts.map +1 -0
- package/dist/src/local-first/Shared.js +569 -0
- package/dist/src/local-first/Storage.d.ts +50 -53
- package/dist/src/local-first/Storage.d.ts.map +1 -1
- package/dist/src/local-first/Storage.js +121 -173
- package/dist/src/local-first/Timestamp.d.ts +28 -35
- package/dist/src/local-first/Timestamp.d.ts.map +1 -1
- package/dist/src/local-first/Timestamp.js +24 -25
- package/dist/src/local-first/index.d.ts +2 -15
- package/dist/src/local-first/index.d.ts.map +1 -1
- package/dist/src/local-first/index.js +2 -15
- package/package.json +36 -26
- package/src/Array.ts +467 -106
- package/src/Assert.ts +87 -12
- package/src/BigInt.ts +7 -1
- package/src/Brand.ts +41 -12
- package/src/Buffer.ts +10 -4
- package/src/Cache.ts +11 -2
- package/src/Callbacks.ts +33 -23
- package/src/Console.ts +567 -178
- package/src/Crypto.ts +111 -62
- package/src/Eq.ts +30 -3
- package/src/Error.ts +50 -34
- package/src/Function.ts +140 -68
- package/src/Identicon.ts +7 -1
- package/src/Lookup.ts +415 -0
- package/src/Microtask.ts +58 -0
- package/src/Number.ts +40 -7
- package/src/Object.ts +155 -16
- package/src/Option.ts +74 -0
- package/src/Order.ts +11 -3
- package/src/Platform.ts +43 -7
- package/src/Polyfills.ts +465 -0
- package/src/Random.ts +25 -11
- package/src/Redacted.ts +8 -4
- package/src/Ref.ts +72 -21
- package/src/RefCount.ts +170 -0
- package/src/Relation.ts +188 -136
- package/src/Resource.ts +864 -0
- package/src/Result.ts +548 -381
- package/src/Schedule.ts +1471 -0
- package/src/Set.ts +247 -0
- package/src/Skiplist.ts +7 -1
- package/src/Sqlite.ts +409 -304
- package/src/Store.ts +94 -48
- package/src/String.ts +6 -0
- package/src/Task.ts +3685 -740
- package/src/Test.ts +162 -0
- package/src/Time.ts +348 -145
- package/src/Tracer.ts +54 -0
- package/src/Type.ts +1064 -630
- package/src/Types.ts +202 -7
- package/src/WebSocket.ts +311 -178
- package/src/Worker.ts +527 -147
- package/src/index.ts +74 -3
- package/src/local-first/Db.ts +871 -597
- package/src/local-first/Error.ts +17 -0
- package/src/local-first/Evolu.ts +884 -823
- package/src/local-first/LocalAuth.ts +10 -10
- package/src/local-first/Owner.ts +94 -42
- package/src/local-first/Protocol.ts +440 -303
- package/src/local-first/Query.ts +243 -194
- package/src/local-first/Relay.ts +91 -252
- package/src/local-first/Schema.ts +284 -394
- package/src/local-first/Shared.ts +932 -0
- package/src/local-first/Storage.ts +209 -282
- package/src/local-first/Timestamp.ts +38 -45
- package/src/local-first/index.ts +2 -16
- package/LICENSE +0 -21
- package/dist/src/Instances.d.ts +0 -34
- package/dist/src/Instances.d.ts.map +0 -1
- package/dist/src/Instances.js +0 -44
- package/dist/src/Resources.d.ts +0 -118
- package/dist/src/Resources.d.ts.map +0 -1
- package/dist/src/Resources.js +0 -197
- package/dist/src/local-first/Platform.d.ts +0 -25
- package/dist/src/local-first/Platform.d.ts.map +0 -1
- package/dist/src/local-first/Platform.js +0 -1
- package/dist/src/local-first/Public.d.ts +0 -22
- package/dist/src/local-first/Public.d.ts.map +0 -1
- package/dist/src/local-first/Public.js +0 -15
- package/dist/src/local-first/PublicKysely.d.ts +0 -148
- package/dist/src/local-first/PublicKysely.d.ts.map +0 -1
- package/dist/src/local-first/PublicKysely.js +0 -184
- package/dist/src/local-first/Sync.d.ts +0 -112
- package/dist/src/local-first/Sync.d.ts.map +0 -1
- package/dist/src/local-first/Sync.js +0 -529
- package/src/Instances.ts +0 -90
- package/src/Resources.ts +0 -367
- package/src/local-first/Platform.ts +0 -27
- package/src/local-first/Public.ts +0 -42
- package/src/local-first/PublicKysely.ts +0 -239
- package/src/local-first/Sync.ts +0 -960
package/src/Set.ts
ADDED
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Set helpers.
|
|
3
|
+
*
|
|
4
|
+
* All helpers return readonly sets for safety. Native Set methods like `add()`
|
|
5
|
+
* and `delete()` mutate the original — use {@link addToSet} and
|
|
6
|
+
* {@link deleteFromSet} instead for immutable operations that return new sets.
|
|
7
|
+
*
|
|
8
|
+
* Use {@link isNonEmptySet} to narrow to {@link NonEmptyReadonlySet} before
|
|
9
|
+
* calling functions like {@link firstInSet} that require a non-empty set.
|
|
10
|
+
*
|
|
11
|
+
* ### Example
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* // Type guards
|
|
15
|
+
* const set: ReadonlySet<number> = new Set([1, 2, 3]);
|
|
16
|
+
* if (isNonEmptySet(set)) {
|
|
17
|
+
* firstInSet(set);
|
|
18
|
+
* }
|
|
19
|
+
*
|
|
20
|
+
* // Immutable transformations
|
|
21
|
+
* const added = addToSet(new Set([1, 2]), 3); // Set {1, 2, 3}
|
|
22
|
+
* const removed = deleteFromSet(new Set([1, 2, 3]), 2); // Set {1, 3}
|
|
23
|
+
* const mapped = mapSet(new Set([1, 2, 3]), (x) => x * 2); // Set {2, 4, 6}
|
|
24
|
+
* const filtered = filterSet(new Set([1, 2, 3, 4]), (x) => x % 2 === 0); // Set {2, 4}
|
|
25
|
+
*
|
|
26
|
+
* // Set operations
|
|
27
|
+
* const union = unionSets(new Set([1, 2]), new Set([2, 3])); // Set {1, 2, 3}
|
|
28
|
+
* const intersection = intersectSets(new Set([1, 2]), new Set([2, 3])); // Set {2}
|
|
29
|
+
* const difference = differenceSets(new Set([1, 2, 3]), new Set([2])); // Set {1, 3}
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* @module
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
import type { NonEmptyReadonlyArray } from "./Array.js";
|
|
36
|
+
import type { Brand } from "./Brand.js";
|
|
37
|
+
import type { PredicateWithIndex, RefinementWithIndex } from "./Types.js";
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* An empty readonly set.
|
|
41
|
+
*
|
|
42
|
+
* Use as a default or initial value to avoid allocating new empty sets.
|
|
43
|
+
*
|
|
44
|
+
* @group Constants
|
|
45
|
+
*/
|
|
46
|
+
export const emptySet: ReadonlySet<never> = /*#__PURE__*/ new Set();
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Creates a readonly set from an array.
|
|
50
|
+
*
|
|
51
|
+
* Preserves non-empty type when the input array is non-empty.
|
|
52
|
+
*
|
|
53
|
+
* ### Example
|
|
54
|
+
*
|
|
55
|
+
* ```ts
|
|
56
|
+
* createSet([1, 2, 3]); // NonEmptyReadonlySet<number>
|
|
57
|
+
* createSet([] as ReadonlyArray<number>); // ReadonlySet<number>
|
|
58
|
+
* ```
|
|
59
|
+
*
|
|
60
|
+
* @group Constructors
|
|
61
|
+
*/
|
|
62
|
+
export function createSet<T>(
|
|
63
|
+
items: NonEmptyReadonlyArray<T>,
|
|
64
|
+
): NonEmptyReadonlySet<T>;
|
|
65
|
+
/** Possibly empty set. */
|
|
66
|
+
export function createSet<T>(items: ReadonlyArray<T>): ReadonlySet<T>;
|
|
67
|
+
export function createSet<T>(items: ReadonlyArray<T>): ReadonlySet<T> {
|
|
68
|
+
return new Set(items) as ReadonlySet<T>;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* A readonly set with at least one element (branded for type safety).
|
|
73
|
+
*
|
|
74
|
+
* Use {@link isNonEmptySet} to narrow from `ReadonlySet`, or use functions like
|
|
75
|
+
* {@link addToSet} that return branded non-empty sets.
|
|
76
|
+
*
|
|
77
|
+
* There is no mutable `NonEmptySet` type because mutable sets can be emptied
|
|
78
|
+
* after narrowing (via `clear()` or `delete()`), making compile-time guarantees
|
|
79
|
+
* impossible.
|
|
80
|
+
*
|
|
81
|
+
* @group Types
|
|
82
|
+
*/
|
|
83
|
+
export type NonEmptyReadonlySet<T> = ReadonlySet<T> & Brand<"NonEmpty">;
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Checks if a set is non-empty and narrows its type to
|
|
87
|
+
* {@link NonEmptyReadonlySet}.
|
|
88
|
+
*
|
|
89
|
+
* Both mutable and readonly sets narrow to the branded
|
|
90
|
+
* {@link NonEmptyReadonlySet} type, which can be used with functions like
|
|
91
|
+
* {@link firstInSet}.
|
|
92
|
+
*
|
|
93
|
+
* To check if a set is empty, use `if (!isNonEmptySet(set))` — using the
|
|
94
|
+
* negated guard is better than `.size === 0` for early returns because
|
|
95
|
+
* TypeScript narrows the type after the check.
|
|
96
|
+
*
|
|
97
|
+
* ### Example
|
|
98
|
+
*
|
|
99
|
+
* ```ts
|
|
100
|
+
* const set: ReadonlySet<number> = new Set([1, 2, 3]);
|
|
101
|
+
* if (isNonEmptySet(set)) {
|
|
102
|
+
* firstInSet(set); // set is NonEmptyReadonlySet<number>
|
|
103
|
+
* }
|
|
104
|
+
* ```
|
|
105
|
+
*
|
|
106
|
+
* @group Type guards
|
|
107
|
+
*/
|
|
108
|
+
export const isNonEmptySet = <T>(
|
|
109
|
+
set: ReadonlySet<T>,
|
|
110
|
+
): set is NonEmptyReadonlySet<T> => set.size > 0;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Returns a new readonly set with an item added.
|
|
114
|
+
*
|
|
115
|
+
* If the item already exists, returns a new set with the same elements (still a
|
|
116
|
+
* new reference for change detection).
|
|
117
|
+
*
|
|
118
|
+
* ### Example
|
|
119
|
+
*
|
|
120
|
+
* ```ts
|
|
121
|
+
* addToSet(new Set([1, 2]), 3); // Set {1, 2, 3}
|
|
122
|
+
* addToSet(new Set([1, 2]), 2); // Set {1, 2} (new reference)
|
|
123
|
+
* ```
|
|
124
|
+
*
|
|
125
|
+
* @group Transformations
|
|
126
|
+
*/
|
|
127
|
+
export const addToSet = <T>(
|
|
128
|
+
set: ReadonlySet<T>,
|
|
129
|
+
item: T,
|
|
130
|
+
): NonEmptyReadonlySet<T> => {
|
|
131
|
+
const next = new Set(set);
|
|
132
|
+
next.add(item);
|
|
133
|
+
return next as ReadonlySet<T> as NonEmptyReadonlySet<T>;
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Returns a new readonly set with an item removed.
|
|
138
|
+
*
|
|
139
|
+
* If the item doesn't exist, returns a new set with the same elements (still a
|
|
140
|
+
* new reference for change detection).
|
|
141
|
+
*
|
|
142
|
+
* ### Example
|
|
143
|
+
*
|
|
144
|
+
* ```ts
|
|
145
|
+
* deleteFromSet(new Set([1, 2, 3]), 2); // Set {1, 3}
|
|
146
|
+
* deleteFromSet(new Set([1, 2]), 5); // Set {1, 2} (new reference)
|
|
147
|
+
* ```
|
|
148
|
+
*
|
|
149
|
+
* @group Transformations
|
|
150
|
+
*/
|
|
151
|
+
export const deleteFromSet = <T>(
|
|
152
|
+
set: ReadonlySet<T>,
|
|
153
|
+
item: T,
|
|
154
|
+
): ReadonlySet<T> => {
|
|
155
|
+
const next = new Set(set);
|
|
156
|
+
next.delete(item);
|
|
157
|
+
return next as ReadonlySet<T>;
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Maps a set using a mapper function, returning a new readonly set.
|
|
162
|
+
*
|
|
163
|
+
* Preserves non-empty type.
|
|
164
|
+
*
|
|
165
|
+
* Note: If the mapper produces duplicate values, the resulting set will have
|
|
166
|
+
* fewer elements.
|
|
167
|
+
*
|
|
168
|
+
* ### Example
|
|
169
|
+
*
|
|
170
|
+
* ```ts
|
|
171
|
+
* mapSet(new Set([1, 2, 3]), (x) => x * 2); // Set {2, 4, 6}
|
|
172
|
+
* mapSet(new Set([1, 2, 3]), (x) => x % 2); // Set {1, 0} (duplicates merged)
|
|
173
|
+
* ```
|
|
174
|
+
*
|
|
175
|
+
* @group Transformations
|
|
176
|
+
*/
|
|
177
|
+
export function mapSet<T, U>(
|
|
178
|
+
set: NonEmptyReadonlySet<T>,
|
|
179
|
+
mapper: (item: T) => U,
|
|
180
|
+
): NonEmptyReadonlySet<U>;
|
|
181
|
+
/** Possibly empty set. */
|
|
182
|
+
export function mapSet<T, U>(
|
|
183
|
+
set: ReadonlySet<T>,
|
|
184
|
+
mapper: (item: T) => U,
|
|
185
|
+
): ReadonlySet<U>;
|
|
186
|
+
export function mapSet<T, U>(
|
|
187
|
+
set: ReadonlySet<T>,
|
|
188
|
+
mapper: (item: T) => U,
|
|
189
|
+
): ReadonlySet<U> {
|
|
190
|
+
const next = new Set<U>();
|
|
191
|
+
for (const item of set) {
|
|
192
|
+
next.add(mapper(item));
|
|
193
|
+
}
|
|
194
|
+
return next as ReadonlySet<U>;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Filters a set using a predicate or refinement function, returning a new
|
|
199
|
+
* readonly set.
|
|
200
|
+
*
|
|
201
|
+
* When used with a refinement function (with `value is Type` syntax),
|
|
202
|
+
* TypeScript will narrow the result type.
|
|
203
|
+
*
|
|
204
|
+
* ### Example
|
|
205
|
+
*
|
|
206
|
+
* ```ts
|
|
207
|
+
* filterSet(new Set([1, 2, 3, 4, 5]), (x) => x % 2 === 0); // Set {2, 4}
|
|
208
|
+
* ```
|
|
209
|
+
*
|
|
210
|
+
* @group Transformations
|
|
211
|
+
*/
|
|
212
|
+
export function filterSet<T, S extends T>(
|
|
213
|
+
set: ReadonlySet<T>,
|
|
214
|
+
refinement: RefinementWithIndex<T, S>,
|
|
215
|
+
): ReadonlySet<S>;
|
|
216
|
+
/** With predicate. */
|
|
217
|
+
export function filterSet<T>(
|
|
218
|
+
set: ReadonlySet<T>,
|
|
219
|
+
predicate: PredicateWithIndex<T>,
|
|
220
|
+
): ReadonlySet<T>;
|
|
221
|
+
export function filterSet<T>(
|
|
222
|
+
set: ReadonlySet<T>,
|
|
223
|
+
predicate: PredicateWithIndex<T>,
|
|
224
|
+
): ReadonlySet<T> {
|
|
225
|
+
const next = new Set<T>();
|
|
226
|
+
let index = 0;
|
|
227
|
+
for (const item of set) {
|
|
228
|
+
if (predicate(item, index++)) {
|
|
229
|
+
next.add(item);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return next as ReadonlySet<T>;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Returns the first element of a non-empty set (by insertion order).
|
|
237
|
+
*
|
|
238
|
+
* ### Example
|
|
239
|
+
*
|
|
240
|
+
* ```ts
|
|
241
|
+
* firstInSet(new Set(["a", "b", "c"])); // "a"
|
|
242
|
+
* ```
|
|
243
|
+
*
|
|
244
|
+
* @group Accessors
|
|
245
|
+
*/
|
|
246
|
+
export const firstInSet = <T>(set: NonEmptyReadonlySet<T>): T =>
|
|
247
|
+
set.values().next().value as T;
|
package/src/Skiplist.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Skiplist data structure with probabilistic level generation.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { RandomDep } from "./Random.js";
|
|
2
8
|
import { PositiveInt } from "./Type.js";
|
|
3
9
|
|
|
4
10
|
export interface SkiplistLevel {
|