@byloth/core 2.0.0-rc.8 → 2.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 (47) hide show
  1. package/dist/core.js +3372 -609
  2. package/dist/core.js.map +1 -1
  3. package/dist/core.umd.cjs +2 -2
  4. package/dist/core.umd.cjs.map +1 -1
  5. package/package.json +13 -10
  6. package/src/core/types.ts +41 -0
  7. package/src/helpers.ts +11 -2
  8. package/src/index.ts +12 -9
  9. package/src/models/aggregators/aggregated-async-iterator.ts +765 -21
  10. package/src/models/aggregators/aggregated-iterator.ts +698 -22
  11. package/src/models/aggregators/reduced-iterator.ts +699 -10
  12. package/src/models/aggregators/types.ts +153 -10
  13. package/src/models/callbacks/callable-object.ts +42 -6
  14. package/src/models/callbacks/index.ts +2 -2
  15. package/src/models/callbacks/publisher.ts +140 -5
  16. package/src/models/callbacks/switchable-callback.ts +143 -5
  17. package/src/models/callbacks/types.ts +16 -0
  18. package/src/models/exceptions/core.ts +112 -3
  19. package/src/models/exceptions/index.ts +340 -13
  20. package/src/models/index.ts +4 -8
  21. package/src/models/iterators/smart-async-iterator.ts +687 -22
  22. package/src/models/iterators/smart-iterator.ts +631 -21
  23. package/src/models/iterators/types.ts +268 -9
  24. package/src/models/json/json-storage.ts +388 -110
  25. package/src/models/json/types.ts +10 -1
  26. package/src/models/promises/deferred-promise.ts +75 -5
  27. package/src/models/promises/index.ts +1 -3
  28. package/src/models/promises/smart-promise.ts +232 -4
  29. package/src/models/promises/timed-promise.ts +38 -1
  30. package/src/models/promises/types.ts +84 -2
  31. package/src/models/timers/clock.ts +91 -19
  32. package/src/models/timers/countdown.ts +152 -22
  33. package/src/models/timers/game-loop.ts +243 -0
  34. package/src/models/timers/index.ts +2 -1
  35. package/src/models/types.ts +6 -5
  36. package/src/utils/async.ts +43 -0
  37. package/src/utils/curve.ts +75 -0
  38. package/src/utils/date.ts +204 -10
  39. package/src/utils/dom.ts +16 -2
  40. package/src/utils/index.ts +3 -2
  41. package/src/utils/iterator.ts +200 -17
  42. package/src/utils/math.ts +55 -3
  43. package/src/utils/random.ts +109 -2
  44. package/src/utils/string.ts +11 -0
  45. package/src/models/game-loop.ts +0 -83
  46. package/src/models/promises/long-running-task.ts +0 -294
  47. package/src/models/promises/thenable.ts +0 -97
@@ -1,26 +1,57 @@
1
-
2
1
  import { isBrowser } from "../../helpers.js";
3
2
  import { EnvironmentException } from "../exceptions/index.js";
4
3
 
5
4
  import type { JSONValue } from "./types.js";
6
5
 
7
6
  /**
8
- * A wrapper around the `Storage` API to store and retrieve JSON values.
7
+ * A wrapper around the `Storage` API to better store and easily retrieve
8
+ * typed JSON values using the classical key-value pair storage system.
9
+ *
10
+ * It allows to handle either the volatile {@link sessionStorage} or the persistent
11
+ * {@link localStorage} at the same time, depending on what's your required use case.
9
12
  *
10
- * It allows to handle either the `sessionStorage` or the `localStorage`
11
- * storage at the same time, depending on the required use case.
13
+ * ```ts
14
+ * const jsonStorage = new JSONStorage();
15
+ *
16
+ * jsonStorage.write("user:cookieAck", { value: true, version: "2023-02-15" });
17
+ * // ... between sessions ...
18
+ * const cookieAck = jsonStorage.read<{ value: boolean; version: string; }>("user:cookieAck");
19
+ * ```
12
20
  */
