@byloth/core 2.0.0 → 2.0.2

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 (39) hide show
  1. package/README.md +1 -0
  2. package/dist/core.js +900 -187
  3. package/dist/core.js.map +1 -1
  4. package/dist/core.umd.cjs +2 -2
  5. package/dist/core.umd.cjs.map +1 -1
  6. package/package.json +13 -10
  7. package/src/core/types.ts +43 -10
  8. package/src/index.ts +3 -2
  9. package/src/models/aggregators/aggregated-async-iterator.ts +161 -1
  10. package/src/models/aggregators/aggregated-iterator.ts +146 -1
  11. package/src/models/aggregators/reduced-iterator.ts +148 -8
  12. package/src/models/aggregators/types.ts +35 -0
  13. package/src/models/callbacks/callable-object.ts +7 -0
  14. package/src/models/callbacks/publisher.ts +33 -8
  15. package/src/models/callbacks/switchable-callback.ts +102 -21
  16. package/src/models/callbacks/types.ts +32 -0
  17. package/src/models/exceptions/core.ts +29 -0
  18. package/src/models/exceptions/index.ts +105 -1
  19. package/src/models/iterators/smart-async-iterator.ts +145 -0
  20. package/src/models/iterators/smart-iterator.ts +130 -0
  21. package/src/models/iterators/types.ts +79 -1
  22. package/src/models/json/json-storage.ts +123 -0
  23. package/src/models/json/types.ts +1 -1
  24. package/src/models/promises/deferred-promise.ts +15 -0
  25. package/src/models/promises/smart-promise.ts +45 -0
  26. package/src/models/promises/timed-promise.ts +10 -0
  27. package/src/models/promises/types.ts +30 -0
  28. package/src/models/timers/clock.ts +21 -0
  29. package/src/models/timers/countdown.ts +30 -0
  30. package/src/models/timers/game-loop.ts +26 -0
  31. package/src/models/types.ts +1 -1
  32. package/src/utils/async.ts +15 -0
  33. package/src/utils/curve.ts +11 -1
  34. package/src/utils/date.ts +36 -6
  35. package/src/utils/dom.ts +5 -0
  36. package/src/utils/iterator.ts +40 -0
  37. package/src/utils/math.ts +15 -0
  38. package/src/utils/random.ts +34 -0
  39. package/src/utils/string.ts +5 -0
@@ -3,6 +3,9 @@ import type { MaybePromise } from "../promises/types.js";
3
3
  /**
4
4
  * An union type that represents an iterable object that can be either synchronous or asynchronous.
5
5
  *
6
+ * ---
7
+ *
8
+ * @example
6
9
  * ```ts
7
10
  * const iterable: MaybeAsyncIterable<number> = [...];
8
11
  * for await (const value of iterable)
@@ -11,6 +14,8 @@ import type { MaybePromise } from "../promises/types.js";
11
14
  * }
12
15
  * ```
13
16
  *
17
+ * ---
18
+ *
14
19
  * @template T The type of the elements in the iterable.
15
20
  */
16
21
  export type MaybeAsyncIterable<T, R = void, N = undefined> = Iterable<T, R, N> | AsyncIterable<T, R, N>;
@@ -18,6 +23,9 @@ export type MaybeAsyncIterable<T, R = void, N = undefined> = Iterable<T, R, N> |
18
23
  /**
19
24
  * An union type that represents an iterator object that can be either synchronous or asynchronous.
20
25
  *
26
+ * ---
27
+ *
28
+ * @example
21
29
  * ```ts
22
30
  * const iterator: MaybeAsyncIterator<number> = { ... };
23
31
  * for await (const value of iterator)
@@ -26,6 +34,8 @@ export type MaybeAsyncIterable<T, R = void, N = undefined> = Iterable<T, R, N> |
26
34
  * }
27
35
  * ```
28
36
  *
37
+ * ---
38
+ *
29
39
  * @template T The type of the elements in the iterator.
30
40
  */
