@sv443-network/coreutils 3.5.1 → 3.6.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/CHANGELOG.md +10 -0
- package/README.md +1 -0
- package/dist/CoreUtils.cjs +70 -28
- package/dist/CoreUtils.min.cjs +4 -4
- package/dist/CoreUtils.min.mjs +4 -4
- package/dist/CoreUtils.min.umd.js +4 -4
- package/dist/CoreUtils.mjs +70 -28
- package/dist/CoreUtils.umd.js +528 -606
- package/dist/lib/DataStoreSerializer.d.ts +13 -8
- package/dist/lib/NanoEmitter.d.ts +16 -1
- package/dist/lib/misc.d.ts +17 -4
- package/dist/lib/types.d.ts +1 -1
- package/package.json +1 -1
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @module DataStoreSerializer
|
|
3
3
|
* This module contains the DataStoreSerializer class, which allows you to import and export serialized DataStore data - [see the documentation for more info](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#datastoreserializer)
|
|
4
4
|
*/
|
|
5
|
-
import type { DataStore } from "./DataStore.ts";
|
|
5
|
+
import type { DataStore, DataStoreData } from "./DataStore.ts";
|
|
6
6
|
/** Options for the DataStoreSerializer class */
|
|
7
7
|
export type DataStoreSerializerOptions = {
|
|
8
8
|
/** Whether to add a checksum to the exported data. Defaults to `true` */
|
|
@@ -11,13 +11,18 @@ export type DataStoreSerializerOptions = {
|
|
|
11
11
|
ensureIntegrity?: boolean;
|
|
12
12
|
/** If provided, all stores with an ID in the value's array will be remapped to the key's ID when deserialization is called. If they don't match a DataStore instance's ID, nothing will happen. */
|
|
13
13
|
remapIds?: Record<string, string[]>;
|
|
14
|
+
/**
|
|
15
|
+
* Controls the type of the `data` property on {@linkcode SerializedDataStore} objects.
|
|
16
|
+
* When this is set to `false`, `data` will be the raw data object instead of a string, regardless of whether {@linkcode DataStoreSerializer.serialize()} or {@linkcode DataStoreSerializer.serializePartial()} are called with their `stringify` / `stringified` parameter set to `true` or `false`.
|
|
17
|
+
*/
|
|
18
|
+
stringifyData?: boolean;
|
|
14
19
|
};
|
|
15
20
|
/** Meta object and serialized data of a DataStore instance */
|
|
16
|
-
export type SerializedDataStore = {
|
|
21
|
+
export type SerializedDataStore<TData extends string | DataStoreData = string> = {
|
|
17
22
|
/** The ID of the DataStore instance */
|
|
18
23
|
id: string;
|
|
19
24
|
/** The serialized data */
|
|
20
|
-
data:
|
|
25
|
+
data: TData;
|
|
21
26
|
/** The format version of the data */
|
|
22
27
|
formatVersion: number;
|
|
23
28
|
/** Whether the data is encoded */
|
|
@@ -48,10 +53,10 @@ export declare class DataStoreSerializer {
|
|
|
48
53
|
protected options: Required<DataStoreSerializerOptions>;
|
|
49
54
|
constructor(stores: DataStore<any, boolean>[], options?: DataStoreSerializerOptions);
|
|
50
55
|
/**
|
|
51
|
-
* Calculates the checksum of a string. Uses {@linkcode computeHash()} with SHA-256 and digests as a hex string by default.
|
|
56
|
+
* Calculates the checksum of a string or {@linkcode DataStoreData} object. Uses {@linkcode computeHash()} with SHA-256 and digests as a hex string by default.
|
|
52
57
|
* Override this in a subclass if a custom checksum method is needed.
|
|
53
58
|
*/
|
|
54
|
-
protected calcChecksum(input: string): Promise<string>;
|
|
59
|
+
protected calcChecksum(input: string | DataStoreData): Promise<string>;
|
|
55
60
|
/**
|
|
56
61
|
* Serializes only a subset of the data stores into a string.
|
|
57
62
|
* @param stores An array of store IDs or functions that take a store ID and return a boolean
|
|
@@ -65,14 +70,14 @@ export declare class DataStoreSerializer {
|
|
|
65
70
|
* @param useEncoding Whether to encode the data using each DataStore's `encodeData()` method
|
|
66
71
|
* @param stringified Whether to return the result as a string or as an array of `SerializedDataStore` objects
|
|
67
72
|
*/
|
|
68
|
-
serializePartial(stores: StoreFilter, useEncoding?: boolean, stringified?: false): Promise<SerializedDataStore[]>;
|
|
73
|
+
serializePartial(stores: StoreFilter, useEncoding?: boolean, stringified?: false): Promise<SerializedDataStore<string | DataStoreData>[]>;
|
|
69
74
|
/**
|
|
70
75
|
* Serializes only a subset of the data stores into a string.
|
|
71
76
|
* @param stores An array of store IDs or functions that take a store ID and return a boolean
|
|
72
77
|
* @param useEncoding Whether to encode the data using each DataStore's `encodeData()` method
|
|
73
78
|
* @param stringified Whether to return the result as a string or as an array of `SerializedDataStore` objects
|
|
74
79
|
*/
|
|
75
|
-
serializePartial(stores: StoreFilter, useEncoding?: boolean, stringified?: boolean): Promise<string | SerializedDataStore[]>;
|
|
80
|
+
serializePartial(stores: StoreFilter, useEncoding?: boolean, stringified?: boolean): Promise<string | SerializedDataStore<string | DataStoreData>[]>;
|
|
76
81
|
/**
|
|
77
82
|
* Serializes the data stores into a string.
|
|
78
83
|
* @param useEncoding Whether to encode the data using each DataStore's `encodeData()` method
|
|
@@ -84,7 +89,7 @@ export declare class DataStoreSerializer {
|
|
|
84
89
|
* @param useEncoding Whether to encode the data using each DataStore's `encodeData()` method
|
|
85
90
|
* @param stringified Whether to return the result as a string or as an array of `SerializedDataStore` objects
|
|
86
91
|
*/
|
|
87
|
-
serialize(useEncoding?: boolean, stringified?: false): Promise<SerializedDataStore[]>;
|
|
92
|
+
serialize(useEncoding?: boolean, stringified?: false): Promise<SerializedDataStore<string | DataStoreData>[]>;
|
|
88
93
|
/**
|
|
89
94
|
* Deserializes the data exported via {@linkcode serialize()} and imports only a subset into the DataStore instances.
|
|
90
95
|
* Also triggers the migration process if the data format has changed.
|
|
@@ -7,6 +7,13 @@ import type { Prettify } from "./types.ts";
|
|
|
7
7
|
export interface NanoEmitterOptions {
|
|
8
8
|
/** If set to true, allows emitting events through the public method emit() */
|
|
9
9
|
publicEmit: boolean;
|
|
10
|
+
/**
|
|
11
|
+
* When provided, the emitter will remember the last arguments of each listed event.
|
|
12
|
+
* Any listener attached via `on()` or `once()` after the event has already fired will
|
|
13
|
+
* be immediately called / resolved with the cached arguments (catch-up behaviour).
|
|
14
|
+
* Only works for emissions that go through the public `emit()` or the protected `emitEvent()` method.
|
|
15
|
+
*/
|
|
16
|
+
catchUpEvents?: PropertyKey[];
|
|
10
17
|
}
|
|
11
18
|
type NanoEmitterOnMultiTriggerOptions<TEvtMap extends EventsMap, TKey extends keyof TEvtMap = keyof TEvtMap> = {
|
|
12
19
|
/** Calls the callback when one of the given events is emitted. Either one of or both of `oneOf` and `allOf` need to be set. If both are set, they behave like an "AND" condition. */
|
|
@@ -25,14 +32,22 @@ export type NanoEmitterOnMultiOptions<TEvtMap extends EventsMap, TKey extends ke
|
|
|
25
32
|
} & NanoEmitterOnMultiTriggerOptions<TEvtMap, TKey> & (Pick<Required<NanoEmitterOnMultiTriggerOptions<TEvtMap, TKey>>, "oneOf"> | Pick<Required<NanoEmitterOnMultiTriggerOptions<TEvtMap, TKey>>, "allOf">)>;
|
|
26
33
|
/**
|
|
27
34
|
* Class that can be extended or instantiated by itself to create a lightweight event emitter with helper methods and a strongly typed event map.
|
|
28
|
-
* If extended from,
|
|
35
|
+
* If extended from, prefer using `this.emitEvent()` over `this.events.emit()` — it updates the catch-up memory for any events listed in `catchUpEvents`.
|
|
29
36
|
*/
|
|
30
37
|
export declare class NanoEmitter<TEvtMap extends EventsMap = DefaultEvents> {
|
|
31
38
|
protected readonly events: Emitter<TEvtMap>;
|
|
32
39
|
protected eventUnsubscribes: Unsubscribe[];
|
|
33
40
|
protected emitterOptions: NanoEmitterOptions;
|
|
41
|
+
/** Stores the last arguments for each event listed in `catchUpEvents` */
|
|
42
|
+
protected catchUpMemory: Map<PropertyKey, unknown[]>;
|
|
34
43
|
/** Creates a new instance of NanoEmitter - a lightweight event emitter with helper methods and a strongly typed event map */
|
|
35
44
|
constructor(options?: Partial<NanoEmitterOptions>);
|
|
45
|
+
/**
|
|
46
|
+
* Emits an event on this instance, bypassing the `publicEmit` guard.
|
|
47
|
+
* Prefer this over `this.events.emit()` in subclasses — it updates catch-up memory
|
|
48
|
+
* for any event listed in `catchUpEvents` so late listeners can still receive the last value.
|
|
49
|
+
*/
|
|
50
|
+
protected emitEvent<TKey extends keyof TEvtMap>(event: TKey, ...args: Parameters<TEvtMap[TKey]>): void;
|
|
36
51
|
/**
|
|
37
52
|
* Subscribes to an event and calls the callback when it's emitted.
|
|
38
53
|
* @param event The event to subscribe to. Use `as "_"` in case your event names aren't thoroughly typed (like when using a template literal, e.g. \`event-${val}\` as "_")
|
package/dist/lib/misc.d.ts
CHANGED
|
@@ -6,24 +6,32 @@ import type { ListLike, Prettify, Stringifiable } from "./types.ts";
|
|
|
6
6
|
/**
|
|
7
7
|
* A ValueGen value is either its type, a promise that resolves to its type, or a function that returns its type, either synchronous or asynchronous.
|
|
8
8
|
* ValueGen allows for the utmost flexibility when applied to any type, as long as {@linkcode consumeGen()} is used to get the final value.
|
|
9
|
+
* The optional `TFn` parameter allows specifying the function variant's signature, enabling parametrized consumption via {@linkcode consumeGen()}.
|
|
9
10
|
* @template TValueType The type of the value that the ValueGen should yield
|
|
11
|
+
* @template TFn The function signature for the function variant - defaults to a no-arg function for backwards compatibility
|
|
10
12
|
*/
|
|
11
|
-
export type ValueGen<TValueType
|
|
13
|
+
export type ValueGen<TValueType, TFn extends (...args: any[]) => TValueType | Promise<TValueType> = () => TValueType | Promise<TValueType>> = TValueType | Promise<TValueType> | TFn;
|
|
12
14
|
/**
|
|
13
15
|
* Turns a {@linkcode ValueGen} into its final value.
|
|
16
|
+
* @param args Optional arguments forwarded to the function, if the ValueGen is a parametrized function
|
|
14
17
|
* @template TValueType The type of the value that the ValueGen should yield
|
|
18
|
+
* @template TFn The function signature for the function variant - inferred automatically
|
|
15
19
|
*/
|
|
16
|
-
export declare function consumeGen<TValueType>(valGen: ValueGen<TValueType>): Promise<TValueType>;
|
|
20
|
+
export declare function consumeGen<TValueType, TFn extends (...args: any[]) => TValueType | Promise<TValueType> = () => TValueType | Promise<TValueType>>(valGen: ValueGen<TValueType, TFn>, ...args: Parameters<TFn>): Promise<TValueType>;
|
|
17
21
|
/**
|
|
18
22
|
* A StringGen value is either a string, anything that can be converted to a string, or a function that returns one of the previous two, either synchronous or asynchronous, or a promise that returns a string.
|
|
19
23
|
* StringGen allows for the utmost flexibility when dealing with strings, as long as {@linkcode consumeStringGen()} is used to get the final string.
|
|
24
|
+
* The optional `TFn` parameter allows specifying the function variant's signature, enabling parametrized consumption via {@linkcode consumeStringGen()}.
|
|
25
|
+
* @template TFn The function signature for the function variant - defaults to a no-arg function for backwards compatibility
|
|
20
26
|
*/
|
|
21
|
-
export type StringGen = ValueGen<Stringifiable>;
|
|
27
|
+
export type StringGen<TFn extends (...args: any[]) => Stringifiable | Promise<Stringifiable> = () => Stringifiable | Promise<Stringifiable>> = ValueGen<Stringifiable, TFn>;
|
|
22
28
|
/**
|
|
23
29
|
* Turns a {@linkcode StringGen} into its final string value.
|
|
30
|
+
* @param args Optional arguments forwarded to the function, if the StringGen is a parametrized function
|
|
24
31
|
* @template TStrUnion The union of strings that the StringGen should yield - this allows for finer type control compared to {@linkcode consumeGen()}
|
|
32
|
+
* @template TFn The function signature for the function variant - inferred automatically
|
|
25
33
|
*/
|
|
26
|
-
export declare function consumeStringGen<TStrUnion extends string>(strGen: StringGen): Promise<TStrUnion>;
|
|
34
|
+
export declare function consumeStringGen<TStrUnion extends string, TFn extends (...args: any[]) => Stringifiable | Promise<Stringifiable> = () => Stringifiable | Promise<Stringifiable>>(strGen: StringGen<TFn>, ...args: Parameters<TFn>): Promise<TStrUnion>;
|
|
27
35
|
/** Options for the `fetchAdvanced()` function */
|
|
28
36
|
export type FetchAdvancedOpts = Prettify<Partial<{
|
|
29
37
|
/** Timeout in milliseconds after which the fetch call will be canceled with an AbortController signal */
|
|
@@ -50,6 +58,11 @@ export declare function pauseFor(time: number, signal?: AbortSignal, rejectOnAbo
|
|
|
50
58
|
* If no object is passed, it will return an empty object without prototype chain.
|
|
51
59
|
*/
|
|
52
60
|
export declare function pureObj<TObj extends object>(obj?: TObj): TObj;
|
|
61
|
+
/**
|
|
62
|
+
* Transforms an object's enumerable, own, string-keyed properties into getters that return the original values.
|
|
63
|
+
* Set `asCopy` to true to return a new object with the getterified properties instead of a live view of the original object.
|
|
64
|
+
*/
|
|
65
|
+
export declare function getterifyObj<TObj extends object>(obj: TObj, asCopy?: boolean): TObj;
|
|
53
66
|
/**
|
|
54
67
|
* Works similarly to `setInterval()`, but the callback is also called immediately and can be aborted with an `AbortSignal`.
|
|
55
68
|
* Uses `setInterval()` internally, which might cause overlapping calls if the callback's synchronous execution takes longer than the given interval time.
|
package/dist/lib/types.d.ts
CHANGED
|
@@ -35,7 +35,7 @@ export type NonEmptyString<TString extends string> = TString extends "" ? never
|
|
|
35
35
|
/** String constant that decides which set of number formatting options to use */
|
|
36
36
|
export type NumberFormat = "short" | "long";
|
|
37
37
|
/**
|
|
38
|
-
* Makes the structure of a type more readable by expanding
|
|
38
|
+
* Makes the structure of a type more readable by expanding other types merged via intersections.
|
|
39
39
|
* This can be useful for debugging or for improving the readability of complex types.
|
|
40
40
|
*/
|
|
41
41
|
export type Prettify<T> = {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sv443-network/coreutils",
|
|
3
3
|
"libName": "@sv443-network/coreutils",
|
|
4
|
-
"version": "3.
|
|
4
|
+
"version": "3.6.0",
|
|
5
5
|
"description": "Cross-platform, general-purpose, JavaScript core library for Node, Deno and the browser. Intended to be used in conjunction with `@sv443-network/userutils` and `@sv443-network/djsutils`, but can be used independently as well.",
|
|
6
6
|
"main": "dist/CoreUtils.cjs",
|
|
7
7
|
"module": "dist/CoreUtils.mjs",
|