13
21
  export default class JSONStorage
14
22
  {
23
+ /**
24
+ * Whether to prefer the {@link localStorage} over the {@link sessionStorage} when calling an ambivalent method.
25
+ *
26
+ * If `true`, the persistent storage is preferred. If `false`, the volatile storage is preferred.
27
+ * Default is `true`.
28
+ */
15
29
  protected _preferPersistence: boolean;
16
30
 
31
+ /**
32
+ * A reference to the volatile {@link sessionStorage} storage.
33
+ */
17
34
  protected _volatile: Storage;
35
+
36
+ /**
37
+ * A reference to the persistent {@link localStorage} storage.
38
+ */
18
39
  protected _persistent: Storage;
19
40
 
41
+ /**
42
+ * Initializes a new instance of the {@link JSONStorage} class.
43
+ * It cannot be instantiated outside of a browser environment or an {@link EnvironmentException} is thrown.
44
+ *
45
+ * ```ts
46
+ * const jsonStorage = new JSONStorage();
47
+ * ```
48
+ *
49
+ * @param preferPersistence
50
+ * Whether to prefer the {@link localStorage} over the {@link sessionStorage} when calling an ambivalent method.
51
+ * If omitted, it defaults to `true` to prefer the persistent storage.
52
+ */
20
53
  public constructor(preferPersistence = true)
21
54
  {
22
- this._preferPersistence = preferPersistence;
23
-
24
55
  if (!(isBrowser))
25
56
  {
26
57
  throw new EnvironmentException(
@@ -28,227 +59,474 @@ export default class JSONStorage
28
59
  );
29
60
  }
30
61
 
62
+ this._preferPersistence = preferPersistence;
63
+
31
64
  this._volatile = window.sessionStorage;
32
65
  this._persistent = window.localStorage;
33
66
  }
34
67
 
35
- protected _get<T extends JSONValue>(storage: Storage, propertyName: string): T | undefined;
36
- protected _get<T extends JSONValue>(storage: Storage, propertyName: string, defaultValue: T): T;
37
- protected _get<T extends JSONValue>(storage: Storage, propertyName: string, defaultValue?: T): T | undefined;
38
- protected _get<T extends JSONValue>(storage: Storage, propertyName: string, defaultValue?: T): T | undefined
68
+ protected _get<T extends JSONValue>(storage: Storage, key: string): T | undefined;
69
+ protected _get<T extends JSONValue>(storage: Storage, key: string, defaultValue: T): T;
70
+ protected _get<T extends JSONValue>(storage: Storage, key: string, defaultValue?: T): T | undefined;
71
+ protected _get<T extends JSONValue>(storage: Storage, key: string, defaultValue?: T): T | undefined
39
72
  {
40
- const propertyValue = storage.getItem(propertyName);
41
- if (propertyValue)
73
+ const value = storage.getItem(key);
74
+ if (value)
42
75
  {
43
76
  try
44
77
  {
45
- return JSON.parse(propertyValue);
78
+ return JSON.parse(value);
46
79
  }
47
80
  catch
48
81
  {
49
82
  // eslint-disable-next-line no-console
50
83
  console.warn(
51
- `The "${propertyValue}" value for "${propertyName}"` +
84
+ `The "${value}" value for "${key}"` +
52
85
  " property cannot be parsed. Clearing the storage...");
53
86
 
54
- storage.removeItem(propertyName);
87
+ storage.removeItem(key);
55
88
  }
56
89
  }
57
90
 
58
91
  return defaultValue;
59
92
  }
60
- protected _set<T extends JSONValue>(storage: Storage, propertyName: string, newValue?: T): void
93
+ protected _set<T extends JSONValue>(storage: Storage, key: string, newValue?: T): void
61
94
  {
62
95
  const encodedValue = JSON.stringify(newValue);
63
96
  if (encodedValue)
64
97
  {
65
- storage.setItem(propertyName, encodedValue);
98
+ storage.setItem(key, encodedValue);
66
99
  }
67
100
  else
68
101
  {
69
- storage.removeItem(propertyName);
102
+ storage.removeItem(key);
70
103
  }
71
104
  }
72
105
 
73
106
  /**
74
- * Retrieves the value with the specified name from the corresponding storage.
107
+ * Retrieves the value with the specified key from the default storage.
108
+ *
109
+ * ```ts
110
+ * const value: TValue = jsonStorage.get<TValue>("key");
111
+ * ```
112
+ *
113
+ * @template T The type of the value to retrieve.
114
+ *
115
+ * @param key The key of the value to retrieve.
116
+ *
117
+ * @returns The value with the specified key or `undefined` if the key doesn't exist.
118
+ */
119
+ public get<T extends JSONValue>(key: string): T | undefined;
120
+
121
+ /**
122
+ * Retrieves the value with the specified key from the default storage.
123
+ *
124
+ * ```ts
125
+ * const value: TValue = jsonStorage.get<TValue>("key", defaultValue);
126
+ * ```
127
+ *
128
+ * @template T The type of the value to retrieve.
129
+ *
130
+ * @param key The key of the value to retrieve.
131
+ * @param defaultValue The default value to return if the key doesn't exist.
132
+ * @param persistent
133
+ * Whether to prefer the persistent {@link localStorage} over the volatile {@link sessionStorage}.
134
+ * If omitted, it defaults to the `preferPersistence` value set in the constructor.
135
+ *
136
+ * @returns The value with the specified key or the provided default value if the key doesn't exist.
137
+ */
138
+ public get<T extends JSONValue>(key: string, defaultValue: T, persistent?: boolean): T;
139
+
140
+ /**
141
+ * Retrieves the value with the specified key from the default storage.
75
142
  *
76
- * @param propertyName The name of the property to retrieve.
77
- * @param defaultValue The default value to return if the property does not exist.
78
- * @param persistent Whether to use the persistent `localStorage` or the volatile `sessionStorage`.
143
+ * ```ts
144
+ * const value: TValue = jsonStorage.get<TValue>("key", obj?.value);
145
+ * ```
79
146
  *
80
- * @returns The value of the property or the default value if the property does not exist.
147
+ * @template T The type of the value to retrieve.
148
+ *
149
+ * @param key The key of the value to retrieve.
150
+ * @param defaultValue The default value to return (which may be `undefined`) if the key doesn't exist.
151
+ * @param persistent
152
+ * Whether to prefer the persistent {@link localStorage} over the volatile {@link sessionStorage}.
153
+ * If omitted, it defaults to the `preferPersistence` value set in the constructor.
154
+ *
155
+ * @returns The value with the specified key or the default value if the key doesn't exist.
81
156
  */
82
- public get<T extends JSONValue>(propertyName: string, defaultValue: undefined, persistent?: boolean): T | undefined;
83
- public get<T extends JSONValue>(propertyName: string, defaultValue: T, persistent?: boolean): T ;
84
- public get<T extends JSONValue>(propertyName: string, defaultValue?: T, persistent?: boolean): T | undefined;
85
- public get<T extends JSONValue>(propertyName: string, defaultValue?: T, persistent = this._preferPersistence)
157
+ public get<T extends JSONValue>(key: string, defaultValue?: T, persistent?: boolean): T | undefined;
158
+ public get<T extends JSONValue>(key: string, defaultValue?: T, persistent = this._preferPersistence)
86
159
  : T | undefined
87
160
  {
88
161
  const storage = persistent ? this._persistent : this._volatile;
89
162
 
90
- return this._get<T>(storage, propertyName, defaultValue);
163
+ return this._get<T>(storage, key, defaultValue);
91
164
  }
165
+
92
166
  /**
93
- * Retrieves the value with the specified name from the volatile `sessionStorage`.
167
+ * Retrieves the value with the specified key from the volatile {@link sessionStorage}.
168
+ *
169
+ * ```ts
170
+ * const value: TValue = jsonStorage.recall<TValue>("key");
171
+ * ```
94
172
  *
95
- * @param propertyName The name of the property to retrieve.
96
- * @param defaultValue The default value to return if the property does not exist.
173
+ * @template T The type of the value to retrieve.
97
174
  *
98
- * @returns The value of the property or the default value if the property does not exist.
175
+ * @param key The key of the value to retrieve.
176
+ *
177
+ * @returns The value with the specified key or `undefined` if the key doesn't exist.
99
178
  */
100
- public recall<T extends JSONValue>(propertyName: string): T | undefined;
101
- public recall<T extends JSONValue>(propertyName: string, defaultValue: T): T;
102
- public recall<T extends JSONValue>(propertyName: string, defaultValue?: T): T | undefined;
103
- public recall<T extends JSONValue>(propertyName: string, defaultValue?: T): T | undefined
179
+ public recall<T extends JSONValue>(key: string): T | undefined;
180
+
181
+ /**
182
+ * Retrieves the value with the specified key from the volatile {@link sessionStorage}.
183
+ *
184
+ * ```ts
185
+ * const value: TValue = jsonStorage.recall<TValue>("key", defaultValue);
186
+ * ```
187
+ *
188
+ * @template T The type of the value to retrieve.
189
+ *
190
+ * @param key The key of the value to retrieve.
191
+ * @param defaultValue The default value to return if the key doesn't exist.
192
+ *
193
+ * @returns The value with the specified key or the default value if the key doesn't exist.
194
+ */
195
+ public recall<T extends JSONValue>(key: string, defaultValue: T): T;
196
+
197
+ /**
198
+ * Retrieves the value with the specified key from the volatile {@link sessionStorage}.
199
+ *
200
+ * ```ts
201
+ * const value: TValue = jsonStorage.recall<TValue>("key", obj?.value);
202
+ * ```
203
+ *
204
+ * @template T The type of the value to retrieve.
205
+ *
206
+ * @param key The key of the value to retrieve.
207
+ * @param defaultValue The default value to return (which may be `undefined`) if the key doesn't exist.
208
+ *
209
+ * @returns The value with the specified key or the default value if the key doesn't exist.
210
+ */
211
+ public recall<T extends JSONValue>(key: string, defaultValue?: T): T | undefined;
212
+ public recall<T extends JSONValue>(key: string, defaultValue?: T): T | undefined
104
213
  {
105
- return this._get<T>(this._volatile, propertyName, defaultValue);
214
+ return this._get<T>(this._volatile, key, defaultValue);
106
215
  }
216
+
107
217
  /**
108
- * Retrieves the value with the specified name looking first in the
109
- * volatile `sessionStorage` and then in the persistent `localStorage`.
218
+ * Retrieves the value with the specified key looking first in the volatile
219
+ * {@link sessionStorage} and then, if not found, in the persistent {@link localStorage}.
220
+ *
221
+ * ```ts
222
+ * const value: TValue = jsonStorage.retrieve<TValue>("key");
223
+ * ```
224
+ *
225
+ * @template T The type of the value to retrieve.
110
226
  *
111
- * @param propertyName The name of the property to retrieve.
112
- * @param defaultValue The default value to return if the property does not exist.
227
+ * @param key The key of the value to retrieve.
113
228
  *
114
- * @returns The value of the property or the default value if the property does not exist.
229
+ * @returns The value with the specified key or `undefined` if the key doesn't exist.
115
230
  */
116
- public retrieve<T extends JSONValue>(propertyName: string): T | undefined;
117
- public retrieve<T extends JSONValue>(propertyName: string, defaultValue: T): T;
118
- public retrieve<T extends JSONValue>(propertyName: string, defaultValue?: T): T | undefined;
119
- public retrieve<T extends JSONValue>(propertyName: string, defaultValue?: T): T | undefined
231
+ public retrieve<T extends JSONValue>(key: string): T | undefined;
232
+
233
+ /**
234
+ * Retrieves the value with the specified key looking first in the volatile
235
+ * {@link sessionStorage} and then, if not found, in the persistent {@link localStorage}.
236
+ *
237
+ * ```ts
238
+ * const value: TValue = jsonStorage.retrieve<TValue>("key", defaultValue);
239
+ * ```
240
+ *
241
+ * @template T The type of the value to retrieve.
242
+ *
243
+ * @param key The key of the value to retrieve.
244
+ * @param defaultValue The default value to return if the key doesn't exist.
245
+ *
246
+ * @returns The value with the specified key or the default value if the key doesn't exist.
247
+ */
248
+ public retrieve<T extends JSONValue>(key: string, defaultValue: T): T;
249
+
250
+ /**
251
+ * Retrieves the value with the specified key looking first in the volatile
252
+ * {@link sessionStorage} and then, if not found, in the persistent {@link localStorage}.
253
+ *
254
+ * ```ts
255
+ * const value: TValue = jsonStorage.retrieve<TValue>("key", obj?.value);
256
+ * ```
257
+ *
258
+ * @template T The type of the value to retrieve.
259
+ *
260
+ * @param key The key of the value to retrieve.
261
+ * @param defaultValue The default value to return (which may be `undefined`) if the key doesn't exist.
262
+ *
263
+ * @returns The value with the specified key or the default value if the key doesn't exist.
264
+ */
265
+ public retrieve<T extends JSONValue>(key: string, defaultValue?: T): T | undefined;
266
+ public retrieve<T extends JSONValue>(key: string, defaultValue?: T): T | undefined
120
267
  {
121
- return this.recall<T>(propertyName) ?? this.read<T>(propertyName, defaultValue);
268
+ return this.recall<T>(key) ?? this.read<T>(key, defaultValue);
122
269
  }
270
+
123
271
  /**
124
- * Retrieves the value with the specified name from the persistent `localStorage`.
272
+ * Retrieves the value with the specified key from the persistent {@link localStorage}.
125
273
  *
126
- * @param propertyName The name of the property to retrieve.
127
- * @param defaultValue The default value to return if the property does not exist.
274
+ * ```ts
275
+ * const value: TValue = jsonStorage.read<TValue>("key");
276
+ * ```
128
277
  *
129
- * @returns The value of the property or the default value if the property does not exist.
278
+ * @template T The type of the value to retrieve.
279
+ *
280
+ * @param key The key of the value to retrieve.
281
+ *
282
+ * @returns The value with the specified key or `undefined` if the key doesn't exist.
130
283
  */
131
- public read<T extends JSONValue>(propertyName: string): T | undefined;
132
- public read<T extends JSONValue>(propertyName: string, defaultValue: T): T;
133
- public read<T extends JSONValue>(propertyName: string, defaultValue?: T): T | undefined;
134
- public read<T extends JSONValue>(propertyName: string, defaultValue?: T): T | undefined
284
+ public read<T extends JSONValue>(key: string): T | undefined;
285
+
286
+ /**
287
+ * Retrieves the value with the specified key from the persistent {@link localStorage}.
288
+ *
289
+ * ```ts
290
+ * const value: TValue = jsonStorage.read<TValue>("key", defaultValue);
291
+ * ```
292
+ *
293
+ * @template T The type of the value to retrieve.
294
+ *
295
+ * @param key The key of the value to retrieve.
296
+ * @param defaultValue The default value to return if the key doesn't exist.
297
+ *
298
+ * @returns The value with the specified key or the default value if the key doesn't exist.
299
+ */
300
+ public read<T extends JSONValue>(key: string, defaultValue: T): T;
301
+
302
+ /**
303
+ * Retrieves the value with the specified key from the persistent {@link localStorage}.
304
+ *
305
+ * ```ts
306
+ * const value: TValue = jsonStorage.read<TValue>("key", obj?.value);
307
+ * ```
308
+ *
309
+ * @template T The type of the value to retrieve.
310
+ *
311
+ * @param key The key of the value to retrieve.
312
+ * @param defaultValue The default value to return (which may be `undefined`) if the key doesn't exist.
313
+ *
314
+ * @returns The value with the specified key or the default value if the key doesn't exist.
315
+ */
316
+ public read<T extends JSONValue>(key: string, defaultValue?: T): T | undefined;
317
+ public read<T extends JSONValue>(key: string, defaultValue?: T): T | undefined
135
318
  {
136
- return this._get<T>(this._persistent, propertyName, defaultValue);
319
+ return this._get<T>(this._persistent, key, defaultValue);
137
320
  }
138
321
 
139
322
  /**
140
- * Checks whether the property with the specified name exists in the corresponding storage.
323
+ * Checks whether the value with the specified key exists within the default storage.
324
+ *
325
+ * ```ts
326
+ * if (jsonStorage.has("key"))
327
+ * {
328
+ * // The key exists. Do something...
329
+ * }
330
+ * ```
141
331
  *
142
- * @param propertyName The name of the property to check.
143
- * @param persistent Whether to use the persistent `localStorage` or the volatile `sessionStorage`.
332
+ * @param key The key of the value to check.
333
+ * @param persistent
334
+ * Whether to prefer the persistent {@link localStorage} over the volatile {@link sessionStorage}.
335
+ * If omitted, it defaults to the `preferPersistence` value set in the constructor.
144
336
  *
145
- * @returns `true` if the property exists, `false` otherwise.
337
+ * @returns `true` if the key exists, `false` otherwise.
146
338
  */
147
- public has(propertyName: string, persistent?: boolean): boolean
339
+ public has(key: string, persistent?: boolean): boolean
148
340
  {
149
341
  const storage = persistent ? this._persistent : this._volatile;
150
342
 
151
- return storage.getItem(propertyName) !== null;
343
+ return storage.getItem(key) !== null;
152
344
  }
345
+
153
346
  /**
154
- * Checks whether the property with the specified name exists in the volatile `sessionStorage`.
347
+ * Checks whether the value with the specified key exists within the volatile {@link sessionStorage}.
348
+ *
349
+ * ```ts
350
+ * if (jsonStorage.knows("key"))
351
+ * {
352
+ * // The key exists. Do something...
353
+ * }
354
+ * ```
155
355
  *
156
- * @param propertyName The name of the property to check.
356
+ * @param key The key of the value to check.
157
357
  *
158
- * @returns `true` if the property exists, `false` otherwise.
358
+ * @returns `true` if the key exists, `false` otherwise.
159
359
  */
160
- public knows(propertyName: string): boolean
360
+ public knows(key: string): boolean
161
361
  {
162
- return this._volatile.getItem(propertyName) !== null;
362
+ return this._volatile.getItem(key) !== null;
163
363
  }
364
+
164
365
  /**
165
- * Checks whether the property with the specified name exists looking first in the
166
- * volatile `sessionStorage` and then in the persistent `localStorage`.
366
+ * Checks whether the value with the specified key exists looking first in the
367
+ * volatile {@link sessionStorage} and then, if not found, in the persistent {@link localStorage}.
368
+ *
369
+ * ```ts
370
+ * if (jsonStorage.find("key"))
371
+ * {
372
+ * // The key exists. Do something...
373
+ * }
374
+ * ```
167
375
  *
168
- * @param propertyName The name of the property to check.
376
+ * @param key The key of the value to check.
169
377
  *
170
- * @returns `true` if the property exists, `false` otherwise.
378
+ * @returns `true` if the key exists, `false` otherwise.
171
379
  */
172
- public find(propertyName: string): boolean
380
+ public find(key: string): boolean
173
381
  {
174
- return this.knows(propertyName) ?? this.exists(propertyName);
382
+ return this.knows(key) ?? this.exists(key);
175
383
  }
384
+
176
385
  /**
177
- * Checks whether the property with the specified name exists in the persistent `localStorage`.
386
+ * Checks whether the value with the specified key exists within the persistent {@link localStorage}.
387
+ *
388
+ * ```ts
389
+ * if (jsonStorage.exists("key"))
390
+ * {
391
+ * // The key exists. Do something...
392
+ * }
393
+ * ```
178
394
  *
179
- * @param propertyName The name of the property to check.
395
+ * @param key The key of the value to check.
180
396
  *
181
- * @returns `true` if the property exists, `false` otherwise.
397
+ * @returns `true` if the key exists, `false` otherwise.
182
398
  */
183
- public exists(propertyName: string): boolean
399
+ public exists(key: string): boolean
184
400
  {
185
- return this._persistent.getItem(propertyName) !== null;
401
+ return this._persistent.getItem(key) !== null;
186
402
  }
187
403
 
188
404
  /**
189
- * Sets the value with the specified name in the corresponding storage.
190
- * If the value is `undefined`, the property is removed from the storage.
405
+ * Sets the value with the specified key in the default storage.
406
+ * If the value is `undefined` or omitted, the key is removed from the storage.
407
+ *
408
+ * ```ts
409
+ * jsonStorage.set("key");
410
+ * jsonStorage.set("key", value);
411
+ * jsonStorage.set("key", obj?.value);
412
+ * ```
191
413
  *
192
- * @param propertyName The name of the property to set.
193
- * @param newValue The new value to set.
194
- * @param persistent Whether to use the persistent `localStorage` or the volatile `sessionStorage`.
414
+ * @template T The type of the value to set.
415
+ *
416
+ * @param key The key of the value to set.
417
+ * @param newValue The new value to set. If it's `undefined` or omitted, the key is removed instead.
418
+ * @param persistent
419
+ * Whether to prefer the persistent {@link localStorage} over the volatile {@link sessionStorage}.
420
+ * If omitted, it defaults to the `preferPersistence` value set in the constructor.
195
421
  */
196
- public set<T extends JSONValue>(propertyName: string, newValue?: T, persistent = this._preferPersistence): void
422
+ public set<T extends JSONValue>(key: string, newValue?: T, persistent = this._preferPersistence): void
197
423
  {
198
424
  const storage = persistent ? this._persistent : this._volatile;
199
425
 
200
- this._set<T>(storage, propertyName, newValue);
426
+ this._set<T>(storage, key, newValue);
427
+ }
428
+
429
+ /**
430
+ * Sets the value with the specified key in the volatile {@link sessionStorage}.
431
+ * If the value is `undefined` or omitted, the key is removed from the storage.
432
+ *
433
+ * ```ts
434
+ * jsonStorage.remember("key");
435
+ * jsonStorage.remember("key", value);
436
+ * jsonStorage.remember("key", obj?.value);
437
+ * ```
438
+ *
439
+ * @template T The type of the value to set.
440
+ *
441
+ * @param key The key of the value to set.
442
+ * @param newValue The new value to set. If it's `undefined` or omitted, the key is removed instead.
443
+ */
444
+ public remember<T extends JSONValue>(key: string, newValue?: T): void
445
+ {
446
+ this._set<T>(this._volatile, key, newValue);
201
447
  }
448
+
202
449
  /**
203
- * Sets the value with the specified name in the volatile `sessionStorage`.
204
- * If the value is `undefined`, the property is removed from the storage.
450
+ * Sets the value with the specified key in the persistent {@link localStorage}.
451
+ * If the value is `undefined` or omitted, the key is removed from the storage.
452
+ *
453
+ * ```ts
454
+ * jsonStorage.write("key");
455
+ * jsonStorage.write("key", value);
456
+ * jsonStorage.write("key", obj?.value);
457
+ * ```
205
458
  *
206
- * @param propertyName The name of the property to set.
207
- * @param newValue The new value to set.
459
+ * @template T The type of the value to set.
460
+ *
461
+ * @param key The key of the value to set.
462
+ * @param newValue The new value to set. If it's `undefined` or omitted, the key is removed instead.
208
463
  */
209
- public remember<T extends JSONValue>(propertyName: string, newValue?: T): void
464
+ public write<T extends JSONValue>(key: string, newValue?: T): void
210
465
  {
211
- this._set<T>(this._volatile, propertyName, newValue);
466
+ this._set<T>(this._persistent, key, newValue);
212
467
  }
468
+
213
469
  /**
214
- * Sets the value with the specified name in the persistent `localStorage`.
215
- * If the value is `undefined`, the property is removed from the storage.
470
+ * Removes the value with the specified key from the default storage.
471
+ *
472
+ * ```ts
473
+ * jsonStorage.delete("key");
474
+ * ```
216
475
  *
217
- * @param propertyName The name of the property to set.
218
- * @param newValue The new value to set.
476
+ * @param key The key of the value to remove.
477
+ * @param persistent
478
+ * Whether to prefer the persistent {@link localStorage} over the volatile {@link sessionStorage}.
479
+ * If omitted, it defaults to the `preferPersistence` value set in the constructor.
219
480
  */
220
- public write<T extends JSONValue>(propertyName: string, newValue?: T): void
481
+ public delete(key: string, persistent?: boolean): void
221
482
  {
222
- this._set<T>(this._persistent, propertyName, newValue);
483
+ const storage = persistent ? this._persistent : this._volatile;
484
+
485
+ storage.removeItem(key);
223
486
  }
224
487
 
225
488
  /**
226
- * Removes the value with the specified name from the volatile `sessionStorage`.
489
+ * Removes the value with the specified key from the volatile {@link sessionStorage}.
227
490
  *
228
- * @param propertyName The name of the property to remove.
491
+ * ```ts
492
+ * jsonStorage.forget("key");
493
+ * ```
494
+ *
495
+ * @param key The key of the value to remove.
229
496
  */
230
- public forget(propertyName: string): void
497
+ public forget(key: string): void
231
498
  {
232
- this._volatile.removeItem(propertyName);
499
+ this._volatile.removeItem(key);
233
500
  }
501
+
234
502
  /**
235
- * Removes the value with the specified name from the persistent `localStorage`.
503
+ * Removes the value with the specified key from the persistent {@link localStorage}.
236
504
  *
237
- * @param propertyName The name of the property to remove.
505
+ * ```ts
506
+ * jsonStorage.erase("key");
507
+ * ```
508
+ *
509
+ * @param key The key of the value to remove.
238
510
  */
239
- public erase(propertyName: string): void
511
+ public erase(key: string): void
240
512
  {
241
- this._persistent.removeItem(propertyName);
513
+ this._persistent.removeItem(key);
242
514
  }
515
+
243
516
  /**
244
- * Removes the value with the specified name from all the storages.
517
+ * Removes the value with the specified key from both the
518
+ * volatile {@link sessionStorage} and the persistent {@link localStorage}.
519
+ *
520
+ * ```ts
521
+ * jsonStorage.clear("key");
522
+ * ```
245
523
  *
246
- * @param propertyName The name of the property to remove.
524
+ * @param key The key of the value to remove.
247
525
  */
248
- public clear(propertyName: string): void
526
+ public clear(key: string): void
249
527
  {
250
- this._volatile.removeItem(propertyName);
251
- this._persistent.removeItem(propertyName);
528
+ this._volatile.removeItem(key);
529
+ this._persistent.removeItem(key);
252
530
  }
253
531
 
254
532
  public readonly [Symbol.toStringTag]: string = "JSONStorage";
@@ -1,5 +1,14 @@
1
+ /**
2
+ * A type that represents a JSON array.
3
+ */
1
4
  export type JSONArray = JSONValue[];
2
5
 
3
- // eslint-disable-next-line @typescript-eslint/consistent-indexed-object-style
6
+ /**
7
+ * A type that represents a JSON object.
8
+ */
4
9
  export interface JSONObject { [key: string]: JSONValue }
10
+
11
+ /**
12
+ * A type that represents all the possible values of a JSON value.
13
+ */
5
14
  export type JSONValue = boolean | number | string | null | JSONObject | JSONArray;