31
41
  export type MaybeAsyncIterator<T, R = void, N = undefined> = Iterator<T, R, N> | AsyncIterator<T, R, N>;
@@ -33,6 +43,9 @@ export type MaybeAsyncIterator<T, R = void, N = undefined> = Iterator<T, R, N> |
33
43
  /**
34
44
  * An union type that represents a generator object that can be either synchronous or asynchronous.
35
45
  *
46
+ * ---
47
+ *
48
+ * @example
36
49
  * ```ts
37
50
  * const generator: MaybeAsyncGenerator<number> = [async] function*() { ... };
38
51
  * for await (const value of generator)
@@ -46,6 +59,9 @@ export type MaybeAsyncGenerator<T, R = void, N = undefined> = Generator<T, R, N>
46
59
  * An utility type that represents a function that returns a generator object.
47
60
  * It differs from the native `GeneratorFunction` type by allowing to specify the types of the returned generator.
48
61
  *
62
+ * ---
63
+ *
64
+ * @example
49
65
  * ```ts
50
66
  * const generatorFn: GeneratorFunction<number> = function*() { ... };
51
67
  * const generator: Generator<number> = generatorFn();
@@ -56,6 +72,8 @@ export type MaybeAsyncGenerator<T, R = void, N = undefined> = Generator<T, R, N>
56
72
  * }
57
73
  * ```
58
74
  *
75
+ * ---
76
+ *
59
77
  * @template T The type of the elements generated by the generator.
60
78
  * @template R The type of the return value of the generator. Default is `void`.
61
79
  * @template N The type of the `next` method argument. Default is `undefined`.
@@ -66,6 +84,9 @@ export type GeneratorFunction<T, R = void, N = undefined> = () => Generator<T, R
66
84
  * An utility type that represents a function that returns an asynchronous generator object.
67
85
  * It differs from the native `AsyncGeneratorFunction` type by allowing to specify the types of the returned generator.
68
86
  *
87
+ * ---
88
+ *
89
+ * @example
69
90
  * ```ts
70
91
  * const asyncGeneratorFn: AsyncGeneratorFunction<number> = async function*() { ... };
71
92
  * const generator: AsyncGenerator<number> = asyncGeneratorFn();
@@ -75,6 +96,8 @@ export type GeneratorFunction<T, R = void, N = undefined> = () => Generator<T, R
75
96
  * }
76
97
  * ```
77
98
  *
99
+ * ---
100
+ *
78
101
  * @template T The type of the elements generated by the generator.
79
102
  * @template R The type of the return value of the generator. Default is `void`.
80
103
  * @template N The type of the `next` method argument. Default is `undefined`.
@@ -85,6 +108,9 @@ export type AsyncGeneratorFunction<T, R = void, N = undefined> = () => AsyncGene
85
108
  * An utility type that represents a function that returns a
86
109
  * generator object that can be either synchronous or asynchronous.
87
110
  *
111
+ * ---
112
+ *
113
+ * @example
88
114
  * ```ts
89
115
  * const generatorFn: MaybeAsyncGeneratorFunction<number> = [async] function*() { ... };
90
116
  * const generator: MaybeAsyncGenerator<number> = generatorFn();
@@ -94,6 +120,8 @@ export type AsyncGeneratorFunction<T, R = void, N = undefined> = () => AsyncGene
94
120
  * }
95
121
  * ```
96
122
  *
123
+ * ---
124
+ *
97
125
  * @template T The type of the elements generated by the generator.
98
126
  * @template R The type of the return value of the generator. Default is `void`.
99
127
  * @template N The type of the `next` method argument. Default is `undefined`.
@@ -105,13 +133,18 @@ export type MaybeAsyncGeneratorFunction<T, R = void, N = undefined> = () => Mayb
105
133
  * {@link https://en.wikipedia.org/wiki/Iteratee|iteratee} function.
106
134
  * It can be used to transform the elements of an iterable.
107
135
  *
136
+ * ---
137
+ *
138
+ * @example
108
139
  * ```ts
109
140
  * const iteratee: Iteratee<number, string> = (value: number) => `${value}`;
110
141
  * const values: string[] = [1, 2, 3, 4, 5].map(iteratee);
111
- *
142
+ *
112
143
  * console.log(values); // ["1", "2", "3", "4", "5"]
113
144
  * ```
114
145
  *
146
+ * ---
147
+ *
115
148
  * @template T The type of the elements in the iterable.
116
149
  * @template R The type of the return value of the iteratee. Default is `void`.
117
150
  */
