@sv443-network/coreutils 2.0.2 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +38 -0
- package/README.md +4 -3
- package/dist/CoreUtils.cjs +188 -114
- package/dist/CoreUtils.min.cjs +5 -3
- package/dist/CoreUtils.min.mjs +5 -3
- package/dist/CoreUtils.min.umd.js +5 -3
- package/dist/CoreUtils.mjs +188 -114
- package/dist/CoreUtils.umd.js +196 -120
- package/dist/lib/DataStore.d.ts +23 -9
- package/dist/lib/DataStoreEngine.d.ts +10 -10
- package/dist/lib/DataStoreSerializer.d.ts +3 -1
- package/dist/lib/Debouncer.d.ts +1 -1
- package/dist/lib/Errors.d.ts +15 -3
- package/dist/lib/NanoEmitter.d.ts +7 -6
- package/dist/lib/TieredCache.d.ts +4 -4
- package/dist/lib/Translate.d.ts +1 -1
- package/dist/lib/crypto.d.ts +1 -1
- package/dist/lib/index.d.ts +13 -13
- package/dist/lib/math.d.ts +2 -2
- package/dist/lib/misc.d.ts +7 -1
- package/dist/lib/{TestDataStore.d.ts → test/DirectAccessDataStore.d.ts} +4 -3
- package/dist/lib/test/softExpect.d.ts +11 -0
- package/dist/lib/text.d.ts +1 -1
- package/dist/lib/types.d.ts +7 -0
- package/package.json +18 -18
package/dist/lib/DataStore.d.ts
CHANGED
|
@@ -2,12 +2,16 @@
|
|
|
2
2
|
* @module DataStore
|
|
3
3
|
* This module contains the DataStore class, which is a general purpose, sync and async persistent database for JSON-serializable data - [see the documentation for more info](https://github.com/Sv443-Network/CoreUtils/blob/main/docs.md#class-datastore)
|
|
4
4
|
*/
|
|
5
|
-
import type { DataStoreEngine } from "./DataStoreEngine.
|
|
6
|
-
import type { LooseUnion, Prettify, SerializableVal } from "./types.
|
|
5
|
+
import type { DataStoreEngine } from "./DataStoreEngine.ts";
|
|
6
|
+
import type { LooseUnion, Prettify, SerializableVal } from "./types.ts";
|
|
7
7
|
/** Function that takes the data in the old format and returns the data in the new format. Also supports an asynchronous migration. */
|
|
8
8
|
type MigrationFunc = (oldData: any) => any | Promise<any>;
|
|
9
9
|
/** Dictionary of format version numbers and the function that migrates to them from the previous whole integer */
|
|
10
10
|
export type DataMigrationsDict = Record<number, MigrationFunc>;
|
|
11
|
+
/** Tuple of a compression format identifier and a function to use to encode the data */
|
|
12
|
+
export type EncodeTuple = [format: LooseUnion<CompressionFormat> | null, encode: (data: string) => string | Promise<string>];
|
|
13
|
+
/** Tuple of a compression format identifier and a function to use to decode the data */
|
|
14
|
+
export type DecodeTuple = [format: LooseUnion<CompressionFormat> | null, decode: (data: string) => string | Promise<string>];
|
|
11
15
|
/** Options for the DataStore instance */
|
|
12
16
|
export type DataStoreOptions<TData extends DataStoreData> = Prettify<{
|
|
13
17
|
/**
|
|
@@ -52,12 +56,19 @@ export type DataStoreOptions<TData extends DataStoreData> = Prettify<{
|
|
|
52
56
|
* To migrate IDs manually, use the method {@linkcode DataStore.migrateId()} instead.
|
|
53
57
|
*/
|
|
54
58
|
migrateIds?: string | string[];
|
|
59
|
+
/**
|
|
60
|
+
* Whether to keep a copy of the data in memory for synchronous read access. Defaults to `true`.
|
|
61
|
+
*
|
|
62
|
+
* - ⚠️ If turned off, {@linkcode DataStore.getData()} will throw an error and only {@linkcode DataStore.loadData()} can be used to access the data.
|
|
63
|
+
* This may be useful if multiple sources are modifying the data, or the data is very large and you want to save memory, but it will make accessing the data slower, especially when combined with compression.
|
|
64
|
+
*/
|
|
65
|
+
memoryCache?: boolean;
|
|
55
66
|
} & ({
|
|
56
67
|
encodeData?: never;
|
|
57
68
|
decodeData?: never;
|
|
58
69
|
/**
|
|
59
70
|
* The format to use for compressing the data. Defaults to `deflate-raw`. Explicitly set to `null` to store data uncompressed.
|
|
60
|
-
* - ⚠️ Use either this property, or both `encodeData` and `decodeData`, but not
|
|
71
|
+
* - ⚠️ Use either this property, or both `encodeData` and `decodeData`, but not a combination of the three!
|
|
61
72
|
*/
|
|
62
73
|
compressionFormat?: CompressionFormat | null;
|
|
63
74
|
} | {
|
|
@@ -70,7 +81,7 @@ export type DataStoreOptions<TData extends DataStoreData> = Prettify<{
|
|
|
70
81
|
* You can make use of the [`compress()` function](https://github.com/Sv443-Network/CoreUtils/blob/main/docs.md#function-compress) here to make the data use up less space at the cost of a little bit of performance.
|
|
71
82
|
* @param data The input data as a serialized object (JSON string)
|
|
72
83
|
*/
|
|
73
|
-
encodeData:
|
|
84
|
+
encodeData: EncodeTuple;
|
|
74
85
|
/**
|
|
75
86
|
* Tuple of a compression format identifier and a function to use to decode the data after reading it from persistent storage.
|
|
76
87
|
* Set the identifier to `null` or `"identity"` to indicate that no traditional compression is used.
|
|
@@ -80,7 +91,7 @@ export type DataStoreOptions<TData extends DataStoreData> = Prettify<{
|
|
|
80
91
|
* You can make use of the [`decompress()` function](https://github.com/Sv443-Network/CoreUtils/blob/main/docs.md#function-decompress) here to make the data use up less space at the cost of a little bit of performance.
|
|
81
92
|
* @returns The resulting data as a valid serialized object (JSON string)
|
|
82
93
|
*/
|
|
83
|
-
decodeData:
|
|
94
|
+
decodeData: DecodeTuple;
|
|
84
95
|
compressionFormat?: never;
|
|
85
96
|
})>;
|
|
86
97
|
/** Generic type that represents the serializable data structure saved in a {@linkcode DataStore} instance. */
|
|
@@ -96,7 +107,8 @@ export type DataStoreData<TData extends SerializableVal = SerializableVal> = Rec
|
|
|
96
107
|
* - ⚠️ The data is stored as a JSON string, so only data compatible with [`JSON.stringify()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) can be used. Circular structures and complex objects (containing functions, symbols, etc.) will either throw an error on load and save or cause otherwise unexpected behavior. Properties with a value of `undefined` will be removed from the data prior to saving it, so use `null` instead.
|
|
97
108
|
* - ⚠️ Make sure to call {@linkcode loadData()} at least once after creating an instance, or the returned data will be the same as `options.defaultData`
|
|
98
109
|
*
|
|
99
|
-
* @template TData The type of the data that is saved in persistent storage for the currently set format version
|
|
110
|
+
* @template TData The type of the data that is saved in persistent storage for the currently set format version
|
|
111
|
+
* (TODO:FIXME: will be automatically inferred from `defaultData` if not provided)
|
|
100
112
|
*/
|
|
101
113
|
export declare class DataStore<TData extends DataStoreData> {
|
|
102
114
|
readonly id: string;
|
|
@@ -105,6 +117,7 @@ export declare class DataStore<TData extends DataStoreData> {
|
|
|
105
117
|
readonly encodeData: DataStoreOptions<TData>["encodeData"];
|
|
106
118
|
readonly decodeData: DataStoreOptions<TData>["decodeData"];
|
|
107
119
|
readonly compressionFormat: Exclude<DataStoreOptions<TData>["compressionFormat"], undefined>;
|
|
120
|
+
readonly memoryCache: boolean;
|
|
108
121
|
readonly engine: DataStoreEngine<TData>;
|
|
109
122
|
options: DataStoreOptions<TData>;
|
|
110
123
|
/**
|
|
@@ -114,9 +127,9 @@ export declare class DataStore<TData extends DataStoreData> {
|
|
|
114
127
|
*/
|
|
115
128
|
protected firstInit: boolean;
|
|
116
129
|
/** In-memory cached copy of the data that is saved in persistent storage used for synchronous read access. */
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
130
|
+
protected cachedData: TData;
|
|
131
|
+
protected migrations?: DataMigrationsDict;
|
|
132
|
+
protected migrateIds: string[];
|
|
120
133
|
/**
|
|
121
134
|
* Creates an instance of DataStore to manage a sync & async database that is cached in memory and persistently saved across sessions.
|
|
122
135
|
* Supports migrating data from older versions to newer ones and populating the cache with default data if no persistent data is found.
|
|
@@ -136,6 +149,7 @@ export declare class DataStore<TData extends DataStoreData> {
|
|
|
136
149
|
/**
|
|
137
150
|
* Returns a copy of the data from the in-memory cache.
|
|
138
151
|
* Use {@linkcode loadData()} to get fresh data from persistent storage (usually not necessary since the cache should always exactly reflect persistent storage).
|
|
152
|
+
* ⚠️ If `memoryCache` was set to `false` in the constructor options, this method will throw an error.
|
|
139
153
|
*/
|
|
140
154
|
getData(): TData;
|
|
141
155
|
/** Saves the data synchronously to the in-memory cache and asynchronously to the persistent storage */
|
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
* This module contains the `DataStoreEngine` class and some of its subclasses like `FileStorageEngine` and `BrowserStorageEngine`.
|
|
4
4
|
* [See the documentation for more info.](https://github.com/Sv443-Network/CoreUtils/blob/main/docs.md#class-datastoreengine)
|
|
5
5
|
*/
|
|
6
|
-
import type { DataStoreData, DataStoreOptions } from "./DataStore.
|
|
7
|
-
import type { Prettify, SerializableVal } from "./types.
|
|
6
|
+
import type { DataStoreData, DataStoreOptions } from "./DataStore.ts";
|
|
7
|
+
import type { Prettify, SerializableVal } from "./types.ts";
|
|
8
8
|
/** Contains the only properties of {@linkcode DataStoreOptions} that are relevant to the {@linkcode DataStoreEngine} class. */
|
|
9
9
|
export type DataStoreEngineDSOptions<TData extends DataStoreData> = Prettify<Pick<DataStoreOptions<TData>, "decodeData" | "encodeData" | "id">>;
|
|
10
10
|
export interface DataStoreEngine<TData extends DataStoreData> {
|
|
@@ -13,7 +13,7 @@ export interface DataStoreEngine<TData extends DataStoreData> {
|
|
|
13
13
|
}
|
|
14
14
|
/**
|
|
15
15
|
* Base class for creating {@linkcode DataStore} storage engines.
|
|
16
|
-
* This acts as an interchangeable API for writing and reading persistent data in various environments.
|
|
16
|
+
* This acts as an interchangeable API for writing and reading persistent JSON-serializable data in various environments.
|
|
17
17
|
*/
|
|
18
18
|
export declare abstract class DataStoreEngine<TData extends DataStoreData> {
|
|
19
19
|
protected dataStoreOptions: DataStoreEngineDSOptions<TData>;
|
|
@@ -26,7 +26,7 @@ export declare abstract class DataStoreEngine<TData extends DataStoreData> {
|
|
|
26
26
|
abstract setValue(name: string, value: SerializableVal): Promise<void>;
|
|
27
27
|
/** Deletes a value from persistent storage */
|
|
28
28
|
abstract deleteValue(name: string): Promise<void>;
|
|
29
|
-
/** Serializes the given object to a string, optionally encoded with `options.encodeData` if {@linkcode useEncoding} is set to
|
|
29
|
+
/** Serializes the given object to a string, optionally encoded with `options.encodeData` if {@linkcode useEncoding} is not set to false and the `encodeData` and `decodeData` options are set */
|
|
30
30
|
serializeData(data: TData, useEncoding?: boolean): Promise<string>;
|
|
31
31
|
/** Deserializes the given string to a JSON object, optionally decoded with `options.decodeData` if {@linkcode useEncoding} is set to true */
|
|
32
32
|
deserializeData(data: string, useEncoding?: boolean): Promise<TData>;
|
|
@@ -49,10 +49,10 @@ export type BrowserStorageEngineOptions = {
|
|
|
49
49
|
dataStoreOptions?: DataStoreEngineDSOptions<DataStoreData>;
|
|
50
50
|
};
|
|
51
51
|
/**
|
|
52
|
-
* Storage engine for the {@linkcode DataStore} class that uses the browser's LocalStorage or SessionStorage to store data.
|
|
52
|
+
* Storage engine for the {@linkcode DataStore} class that uses the browser's LocalStorage or SessionStorage to store JSON-serializable data.
|
|
53
53
|
*
|
|
54
54
|
* - ⚠️ Requires a DOM environment
|
|
55
|
-
* - ⚠️ Don't reuse
|
|
55
|
+
* - ⚠️ Don't reuse engine instances, always create a new one for each {@linkcode DataStore} instance
|
|
56
56
|
*/
|
|
57
57
|
export declare class BrowserStorageEngine<TData extends DataStoreData> extends DataStoreEngine<TData> {
|
|
58
58
|
protected options: BrowserStorageEngineOptions & Required<Pick<BrowserStorageEngineOptions, "type">>;
|
|
@@ -60,7 +60,7 @@ export declare class BrowserStorageEngine<TData extends DataStoreData> extends D
|
|
|
60
60
|
* Creates an instance of `BrowserStorageEngine`.
|
|
61
61
|
*
|
|
62
62
|
* - ⚠️ Requires a DOM environment
|
|
63
|
-
* - ⚠️ Don't reuse
|
|
63
|
+
* - ⚠️ Don't reuse engine instances, always create a new one for each {@linkcode DataStore} instance
|
|
64
64
|
*/
|
|
65
65
|
constructor(options?: BrowserStorageEngineOptions);
|
|
66
66
|
/** Fetches a value from persistent storage */
|
|
@@ -81,10 +81,10 @@ export type FileStorageEngineOptions = {
|
|
|
81
81
|
dataStoreOptions?: DataStoreEngineDSOptions<DataStoreData>;
|
|
82
82
|
};
|
|
83
83
|
/**
|
|
84
|
-
* Storage engine for the {@linkcode DataStore} class that uses a JSON file to store data.
|
|
84
|
+
* Storage engine for the {@linkcode DataStore} class that uses a JSON file to store JSON-serializable data.
|
|
85
85
|
*
|
|
86
86
|
* - ⚠️ Requires Node.js or Deno with Node compatibility (v1.31+)
|
|
87
|
-
* - ⚠️ Don't reuse
|
|
87
|
+
* - ⚠️ Don't reuse engine instances, always create a new one for each {@linkcode DataStore} instance
|
|
88
88
|
*/
|
|
89
89
|
export declare class FileStorageEngine<TData extends DataStoreData> extends DataStoreEngine<TData> {
|
|
90
90
|
protected options: FileStorageEngineOptions & Required<Pick<FileStorageEngineOptions, "filePath">>;
|
|
@@ -93,7 +93,7 @@ export declare class FileStorageEngine<TData extends DataStoreData> extends Data
|
|
|
93
93
|
* Creates an instance of `FileStorageEngine`.
|
|
94
94
|
*
|
|
95
95
|
* - ⚠️ Requires Node.js or Deno with Node compatibility (v1.31+)
|
|
96
|
-
* - ⚠️ Don't reuse
|
|
96
|
+
* - ⚠️ Don't reuse engine instances, always create a new one for each {@linkcode DataStore} instance
|
|
97
97
|
*/
|
|
98
98
|
constructor(options?: FileStorageEngineOptions);
|
|
99
99
|
/** Reads the file contents */
|
|
@@ -2,13 +2,15 @@
|
|
|
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, DataStoreData } from "./DataStore.
|
|
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` */
|
|
9
9
|
addChecksum?: boolean;
|
|
10
10
|
/** Whether to ensure the integrity of the data when importing it by throwing an error (doesn't throw when the checksum property doesn't exist). Defaults to `true` */
|
|
11
11
|
ensureIntegrity?: boolean;
|
|
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
|
+
remapIds?: Record<string, string[]>;
|
|
12
14
|
};
|
|
13
15
|
/** Meta object and serialized data of a DataStore instance */
|
|
14
16
|
export type SerializedDataStore = {
|
package/dist/lib/Debouncer.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @module Debouncer
|
|
3
3
|
* This module contains the Debouncer class and debounce function that allow you to reduce the amount of calls in rapidly firing event listeners and such - [see the documentation for more info](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#debouncer)
|
|
4
4
|
*/
|
|
5
|
-
import { NanoEmitter } from "./NanoEmitter.
|
|
5
|
+
import { NanoEmitter } from "./NanoEmitter.ts";
|
|
6
6
|
/**
|
|
7
7
|
* The type of edge to use for the debouncer - [see the docs for a diagram and explanation.](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#debouncer)
|
|
8
8
|
* - `immediate` - (default & recommended) - calls the listeners at the very first call ("rising" edge) and queues the latest call until the timeout expires
|
package/dist/lib/Errors.d.ts
CHANGED
|
@@ -7,15 +7,27 @@ export declare class DatedError extends Error {
|
|
|
7
7
|
readonly date: Date;
|
|
8
8
|
constructor(message: string, options?: ErrorOptions);
|
|
9
9
|
}
|
|
10
|
-
/** Error while validating checksum */
|
|
10
|
+
/** Error while validating checksum - extends {@linkcode DatedError} */
|
|
11
11
|
export declare class ChecksumMismatchError extends DatedError {
|
|
12
12
|
constructor(message: string, options?: ErrorOptions);
|
|
13
13
|
}
|
|
14
|
-
/**
|
|
14
|
+
/** Custom error class that can have a custom name - extends {@linkcode DatedError} */
|
|
15
|
+
export declare class CustomError extends DatedError {
|
|
16
|
+
constructor(name: string, message: string, options?: ErrorOptions);
|
|
17
|
+
}
|
|
18
|
+
/** Error while migrating data - extends {@linkcode DatedError} */
|
|
15
19
|
export declare class MigrationError extends DatedError {
|
|
16
20
|
constructor(message: string, options?: ErrorOptions);
|
|
17
21
|
}
|
|
18
|
-
/** Error while validating data */
|
|
22
|
+
/** Error while validating data - extends {@linkcode DatedError} */
|
|
19
23
|
export declare class ValidationError extends DatedError {
|
|
20
24
|
constructor(message: string, options?: ErrorOptions);
|
|
21
25
|
}
|
|
26
|
+
/** Error related to script context (e.g. trying to access APIs that aren't supported by the executing JS engine) - extends {@linkcode DatedError} */
|
|
27
|
+
export declare class ScriptContextError extends DatedError {
|
|
28
|
+
constructor(message: string, options?: ErrorOptions);
|
|
29
|
+
}
|
|
30
|
+
/** Error related to networking - extends {@linkcode DatedError} */
|
|
31
|
+
export declare class NetworkError extends DatedError {
|
|
32
|
+
constructor(message: string, options?: ErrorOptions);
|
|
33
|
+
}
|
|
@@ -3,22 +3,22 @@
|
|
|
3
3
|
* This module contains the NanoEmitter class, which is a tiny event emitter powered by [nanoevents](https://www.npmjs.com/package/nanoevents) - [see the documentation for more info](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#nanoemitter)
|
|
4
4
|
*/
|
|
5
5
|
import { type DefaultEvents, type Emitter, type EventsMap, type Unsubscribe } from "nanoevents";
|
|
6
|
-
import type { Prettify } from "./types.
|
|
6
|
+
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
10
|
}
|
|
11
11
|
type NanoEmitterOnMultiTriggerOptions<TEvtMap extends EventsMap, TKey extends keyof TEvtMap = keyof TEvtMap> = {
|
|
12
|
-
/** Calls the callback when one of the given events is emitted */
|
|
12
|
+
/** 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. */
|
|
13
13
|
oneOf?: TKey[];
|
|
14
|
-
/** Calls the callback when all of the given events are emitted */
|
|
14
|
+
/** Calls the callback when all of the given events are emitted. Either one of or both of `oneOf` and `allOf` need to be set. If both are set, they behave like an "AND" condition. */
|
|
15
15
|
allOf?: TKey[];
|
|
16
16
|
};
|
|
17
17
|
/** Options for the {@linkcode NanoEmitter.onMulti()} method */
|
|
18
18
|
export type NanoEmitterOnMultiOptions<TEvtMap extends EventsMap, TKey extends keyof TEvtMap = keyof TEvtMap> = Prettify<{
|
|
19
19
|
/** If true, the callback will be called only once for the first event (or set of events) that match the criteria */
|
|
20
20
|
once?: boolean;
|
|
21
|
-
/** If provided, can be used to
|
|
21
|
+
/** If provided, can be used to cancel the subscription if the signal is aborted */
|
|
22
22
|
signal?: AbortSignal;
|
|
23
23
|
/** The callback to call when the event with the given name is emitted */
|
|
24
24
|
callback: (event: TKey, ...args: Parameters<TEvtMap[TKey]>) => void;
|
|
@@ -80,11 +80,12 @@ export declare class NanoEmitter<TEvtMap extends EventsMap = DefaultEvents> {
|
|
|
80
80
|
* `callback` (required) is the function that will be called when the conditions are met.
|
|
81
81
|
*
|
|
82
82
|
* Set `once` to true to call the callback only once for the first event (or set of events) that match the criteria, then stop listening.
|
|
83
|
-
* If `signal` is provided, the subscription will be
|
|
83
|
+
* If `signal` is provided, the subscription will be canceled when the given signal is aborted.
|
|
84
84
|
*
|
|
85
85
|
* If `oneOf` is used, the callback will be called when any of the matching events are emitted.
|
|
86
86
|
* If `allOf` is used, the callback will be called after all of the matching events are emitted at least once, then any time any of them are emitted.
|
|
87
|
-
*
|
|
87
|
+
* If both `oneOf` and `allOf` are used together, the callback will be called when any of the `oneOf` events are emitted AND all of the `allOf` events have been emitted at least once.
|
|
88
|
+
* At least one of `oneOf` or `allOf` must be provided.
|
|
88
89
|
*
|
|
89
90
|
* @returns Returns a function that can be called to unsubscribe all listeners created by this call. Alternatively, pass an `AbortSignal` to all options objects to achieve the same effect or for finer control.
|
|
90
91
|
*/
|
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
* @module TieredCache
|
|
3
3
|
* This module contains the TieredCache class, which is a cache that can have multiple tiers with different max TTLs, with data being moved between tiers based on what is fetched the most.
|
|
4
4
|
*/
|
|
5
|
-
import { DataStore, type DataStoreData } from "./DataStore.
|
|
6
|
-
import { NanoEmitter, type NanoEmitterOptions } from "./NanoEmitter.
|
|
7
|
-
import type { DataStoreEngine } from "./DataStoreEngine.
|
|
8
|
-
import type { Prettify } from "./types.
|
|
5
|
+
import { DataStore, type DataStoreData } from "./DataStore.ts";
|
|
6
|
+
import { NanoEmitter, type NanoEmitterOptions } from "./NanoEmitter.ts";
|
|
7
|
+
import type { DataStoreEngine } from "./DataStoreEngine.ts";
|
|
8
|
+
import type { Prettify } from "./types.ts";
|
|
9
9
|
/** Options for the {@linkcode TieredCache} class. */
|
|
10
10
|
export type TieredCacheOptions<TData extends DataStoreData> = Prettify<{
|
|
11
11
|
/** Unique identifier for this cache. */
|
package/dist/lib/Translate.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @module Translate
|
|
3
3
|
* This module contains a sophisticated but still lightweight, JSON-based translation system - [see the documentation for more info](https://github.com/Sv443-Network/CoreUtils/blob/main/docs.md#class-translate)
|
|
4
4
|
*/
|
|
5
|
-
import type { Stringifiable } from "./types.
|
|
5
|
+
import type { Stringifiable } from "./types.ts";
|
|
6
6
|
/**
|
|
7
7
|
* Translation object to pass to {@linkcode tr.addTranslations()}
|
|
8
8
|
* Can be a flat object of identifier keys and the translation text as the value, or an infinitely nestable object containing the same.
|
package/dist/lib/crypto.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @module crypto
|
|
3
3
|
* This module contains various cryptographic functions, like compression and Uint8Array encoding - [see the documentation for more info](https://github.com/Sv443-Network/CoreUtils/blob/main/docs.md#crypto)
|
|
4
4
|
*/
|
|
5
|
-
import type { Stringifiable } from "./types.
|
|
5
|
+
import type { Stringifiable } from "./types.ts";
|
|
6
6
|
/** Converts an Uint8Array to a base64-encoded (ASCII) string */
|
|
7
7
|
export declare function abtoa(buf: Uint8Array): string;
|
|
8
8
|
/** Converts a base64-encoded (ASCII) string to an Uint8Array representation of its bytes */
|
package/dist/lib/index.d.ts
CHANGED
|
@@ -2,16 +2,16 @@
|
|
|
2
2
|
* @module @sv443-network/coreutils
|
|
3
3
|
* @description Cross-platform, general-purpose, JavaScript core library for Node, Deno and the browser. Intended to be used in conjunction with [`@sv443-network/userutils`](https://github.com/Sv443-Network/UserUtils) and [`@sv443-network/djsutils`](https://github.com/Sv443-Network/DJSUtils), but can be used independently as well.
|
|
4
4
|
*/
|
|
5
|
-
export * from "./array.
|
|
6
|
-
export * from "./colors.
|
|
7
|
-
export * from "./crypto.
|
|
8
|
-
export * from "./math.
|
|
9
|
-
export * from "./misc.
|
|
10
|
-
export * from "./text.
|
|
11
|
-
export * from "./types.
|
|
12
|
-
export * from "./DataStore.
|
|
13
|
-
export * from "./DataStoreEngine.
|
|
14
|
-
export * from "./DataStoreSerializer.
|
|
15
|
-
export * from "./Debouncer.
|
|
16
|
-
export * from "./Errors.
|
|
17
|
-
export * from "./NanoEmitter.
|
|
5
|
+
export * from "./array.ts";
|
|
6
|
+
export * from "./colors.ts";
|
|
7
|
+
export * from "./crypto.ts";
|
|
8
|
+
export * from "./math.ts";
|
|
9
|
+
export * from "./misc.ts";
|
|
10
|
+
export * from "./text.ts";
|
|
11
|
+
export * from "./types.ts";
|
|
12
|
+
export * from "./DataStore.ts";
|
|
13
|
+
export * from "./DataStoreEngine.ts";
|
|
14
|
+
export * from "./DataStoreSerializer.ts";
|
|
15
|
+
export * from "./Debouncer.ts";
|
|
16
|
+
export * from "./Errors.ts";
|
|
17
|
+
export * from "./NanoEmitter.ts";
|
package/dist/lib/math.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @module math
|
|
3
3
|
* This module contains general-purpose math functions like clamping, mapping and random number generation - [see the documentation for more info](https://github.com/Sv443-Network/CoreUtils/blob/main/docs.md#math)
|
|
4
4
|
*/
|
|
5
|
-
import type { NumberFormat, Stringifiable } from "./types.
|
|
5
|
+
import type { NumberFormat, Stringifiable } from "./types.ts";
|
|
6
6
|
/** Checks if the given {@linkcode bitSet} contains the given {@linkcode checkVal} */
|
|
7
7
|
export declare function bitSetHas<TType extends number | bigint>(bitSet: TType, checkVal: TType): boolean;
|
|
8
8
|
/** Ensures the passed {@linkcode value} always stays between {@linkcode min} and {@linkcode max} */
|
|
@@ -61,5 +61,5 @@ export declare function randRange(max: number, enhancedEntropy?: boolean): numbe
|
|
|
61
61
|
* ```
|
|
62
62
|
*/
|
|
63
63
|
export declare function roundFixed(num: number, fractionDigits: number): number;
|
|
64
|
-
/** Rounds the given values at the given decimal place (same as in {@linkcode roundFixed()}) and checks if they are within the given range (0.5 by default) */
|
|
64
|
+
/** Rounds the given values at the given decimal place (same as in {@linkcode roundFixed()}, 1 decimal place by default) and checks if they are within the given range (0.5 by default) */
|
|
65
65
|
export declare function valsWithin(a: number, b: number, dec?: number, withinRange?: number): boolean;
|
package/dist/lib/misc.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @module misc
|
|
3
3
|
* This module contains miscellaneous functions that don't fit in another category - [see the documentation for more info](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#misc)
|
|
4
4
|
*/
|
|
5
|
-
import type { ListLike, Prettify, Stringifiable } from "./types.
|
|
5
|
+
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.
|
|
@@ -71,3 +71,9 @@ export declare function setImmediateTimeoutLoop(callback: () => void | unknown |
|
|
|
71
71
|
* @throws An error if no exit method is available (e.g. in browser environments)
|
|
72
72
|
*/
|
|
73
73
|
export declare function scheduleExit(code?: number, timeout?: number): void;
|
|
74
|
+
/**
|
|
75
|
+
* Returns the current call stack, starting at the caller of this function.
|
|
76
|
+
* @param asArray Whether to return the stack as an array of strings or a single string. Defaults to true.
|
|
77
|
+
* @param lines The number of lines to return from the stack. Defaults to Infinity (all of them).
|
|
78
|
+
*/
|
|
79
|
+
export declare function getCallStack<TAsArray extends boolean = true>(asArray?: TAsArray, lines?: number): TAsArray extends true ? string[] : string;
|
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import { DataStore, type DataStoreData } from "
|
|
2
|
-
import type { SerializableVal } from "
|
|
1
|
+
import { DataStore, type DataStoreData } from "../DataStore.ts";
|
|
2
|
+
import type { SerializableVal } from "../types.ts";
|
|
3
3
|
/**
|
|
4
4
|
* A DataStore wrapper subclass that exposes internal methods for testing via the `direct_` prefixed methods.
|
|
5
5
|
*/
|
|
6
|
-
export declare class
|
|
6
|
+
export declare class DirectAccessDataStore<TData extends DataStoreData> extends DataStore<TData> {
|
|
7
7
|
direct_getValue<TValue extends SerializableVal = string>(name: string, defaultValue: TValue): Promise<string | TValue>;
|
|
8
8
|
direct_setValue(name: string, value: SerializableVal): Promise<void>;
|
|
9
9
|
direct_renameKey(oldName: string, newName: string): Promise<void>;
|
|
10
10
|
direct_deleteValue(name: string): Promise<void>;
|
|
11
11
|
direct_setFirstInit(value: boolean): void;
|
|
12
|
+
direct_getMemData(): TData;
|
|
12
13
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type Assertion } from "vitest";
|
|
2
|
+
/**
|
|
3
|
+
* Run an assertion in "soft" mode: catch assertion errors and log them instead of throwing.
|
|
4
|
+
*
|
|
5
|
+
* @example ```ts
|
|
6
|
+
* softExpect(() => expect(actual).toBe(42));
|
|
7
|
+
* softExpect(actual, e => e.toBe(42), "optional message");
|
|
8
|
+
* ```
|
|
9
|
+
*/
|
|
10
|
+
export declare function softExpect(assertion: () => void | unknown): void;
|
|
11
|
+
export declare function softExpect<T>(actual: T, assertion: (expectation: Assertion<T>) => void | unknown, message?: string): void;
|
package/dist/lib/text.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ListLike, Prettify, Stringifiable } from "./types.
|
|
1
|
+
import type { ListLike, Prettify, Stringifiable } from "./types.ts";
|
|
2
2
|
/**
|
|
3
3
|
* Automatically pluralizes the given string an `-s` or `-ies` to the passed {@linkcode term}, if {@linkcode num} is not equal to 1.
|
|
4
4
|
* By default, words ending in `-y` will have it replaced with `-ies`, and all other words will simply have `-s` appended.
|
package/dist/lib/types.d.ts
CHANGED
|
@@ -13,6 +13,13 @@ export type ListLike = unknown[] | {
|
|
|
13
13
|
/**
|
|
14
14
|
* A type that offers autocomplete for the passed union but also allows any arbitrary value of the same type to be passed.
|
|
15
15
|
* Supports unions of strings, numbers and objects.
|
|
16
|
+
* @example ```ts
|
|
17
|
+
* type MyUnion = LooseUnion<"foo" | "bar" | "baz">;
|
|
18
|
+
*
|
|
19
|
+
* const a: MyUnion = "foo"; // Valid
|
|
20
|
+
* const b: MyUnion = "qux"; // Also valid, even though "qux" is not part of the union
|
|
21
|
+
* const c: MyUnion = 123; // Not valid, because it's not the same type
|
|
22
|
+
* ```
|
|
16
23
|
*/
|
|
17
24
|
export type LooseUnion<TUnion extends string | number | object> = (TUnion) | (TUnion extends string ? (string & {}) : (TUnion extends number ? (number & {}) : (TUnion extends Record<keyof any, unknown> ? (object & {}) : never)));
|
|
18
25
|
/** Any class reference that can be instantiated with `new` */
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sv443-network/coreutils",
|
|
3
3
|
"libName": "@sv443-network/coreutils",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "3.0.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",
|
|
@@ -41,27 +41,27 @@
|
|
|
41
41
|
"nanoevents": "^9.1.0"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
|
-
"@changesets/cli": "^2.29.
|
|
45
|
-
"@eslint/eslintrc": "^3.3.
|
|
46
|
-
"@eslint/js": "^9.
|
|
47
|
-
"@swc/core": "^1.11
|
|
48
|
-
"@testing-library/dom": "^10.4.
|
|
49
|
-
"@types/deno": "^2.
|
|
50
|
-
"@types/node": "^22.
|
|
44
|
+
"@changesets/cli": "^2.29.8",
|
|
45
|
+
"@eslint/eslintrc": "^3.3.3",
|
|
46
|
+
"@eslint/js": "^9.39.2",
|
|
47
|
+
"@swc/core": "^1.15.11",
|
|
48
|
+
"@testing-library/dom": "^10.4.1",
|
|
49
|
+
"@types/deno": "^2.5.0",
|
|
50
|
+
"@types/node": "^22.19.7",
|
|
51
51
|
"@types/tx2": "^1.0.3",
|
|
52
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
53
|
-
"@typescript-eslint/parser": "^8.
|
|
54
|
-
"@typescript-eslint/utils": "^8.
|
|
55
|
-
"@vitest/coverage-v8": "^3.
|
|
52
|
+
"@typescript-eslint/eslint-plugin": "^8.54.0",
|
|
53
|
+
"@typescript-eslint/parser": "^8.54.0",
|
|
54
|
+
"@typescript-eslint/utils": "^8.54.0",
|
|
55
|
+
"@vitest/coverage-v8": "^3.2.4",
|
|
56
56
|
"esbuild-plugin-umd-wrapper": "^3.0.0",
|
|
57
|
-
"eslint": "^9.
|
|
58
|
-
"globals": "^16.
|
|
57
|
+
"eslint": "^9.39.2",
|
|
58
|
+
"globals": "^16.5.0",
|
|
59
59
|
"jsdom": "^26.1.0",
|
|
60
60
|
"tslib": "^2.8.1",
|
|
61
|
-
"tsup": "^8.5.
|
|
62
|
-
"tsx": "^4.
|
|
63
|
-
"typescript": "^5.
|
|
64
|
-
"vitest": "^3.
|
|
61
|
+
"tsup": "^8.5.1",
|
|
62
|
+
"tsx": "^4.21.0",
|
|
63
|
+
"typescript": "^5.9.3",
|
|
64
|
+
"vitest": "^3.2.4"
|
|
65
65
|
},
|
|
66
66
|
"files": [
|
|
67
67
|
"/dist/CoreUtils.cjs",
|