@@ -121,6 +154,9 @@ export type Iteratee<T, R = void> = (value: T, index: number) => R;
121
154
  * An utility type that represents an asynchronous {@link https://en.wikipedia.org/wiki/Iteratee|iteratee} function.
122
155
  * It can be used to transform the elements of an iterable asynchronously.
123
156
  *
157
+ * ---
158
+ *
159
+ * @example
124
160
  * ```ts
125
161
  * const iteratee: AsyncIteratee<number, string> = async (value: number) => `${value}`;
126
162
  * const values: Promise<string>[] = [1, 2, 3, 4, 5].map(iteratee);
@@ -130,6 +166,8 @@ export type Iteratee<T, R = void> = (value: T, index: number) => R;
130
166
  * }
131
167
  * ```
132
168
  *
169
+ * ---
170
+ *
133
171
  * @template T The type of the elements in the iterable.
134
172
  * @template R The type of the return value of the iteratee. Default is `void`.
135
173
  */
@@ -140,6 +178,9 @@ export type AsyncIteratee<T, R = void> = (value: T, index: number) => Promise<R>
140
178
  * function that can be either synchronous or asynchronous.
141
179
  * It can be used to transform the elements of an iterable.
142
180
  *
181
+ * ---
182
+ *
183
+ * @example
143
184
  * ```ts
144
185
  * const iteratee: MaybeAsyncIteratee<number, string> = [async] (value: number) => `${value}`;
145
186
  * const values: Promise<string>[] = [1, 2, 3, 4, 5].map(iteratee);
@@ -149,6 +190,8 @@ export type AsyncIteratee<T, R = void> = (value: T, index: number) => Promise<R>
149
190
  * }
150
191
  * ```
151
192
  *
193
+ * ---
194
+ *
152
195
  * @template T The type of the elements in the iterable.
153
196
  * @template R The type of the return value of the iteratee. Default is `void`.
154
197
  */
@@ -161,6 +204,9 @@ export type MaybeAsyncIteratee<T, R = void> = (value: T, index: number) => Maybe
161
204
  * It can be used to ensure the type of the elements of an iterable
162
205
  * while allowing the type-system to infer them correctly.
163
206
  *
207
+ * ---
208
+ *
209
+ * @example
164
210
  * ```ts
165
211
  * const iteratee: TypeGuardPredicate<number | string, string> = (value): value is string => typeof value === "string";
166
212
  * const values: string[] = [1, "2", 3, "4", 5].filter(iteratee);
@@ -170,6 +216,8 @@ export type MaybeAsyncIteratee<T, R = void> = (value: T, index: number) => Maybe
170
216
  * }
171
217
  * ```
172
218
  *
219
+ * ---
220
+ *
173
221
  * @template T The type of the elements in the iterable.
174
222
  * @template R
175
223
  * The type of the elements that pass the type guard.
@@ -186,6 +234,9 @@ export type TypeGuardPredicate<T, R extends T> = (value: T, index: number) => va
186
234
  * An utility type that represents a reducer function.
187
235
  * It can be used to reduce the elements of an iterable into a single value.
188
236
  *
237
+ * ---
238
+ *
239
+ * @example
189
240
  * ```ts
190
241
  * const sum: Reducer<number, number> = (accumulator, value) => accumulator + value;
191
242
  * const total: number = [1, 2, 3, 4, 5].reduce(sum);
@@ -193,6 +244,8 @@ export type TypeGuardPredicate<T, R extends T> = (value: T, index: number) => va
193
244
  * console.log(total); // 15
194
245
  * ```
195
246
  *
247
+ * ---
248
+ *
196
249
  * @template T The type of the elements in the iterable.
197
250
  * @template A The type of the accumulator.
198
251
  */
@@ -202,6 +255,9 @@ export type Reducer<T, A> = (accumulator: A, value: T, index: number) => A;
202
255
  * An utility type that represents an asynchronous reducer function.
203
256
  * It can be used to reduce the elements of an iterable into a single value.
204
257
  *
258
+ * ---
259
+ *
260
+ * @example
205
261
  * ```ts
206
262
  * const sum: AsyncReducer<number, number> = async (accumulator, value) => accumulator + value;
207
263
  * const result = await new SmartAsyncIterator<number>([1, 2, 3, 4, 5]).reduce(sum);
@@ -209,6 +265,8 @@ export type Reducer<T, A> = (accumulator: A, value: T, index: number) => A;
209
265
  * console.log(result); // 15
210
266
  * ```
211
267
  *
268
+ * ---
269
+ *
212
270
  * @template T The type of the elements in the iterable.
213
271
  * @template A The type of the accumulator.
214
272
  */
@@ -218,6 +276,9 @@ export type AsyncReducer<T, A> = (accumulator: A, value: T, index: number) => Pr
218
276
  * An utility type that represents a reducer function that can be either synchronous or asynchronous.
219
277
  * It can be used to reduce the elements of an iterable into a single value.
220
278
  *
279
+ * ---
280
+ *
281
+ * @example
221
282
  * ```ts
222
283
  * const sum: MaybeAsyncReducer<number, number> = [async] (accumulator, value) => accumulator + value;
223
284
  * const result = await new SmartAsyncIterator<number>([1, 2, 3, 4, 5]).reduce(sum);
@@ -225,6 +286,8 @@ export type AsyncReducer<T, A> = (accumulator: A, value: T, index: number) => Pr
225
286
  * console.log(result); // 15
226
287
  * ```
227
288
  *
289
+ * ---
290
+ *
228
291
  * @template T The type of the elements in the iterable.
229
292
  * @template A The type of the accumulator.
230
293
  */
@@ -234,6 +297,9 @@ export type MaybeAsyncReducer<T, A> = (accumulator: A, value: T, index: number)
234
297
  * An union type that represents either an iterable or an iterator object.
235
298
  * More in general, it represents an object that can be looped over in one way or another.
236
299
  *
300
+ * ---
301
+ *
302
+ * @example
237
303
  * ```ts
238
304
  * const elements: IteratorLike<number> = { ... };
239
305
  * const iterator: SmartIterator<number> = new SmartIterator(elements);
@@ -243,6 +309,8 @@ export type MaybeAsyncReducer<T, A> = (accumulator: A, value: T, index: number)
243
309
  * }
244
310
  * ```
245
311
  *
312
+ * ---
313
+ *
246
314
  * @template T The type of the elements in the iterable.
247
315
  * @template R The type of the return value of the iterator. Default is `void`.
248
316
  * @template N The type of the `next` method argument. Default is `undefined`.
@@ -253,6 +321,9 @@ export type IteratorLike<T, R = void, N = undefined> = Iterable<T, R, N> | Itera
253
321
  * An union type that represents either an iterable or an iterator object that can be asynchronous.
254
322
  * More in general, it represents an object that can be looped over in one way or another.
255
323
  *
324
+ * ---
325
+ *
326
+ * @example
256
327
  * ```ts
257
328
  * const elements: AsyncIteratorLike<number> = { ... };
258
329
  * const iterator: SmartAsyncIterator<number> = new SmartAsyncIterator(elements);
@@ -262,6 +333,8 @@ export type IteratorLike<T, R = void, N = undefined> = Iterable<T, R, N> | Itera
262
333
  * }
263
334
  * ```
264
335
  *
336
+ * ---
337
+ *
265
338
  * @template T The type of the elements in the iterable.
266
339
  * @template R The type of the return value of the iterator. Default is `void`.
267
340
  * @template N The type of the `next` method argument. Default is `undefined`.
@@ -273,6 +346,9 @@ export type AsyncIteratorLike<T, R = void, N = undefined> = AsyncIterable<T, R,
273
346
  * object that can be either synchronous or asynchronous.
274
347
  * More in general, it represents an object that can be looped over in one way or another.
275
348
  *
349
+ * ---
350
+ *
351
+ * @example
276
352
  * ```ts
277
353
  * const elements: MaybeAsyncIteratorLike<number> = { ... };
278
354
  * const iterator: SmartAsyncIterator<number> = new SmartAsyncIterator(elements);
@@ -282,6 +358,8 @@ export type AsyncIteratorLike<T, R = void, N = undefined> = AsyncIterable<T, R,
282
358
  * }
283
359
  * ```
284
360
  *
361
+ * ---
362
+ *
285
363
  * @template T The type of the elements in the iterable.
286
364
  * @template R The type of the return value of the iterator. Default is `void`.
287
365
  * @template N The type of the `next` method argument. Default is `undefined`.
@@ -10,6 +10,9 @@ import type { JSONValue } from "./types.js";
10
10
  * It allows to handle either the volatile {@link sessionStorage} or the persistent
11
11
  * {@link localStorage} at the same time, depending on what's your required use case.
12
12
  *
13
+ * ---
14
+ *
15
+ * @example
13
16
  * ```ts
14
17
  * const jsonStorage = new JSONStorage();
15
18
  *
@@ -42,10 +45,15 @@ export default class JSONStorage
42
45
  * Initializes a new instance of the {@link JSONStorage} class.
43
46
  * It cannot be instantiated outside of a browser environment or an {@link EnvironmentException} is thrown.
44
47
  *
48
+ * ---
49
+ *
50
+ * @example
45
51
  * ```ts
46
52
  * const jsonStorage = new JSONStorage();
47
53
  * ```
48
54
  *
55
+ * ---
56
+ *
49
57
  * @param preferPersistence
50
58
  * Whether to prefer the {@link localStorage} over the {@link sessionStorage} when calling an ambivalent method.
51
59
  * If omitted, it defaults to `true` to prefer the persistent storage.
@@ -106,10 +114,15 @@ export default class JSONStorage
106
114
  /**
107
115
  * Retrieves the value with the specified key from the default storage.
108
116
  *
117
+ * ---
118
+ *
119
+ * @example
109
120
  * ```ts
110
121
  * const value: TValue = jsonStorage.get<TValue>("key");
111
122
  * ```
112
123
  *
124
+ * ---
125
+ *
113
126
  * @template T The type of the value to retrieve.
114
127
  *
115
128
  * @param key The key of the value to retrieve.
@@ -121,10 +134,15 @@ export default class JSONStorage
121
134
  /**
122
135
  * Retrieves the value with the specified key from the default storage.
123
136
  *
137
+ * ---
138
+ *
139
+ * @example
124
140
  * ```ts
125
141
  * const value: TValue = jsonStorage.get<TValue>("key", defaultValue);
126
142
  * ```
127
143
  *
144
+ * ---
145
+ *
128
146
  * @template T The type of the value to retrieve.
129
147
  *
130
148
  * @param key The key of the value to retrieve.
@@ -140,10 +158,15 @@ export default class JSONStorage
140
158
  /**
141
159
  * Retrieves the value with the specified key from the default storage.
142
160
  *
161
+ * ---
162
+ *
163
+ * @example
143
164
  * ```ts
144
165
  * const value: TValue = jsonStorage.get<TValue>("key", obj?.value);
145
166
  * ```
146
167
  *
168
+ * ---
169
+ *
147
170
  * @template T The type of the value to retrieve.
148
171
  *
149
172
  * @param key The key of the value to retrieve.
@@ -166,10 +189,15 @@ export default class JSONStorage
166
189
  /**
167
190
  * Retrieves the value with the specified key from the volatile {@link sessionStorage}.
168
191
  *
192
+ * ---
193
+ *
194
+ * @example
169
195
  * ```ts
170
196
  * const value: TValue = jsonStorage.recall<TValue>("key");
171
197
  * ```
172
198
  *
199
+ * ---
200
+ *
173
201
  * @template T The type of the value to retrieve.
174
202
  *
175
203
  * @param key The key of the value to retrieve.
@@ -181,10 +209,15 @@ export default class JSONStorage
181
209
  /**
182
210
  * Retrieves the value with the specified key from the volatile {@link sessionStorage}.
183
211
  *
212
+ * ---
213
+ *
214
+ * @example
184
215
  * ```ts
185
216
  * const value: TValue = jsonStorage.recall<TValue>("key", defaultValue);
186
217
  * ```
187
218
  *
219
+ * ---
220
+ *
188
221
  * @template T The type of the value to retrieve.
189
222
  *
190
223
  * @param key The key of the value to retrieve.
@@ -197,10 +230,15 @@ export default class JSONStorage
197
230
  /**
198
231
  * Retrieves the value with the specified key from the volatile {@link sessionStorage}.
199
232
  *
233
+ * ---
234
+ *
235
+ * @example
200
236
  * ```ts
201
237
  * const value: TValue = jsonStorage.recall<TValue>("key", obj?.value);
202
238
  * ```
203
239
  *
240
+ * ---
241
+ *
204
242
  * @template T The type of the value to retrieve.
205
243
  *
206
244
  * @param key The key of the value to retrieve.
@@ -218,10 +256,15 @@ export default class JSONStorage
218
256
  * Retrieves the value with the specified key looking first in the volatile
219
257
  * {@link sessionStorage} and then, if not found, in the persistent {@link localStorage}.
220
258
  *
259
+ * ---
260
+ *
261
+ * @example
221
262
  * ```ts
222
263
  * const value: TValue = jsonStorage.retrieve<TValue>("key");
223
264
  * ```
224
265
  *
266
+ * ---
267
+ *
225
268
  * @template T The type of the value to retrieve.
226
269
  *
227
270
  * @param key The key of the value to retrieve.
@@ -234,10 +277,15 @@ export default class JSONStorage
234
277
  * Retrieves the value with the specified key looking first in the volatile
235
278
  * {@link sessionStorage} and then, if not found, in the persistent {@link localStorage}.
236
279
  *
280
+ * ---
281
+ *
282
+ * @example
237
283
  * ```ts
238
284
  * const value: TValue = jsonStorage.retrieve<TValue>("key", defaultValue);
239
285
  * ```
240
286
  *
287
+ * ---
288
+ *
241
289
  * @template T The type of the value to retrieve.
242
290
  *
243
291
  * @param key The key of the value to retrieve.
@@ -251,10 +299,15 @@ export default class JSONStorage
251
299
  * Retrieves the value with the specified key looking first in the volatile
252
300
  * {@link sessionStorage} and then, if not found, in the persistent {@link localStorage}.
253
301
  *
302
+ * ---
303
+ *
304
+ * @example
254
305
  * ```ts
255
306
  * const value: TValue = jsonStorage.retrieve<TValue>("key", obj?.value);
256
307
  * ```
257
308
  *
309
+ * ---
310
+ *
258
311
  * @template T The type of the value to retrieve.
259
312
  *
260
313
  * @param key The key of the value to retrieve.
@@ -271,10 +324,15 @@ export default class JSONStorage
271
324
  /**
272
325
  * Retrieves the value with the specified key from the persistent {@link localStorage}.
273
326
  *
327
+ * ---
328
+ *
329
+ * @example
274
330
  * ```ts
275
331
  * const value: TValue = jsonStorage.read<TValue>("key");
276
332
  * ```
277
333
  *
334
+ * ---
335
+ *
278
336
  * @template T The type of the value to retrieve.
279
337
  *
280
338
  * @param key The key of the value to retrieve.
@@ -286,10 +344,15 @@ export default class JSONStorage
286
344
  /**
287
345
  * Retrieves the value with the specified key from the persistent {@link localStorage}.
288
346
  *
347
+ * ---
348
+ *
349
+ * @example
289
350
  * ```ts
290
351
  * const value: TValue = jsonStorage.read<TValue>("key", defaultValue);
291
352
  * ```
292
353
  *
354
+ * ---
355
+ *
293
356
  * @template T The type of the value to retrieve.
294
357
  *
295
358
  * @param key The key of the value to retrieve.
@@ -302,10 +365,15 @@ export default class JSONStorage
302
365
  /**
303
366
  * Retrieves the value with the specified key from the persistent {@link localStorage}.
304
367
  *
368
+ * ---
369
+ *
370
+ * @example
305
371
  * ```ts
306
372
  * const value: TValue = jsonStorage.read<TValue>("key", obj?.value);
307
373
  * ```
308
374
  *
375
+ * ---
376
+ *
309
377
  * @template T The type of the value to retrieve.
310
378
  *
311
379
  * @param key The key of the value to retrieve.
@@ -322,6 +390,9 @@ export default class JSONStorage
322
390
  /**
323
391
  * Checks whether the value with the specified key exists within the default storage.
324
392
  *
393
+ * ---
394
+ *
395
+ * @example
325
396
  * ```ts
326
397
  * if (jsonStorage.has("key"))
327
398
  * {
@@ -329,6 +400,8 @@ export default class JSONStorage
329
400
  * }
330
401
  * ```
331
402
  *
403
+ * ---
404
+ *
332
405
  * @param key The key of the value to check.
333
406
  * @param persistent
334
407
  * Whether to prefer the persistent {@link localStorage} over the volatile {@link sessionStorage}.
@@ -346,6 +419,9 @@ export default class JSONStorage
346
419
  /**
347
420
  * Checks whether the value with the specified key exists within the volatile {@link sessionStorage}.
348
421
  *
422
+ * ---
423
+ *
424
+ * @example
349
425
  * ```ts
350
426
  * if (jsonStorage.knows("key"))
351
427
  * {
@@ -353,6 +429,8 @@ export default class JSONStorage
353
429
  * }
354
430
  * ```
355
431
  *
432
+ * ---
433
+ *
356
434
  * @param key The key of the value to check.
357
435
  *
358
436
  * @returns `true` if the key exists, `false` otherwise.
@@ -366,6 +444,9 @@ export default class JSONStorage
366
444
  * Checks whether the value with the specified key exists looking first in the
367
445
  * volatile {@link sessionStorage} and then, if not found, in the persistent {@link localStorage}.
368
446
  *
447
+ * ---
448
+ *
449
+ * @example
369
450
  * ```ts
370
451
  * if (jsonStorage.find("key"))
371
452
  * {
@@ -373,6 +454,8 @@ export default class JSONStorage
373
454
  * }
374
455
  * ```
375
456
  *
457
+ * ---
458
+ *
376
459
  * @param key The key of the value to check.
377
460
  *
378
461
  * @returns `true` if the key exists, `false` otherwise.
@@ -385,6 +468,9 @@ export default class JSONStorage
385
468
  /**
386
469
  * Checks whether the value with the specified key exists within the persistent {@link localStorage}.
387
470
  *
471
+ * ---
472
+ *
473
+ * @example
388
474
  * ```ts
389
475
  * if (jsonStorage.exists("key"))
390
476
  * {
@@ -392,6 +478,8 @@ export default class JSONStorage
392
478
  * }
393
479
  * ```
394
480
  *
481
+ * ---
482
+ *
395
483
  * @param key The key of the value to check.
396
484
  *
397
485
  * @returns `true` if the key exists, `false` otherwise.
@@ -405,12 +493,17 @@ export default class JSONStorage
405
493
  * Sets the value with the specified key in the default storage.
406
494
  * If the value is `undefined` or omitted, the key is removed from the storage.
407
495
  *
496
+ * ---
497
+ *
498
+ * @example
408
499
  * ```ts
409
500
  * jsonStorage.set("key");
410
501
  * jsonStorage.set("key", value);
411
502
  * jsonStorage.set("key", obj?.value);
412
503
  * ```
413
504
  *
505
+ * ---
506
+ *
414
507
  * @template T The type of the value to set.
415
508
  *
416
509
  * @param key The key of the value to set.
@@ -430,12 +523,17 @@ export default class JSONStorage
430
523
  * Sets the value with the specified key in the volatile {@link sessionStorage}.
431
524
  * If the value is `undefined` or omitted, the key is removed from the storage.
432
525
  *
526
+ * ---
527
+ *
528
+ * @example
433
529
  * ```ts
434
530
  * jsonStorage.remember("key");
435
531
  * jsonStorage.remember("key", value);
436
532
  * jsonStorage.remember("key", obj?.value);
437
533
  * ```
438
534
  *
535
+ * ---
536
+ *
439
537
  * @template T The type of the value to set.
440
538
  *
441
539
  * @param key The key of the value to set.
@@ -450,12 +548,17 @@ export default class JSONStorage
450
548
  * Sets the value with the specified key in the persistent {@link localStorage}.
451
549
  * If the value is `undefined` or omitted, the key is removed from the storage.
452
550
  *
551
+ * ---
552
+ *
553
+ * @example
453
554
  * ```ts
454
555
  * jsonStorage.write("key");
455
556
  * jsonStorage.write("key", value);
456
557
  * jsonStorage.write("key", obj?.value);
457
558
  * ```
458
559
  *
560
+ * ---
561
+ *
459
562
  * @template T The type of the value to set.
460
563
  *
461
564
  * @param key The key of the value to set.
@@ -469,10 +572,15 @@ export default class JSONStorage
469
572
  /**
470
573
  * Removes the value with the specified key from the default storage.
471
574
  *
575
+ * ---
576
+ *
577
+ * @example
472
578
  * ```ts
473
579
  * jsonStorage.delete("key");
474
580
  * ```
475
581
  *
582
+ * ---
583
+ *
476
584
  * @param key The key of the value to remove.
477
585
  * @param persistent
478
586
  * Whether to prefer the persistent {@link localStorage} over the volatile {@link sessionStorage}.
@@ -488,10 +596,15 @@ export default class JSONStorage
488
596
  /**
489
597
  * Removes the value with the specified key from the volatile {@link sessionStorage}.
490
598
  *
599
+ * ---
600
+ *
601
+ * @example
491
602
  * ```ts
492
603
  * jsonStorage.forget("key");
493
604
  * ```
494
605
  *
606
+ * ---
607
+ *
495
608
  * @param key The key of the value to remove.
496
609
  */
497
610
  public forget(key: string): void
@@ -502,10 +615,15 @@ export default class JSONStorage
502
615
  /**
503
616
  * Removes the value with the specified key from the persistent {@link localStorage}.
504
617
  *
618
+ * ---
619
+ *
620
+ * @example
505
621
  * ```ts
506
622
  * jsonStorage.erase("key");
507
623
  * ```
508
624
  *
625
+ * ---
626
+ *
509
627
  * @param key The key of the value to remove.
510
628
  */
511
629
  public erase(key: string): void
@@ -517,10 +635,15 @@ export default class JSONStorage
517
635
  * Removes the value with the specified key from both the
518
636
  * volatile {@link sessionStorage} and the persistent {@link localStorage}.
519
637
  *
638
+ * ---
639
+ *
640
+ * @example
520
641
  * ```ts
521
642
  * jsonStorage.clear("key");
522
643
  * ```
523
644
  *
645
+ * ---
646
+ *
524
647
  * @param key The key of the value to remove.
525
648
  */
526
649
  public clear(key: string): void
@@ -6,7 +6,7 @@ export type JSONArray = JSONValue[];
6
6
  /**
7
7
  * A type that represents a JSON object.
8
8
  */
9
- export interface JSONObject { [key: string]: JSONValue }
9
+ export type JSONObject<T extends object = object> = { [K in keyof T]: JSONValue };
10
10
 
11
11
  /**
12
12
  * A type that represents all the possible values of a JSON value.