@sv443-network/userutils 9.4.3 → 10.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 +40 -0
- package/LICENSE.txt +1 -1
- package/README.md +164 -86
- package/dist/UserUtils.cjs +1889 -0
- package/dist/UserUtils.mjs +1856 -0
- package/dist/UserUtils.umd.js +2078 -0
- package/dist/lib/Dialog.d.ts +2 -1
- package/dist/lib/Errors.d.ts +9 -0
- package/dist/lib/GMStorageEngine.d.ts +33 -0
- package/dist/lib/Mixins.d.ts +14 -11
- package/dist/lib/SelectorObserver.d.ts +1 -2
- package/dist/lib/dom.d.ts +8 -1
- package/dist/lib/index.d.ts +3 -11
- package/dist/lib/translation.d.ts +1 -1
- package/package.json +43 -50
- package/dist/index.cjs +0 -2076
- package/dist/index.global.js +0 -2118
- package/dist/index.js +0 -2019
- package/dist/lib/DataStore.d.ts +0 -164
- package/dist/lib/DataStoreSerializer.d.ts +0 -115
- package/dist/lib/Debouncer.d.ts +0 -86
- package/dist/lib/NanoEmitter.d.ts +0 -71
- package/dist/lib/array.d.ts +0 -17
- package/dist/lib/colors.d.ts +0 -25
- package/dist/lib/crypto.d.ts +0 -29
- package/dist/lib/errors.d.ts +0 -21
- package/dist/lib/math.d.ts +0 -81
- package/dist/lib/misc.d.ts +0 -69
- package/dist/lib/types.d.ts +0 -38
package/dist/lib/DataStore.d.ts
DELETED
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module lib/DataStore
|
|
3
|
-
* This module contains the DataStore class, which is a general purpose, sync and async persistent JSON database - [see the documentation for more info](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#datastore)
|
|
4
|
-
*/
|
|
5
|
-
import type { Prettify } from "./types.js";
|
|
6
|
-
/** Function that takes the data in the old format and returns the data in the new format. Also supports an asynchronous migration. */
|
|
7
|
-
type MigrationFunc = (oldData: any) => any | Promise<any>;
|
|
8
|
-
/** Dictionary of format version numbers and the function that migrates to them from the previous whole integer */
|
|
9
|
-
export type DataMigrationsDict = Record<number, MigrationFunc>;
|
|
10
|
-
/** Options for the DataStore instance */
|
|
11
|
-
export type DataStoreOptions<TData> = Prettify<{
|
|
12
|
-
/**
|
|
13
|
-
* A unique internal ID for this data store.
|
|
14
|
-
* To avoid conflicts with other scripts, it is recommended to use a prefix that is unique to your script.
|
|
15
|
-
* If you want to change the ID, you should make use of the {@linkcode DataStore.migrateId()} method.
|
|
16
|
-
*/
|
|
17
|
-
id: string;
|
|
18
|
-
/**
|
|
19
|
-
* The default data object to use if no data is saved in persistent storage yet.
|
|
20
|
-
* Until the data is loaded from persistent storage with {@linkcode DataStore.loadData()}, this will be the data returned by {@linkcode DataStore.getData()}.
|
|
21
|
-
*
|
|
22
|
-
* - ⚠️ This has to be an object that can be serialized to JSON using `JSON.stringify()`, so no functions or circular references are allowed, they will cause unexpected behavior.
|
|
23
|
-
*/
|
|
24
|
-
defaultData: TData;
|
|
25
|
-
/**
|
|
26
|
-
* An incremental, whole integer version number of the current format of data.
|
|
27
|
-
* If the format of the data is changed in any way, this number should be incremented, in which case all necessary functions of the migrations dictionary will be run consecutively.
|
|
28
|
-
*
|
|
29
|
-
* - ⚠️ Never decrement this number and optimally don't skip any numbers either!
|
|
30
|
-
*/
|
|
31
|
-
formatVersion: number;
|
|
32
|
-
/**
|
|
33
|
-
* A dictionary of functions that can be used to migrate data from older versions to newer ones.
|
|
34
|
-
* The keys of the dictionary should be the format version that the functions can migrate to, from the previous whole integer value.
|
|
35
|
-
* The values should be functions that take the data in the old format and return the data in the new format.
|
|
36
|
-
* The functions will be run in order from the oldest to the newest version.
|
|
37
|
-
* If the current format version is not in the dictionary, no migrations will be run.
|
|
38
|
-
*/
|
|
39
|
-
migrations?: DataMigrationsDict;
|
|
40
|
-
/**
|
|
41
|
-
* If an ID or multiple IDs are passed here, the data will be migrated from the old ID(s) to the current ID.
|
|
42
|
-
* This will happen once per page load, when {@linkcode DataStore.loadData()} is called.
|
|
43
|
-
* All future calls to {@linkcode DataStore.loadData()} in the session will not check for the old ID(s) anymore.
|
|
44
|
-
* To migrate IDs manually, use the method {@linkcode DataStore.migrateId()} instead.
|
|
45
|
-
*/
|
|
46
|
-
migrateIds?: string | string[];
|
|
47
|
-
/**
|
|
48
|
-
* Where the data should be saved (`"GM"` by default).
|
|
49
|
-
* The protected methods {@linkcode DataStore.getValue()}, {@linkcode DataStore.setValue()} and {@linkcode DataStore.deleteValue()} are used to interact with the storage.
|
|
50
|
-
* `"GM"` storage, `"localStorage"` and `"sessionStorage"` are supported out of the box, but in an extended class you can overwrite those methods to implement any other storage method.
|
|
51
|
-
*/
|
|
52
|
-
storageMethod?: "GM" | "localStorage" | "sessionStorage";
|
|
53
|
-
} & ({
|
|
54
|
-
/**
|
|
55
|
-
* Function to use to encode the data prior to saving it in persistent storage.
|
|
56
|
-
* If this is specified, make sure to declare {@linkcode decodeData()} as well.
|
|
57
|
-
*
|
|
58
|
-
* You can make use of UserUtils' [`compress()`](https://github.com/Sv443-Network/UserUtils?tab=readme-ov-file#compress) function here to make the data use up less space at the cost of a little bit of performance.
|
|
59
|
-
* @param data The input data as a serialized object (JSON string)
|
|
60
|
-
*/
|
|
61
|
-
encodeData: (data: string) => string | Promise<string>;
|
|
62
|
-
/**
|
|
63
|
-
* Function to use to decode the data after reading it from persistent storage.
|
|
64
|
-
* If this is specified, make sure to declare {@linkcode encodeData()} as well.
|
|
65
|
-
*
|
|
66
|
-
* You can make use of UserUtils' [`decompress()`](https://github.com/Sv443-Network/UserUtils?tab=readme-ov-file#decompress) function here to make the data use up less space at the cost of a little bit of performance.
|
|
67
|
-
* @returns The resulting data as a valid serialized object (JSON string)
|
|
68
|
-
*/
|
|
69
|
-
decodeData: (data: string) => string | Promise<string>;
|
|
70
|
-
} | {
|
|
71
|
-
encodeData?: never;
|
|
72
|
-
decodeData?: never;
|
|
73
|
-
})>;
|
|
74
|
-
/**
|
|
75
|
-
* Manages a hybrid synchronous & asynchronous persistent JSON database that is cached in memory and persistently saved across sessions using [GM storage](https://wiki.greasespot.net/GM.setValue) (default), [localStorage](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage) or [sessionStorage](https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage).
|
|
76
|
-
* Supports migrating data from older format versions to newer ones and populating the cache with default data if no persistent data is found.
|
|
77
|
-
* Can be overridden to implement any other storage method.
|
|
78
|
-
*
|
|
79
|
-
* All methods are `protected` or `public`, so you can easily extend this class and overwrite them to use a different storage method or to add other functionality.
|
|
80
|
-
* Remember that you can use `super.methodName()` in the subclass to call the original method if needed.
|
|
81
|
-
*
|
|
82
|
-
* - ⚠️ 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.
|
|
83
|
-
* - ⚠️ If the storageMethod is left as the default of `"GM"`, the directives `@grant GM.getValue` and `@grant GM.setValue` are required. If you then also use the method {@linkcode DataStore.deleteData()}, the extra directive `@grant GM.deleteValue` is needed too.
|
|
84
|
-
* - ⚠️ Make sure to call {@linkcode loadData()} at least once after creating an instance, or the returned data will be the same as `options.defaultData`
|
|
85
|
-
*
|
|
86
|
-
* @template TData The type of the data that is saved in persistent storage for the currently set format version (will be automatically inferred from `defaultData` if not provided)
|
|
87
|
-
*/
|
|
88
|
-
export declare class DataStore<TData extends object = object> {
|
|
89
|
-
readonly id: string;
|
|
90
|
-
readonly formatVersion: number;
|
|
91
|
-
readonly defaultData: TData;
|
|
92
|
-
readonly encodeData: DataStoreOptions<TData>["encodeData"];
|
|
93
|
-
readonly decodeData: DataStoreOptions<TData>["decodeData"];
|
|
94
|
-
readonly storageMethod: Required<DataStoreOptions<TData>>["storageMethod"];
|
|
95
|
-
private cachedData;
|
|
96
|
-
private migrations?;
|
|
97
|
-
private migrateIds;
|
|
98
|
-
/**
|
|
99
|
-
* Creates an instance of DataStore to manage a sync & async database that is cached in memory and persistently saved across sessions.
|
|
100
|
-
* Supports migrating data from older versions to newer ones and populating the cache with default data if no persistent data is found.
|
|
101
|
-
*
|
|
102
|
-
* - ⚠️ Requires the directives `@grant GM.getValue` and `@grant GM.setValue` if the storageMethod is left as the default of `"GM"`
|
|
103
|
-
* - ⚠️ Make sure to call {@linkcode loadData()} at least once after creating an instance, or the returned data will be the same as `options.defaultData`
|
|
104
|
-
*
|
|
105
|
-
* @template TData The type of the data that is saved in persistent storage for the currently set format version (will be automatically inferred from `defaultData` if not provided) - **This has to be a JSON-compatible object!** (no undefined, circular references, etc.)
|
|
106
|
-
* @param options The options for this DataStore instance
|
|
107
|
-
*/
|
|
108
|
-
constructor(options: DataStoreOptions<TData>);
|
|
109
|
-
/**
|
|
110
|
-
* Loads the data saved in persistent storage into the in-memory cache and also returns it.
|
|
111
|
-
* Automatically populates persistent storage with default data if it doesn't contain any data yet.
|
|
112
|
-
* Also runs all necessary migration functions if the data format has changed since the last time the data was saved.
|
|
113
|
-
*/
|
|
114
|
-
loadData(): Promise<TData>;
|
|
115
|
-
/**
|
|
116
|
-
* Returns a copy of the data from the in-memory cache.
|
|
117
|
-
* Use {@linkcode loadData()} to get fresh data from persistent storage (usually not necessary since the cache should always exactly reflect persistent storage).
|
|
118
|
-
* @param deepCopy Whether to return a deep copy of the data (default: `false`) - only necessary if your data object is nested and may have a bigger performance impact if enabled
|
|
119
|
-
*/
|
|
120
|
-
getData(deepCopy?: boolean): TData;
|
|
121
|
-
/** Saves the data synchronously to the in-memory cache and asynchronously to the persistent storage */
|
|
122
|
-
setData(data: TData): Promise<void>;
|
|
123
|
-
/** Saves the default data passed in the constructor synchronously to the in-memory cache and asynchronously to persistent storage */
|
|
124
|
-
saveDefaultData(): Promise<void>;
|
|
125
|
-
/**
|
|
126
|
-
* Call this method to clear all persistently stored data associated with this DataStore instance.
|
|
127
|
-
* The in-memory cache will be left untouched, so you may still access the data with {@linkcode getData()}
|
|
128
|
-
* Calling {@linkcode loadData()} or {@linkcode setData()} after this method was called will recreate persistent storage with the cached or default data.
|
|
129
|
-
*
|
|
130
|
-
* - ⚠️ This requires the additional directive `@grant GM.deleteValue` if the storageMethod is left as the default of `"GM"`
|
|
131
|
-
*/
|
|
132
|
-
deleteData(): Promise<void>;
|
|
133
|
-
/** Returns whether encoding and decoding are enabled for this DataStore instance */
|
|
134
|
-
encodingEnabled(): this is Required<Pick<DataStoreOptions<TData>, "encodeData" | "decodeData">>;
|
|
135
|
-
/**
|
|
136
|
-
* Runs all necessary migration functions consecutively and saves the result to the in-memory cache and persistent storage and also returns it.
|
|
137
|
-
* This method is automatically called by {@linkcode loadData()} if the data format has changed since the last time the data was saved.
|
|
138
|
-
* Though calling this method manually is not necessary, it can be useful if you want to run migrations for special occasions like a user importing potentially outdated data that has been previously exported.
|
|
139
|
-
*
|
|
140
|
-
* If one of the migrations fails, the data will be reset to the default value if `resetOnError` is set to `true` (default). Otherwise, an error will be thrown and no data will be saved.
|
|
141
|
-
*/
|
|
142
|
-
runMigrations(oldData: unknown, oldFmtVer: number, resetOnError?: boolean): Promise<TData>;
|
|
143
|
-
/**
|
|
144
|
-
* Tries to migrate the currently saved persistent data from one or more old IDs to the ID set in the constructor.
|
|
145
|
-
* If no data exist for the old ID(s), nothing will be done, but some time may still pass trying to fetch the non-existent data.
|
|
146
|
-
*/
|
|
147
|
-
migrateId(oldIds: string | string[]): Promise<void>;
|
|
148
|
-
/** Serializes the data using the optional this.encodeData() and returns it as a string */
|
|
149
|
-
protected serializeData(data: TData, useEncoding?: boolean): Promise<string>;
|
|
150
|
-
/** Deserializes the data using the optional this.decodeData() and returns it as a JSON object */
|
|
151
|
-
protected deserializeData(data: string, useEncoding?: boolean): Promise<TData>;
|
|
152
|
-
/** Copies a JSON-compatible object and loses all its internal references in the process */
|
|
153
|
-
protected deepCopy<T>(obj: T): T;
|
|
154
|
-
/** Gets a value from persistent storage - can be overwritten in a subclass if you want to use something other than the default storage methods */
|
|
155
|
-
protected getValue<TValue extends GM.Value = string>(name: string, defaultValue: TValue): Promise<string | TValue>;
|
|
156
|
-
/**
|
|
157
|
-
* Sets a value in persistent storage - can be overwritten in a subclass if you want to use something other than the default storage methods.
|
|
158
|
-
* The default storage engines will stringify all passed values like numbers or booleans, so be aware of that.
|
|
159
|
-
*/
|
|
160
|
-
protected setValue(name: string, value: GM.Value): Promise<void>;
|
|
161
|
-
/** Deletes a value from persistent storage - can be overwritten in a subclass if you want to use something other than the default storage methods */
|
|
162
|
-
protected deleteValue(name: string): Promise<void>;
|
|
163
|
-
}
|
|
164
|
-
export {};
|
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module lib/DataStoreSerializer
|
|
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
|
-
*/
|
|
5
|
-
import type { DataStore } from "./DataStore.js";
|
|
6
|
-
export type DataStoreSerializerOptions = {
|
|
7
|
-
/** Whether to add a checksum to the exported data. Defaults to `true` */
|
|
8
|
-
addChecksum?: boolean;
|
|
9
|
-
/** 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` */
|
|
10
|
-
ensureIntegrity?: boolean;
|
|
11
|
-
};
|
|
12
|
-
/** Serialized data of a DataStore instance */
|
|
13
|
-
export type SerializedDataStore = {
|
|
14
|
-
/** The ID of the DataStore instance */
|
|
15
|
-
id: string;
|
|
16
|
-
/** The serialized data */
|
|
17
|
-
data: string;
|
|
18
|
-
/** The format version of the data */
|
|
19
|
-
formatVersion: number;
|
|
20
|
-
/** Whether the data is encoded */
|
|
21
|
-
encoded: boolean;
|
|
22
|
-
/** The checksum of the data - key is not present when `addChecksum` is `false` */
|
|
23
|
-
checksum?: string;
|
|
24
|
-
};
|
|
25
|
-
/** Result of {@linkcode DataStoreSerializer.loadStoresData()} */
|
|
26
|
-
export type LoadStoresDataResult = {
|
|
27
|
-
/** The ID of the DataStore instance */
|
|
28
|
-
id: string;
|
|
29
|
-
/** The in-memory data object */
|
|
30
|
-
data: object;
|
|
31
|
-
};
|
|
32
|
-
/** A filter for selecting data stores */
|
|
33
|
-
export type StoreFilter = string[] | ((id: string) => boolean);
|
|
34
|
-
/**
|
|
35
|
-
* Allows for easy serialization and deserialization of multiple DataStore instances.
|
|
36
|
-
*
|
|
37
|
-
* All methods are at least `protected`, so you can easily extend this class and overwrite them to use a different storage method or to add additional functionality.
|
|
38
|
-
* Remember that you can call `super.methodName()` in the subclass to access the original method.
|
|
39
|
-
*
|
|
40
|
-
* - ⚠️ Needs to run in a secure context (HTTPS) due to the use of the SubtleCrypto API if checksumming is enabled.
|
|
41
|
-
*/
|
|
42
|
-
export declare class DataStoreSerializer {
|
|
43
|
-
protected stores: DataStore[];
|
|
44
|
-
protected options: Required<DataStoreSerializerOptions>;
|
|
45
|
-
constructor(stores: DataStore[], options?: DataStoreSerializerOptions);
|
|
46
|
-
/** Calculates the checksum of a string */
|
|
47
|
-
protected calcChecksum(input: string): Promise<string>;
|
|
48
|
-
/**
|
|
49
|
-
* Serializes only a subset of the data stores into a string.
|
|
50
|
-
* @param stores An array of store IDs or functions that take a store ID and return a boolean
|
|
51
|
-
* @param useEncoding Whether to encode the data using each DataStore's `encodeData()` method
|
|
52
|
-
* @param stringified Whether to return the result as a string or as an array of `SerializedDataStore` objects
|
|
53
|
-
*/
|
|
54
|
-
serializePartial(stores: StoreFilter, useEncoding?: boolean, stringified?: true): Promise<string>;
|
|
55
|
-
/**
|
|
56
|
-
* Serializes only a subset of the data stores into a string.
|
|
57
|
-
* @param stores An array of store IDs or functions that take a store ID and return a boolean
|
|
58
|
-
* @param useEncoding Whether to encode the data using each DataStore's `encodeData()` method
|
|
59
|
-
* @param stringified Whether to return the result as a string or as an array of `SerializedDataStore` objects
|
|
60
|
-
*/
|
|
61
|
-
serializePartial(stores: StoreFilter, useEncoding?: boolean, stringified?: false): Promise<SerializedDataStore[]>;
|
|
62
|
-
/**
|
|
63
|
-
* Serializes only a subset of the data stores into a string.
|
|
64
|
-
* @param stores An array of store IDs or functions that take a store ID and return a boolean
|
|
65
|
-
* @param useEncoding Whether to encode the data using each DataStore's `encodeData()` method
|
|
66
|
-
* @param stringified Whether to return the result as a string or as an array of `SerializedDataStore` objects
|
|
67
|
-
*/
|
|
68
|
-
serializePartial(stores: StoreFilter, useEncoding?: boolean, stringified?: boolean): Promise<string | SerializedDataStore[]>;
|
|
69
|
-
/**
|
|
70
|
-
* Serializes the data stores into a string.
|
|
71
|
-
* @param useEncoding Whether to encode the data using each DataStore's `encodeData()` method
|
|
72
|
-
* @param stringified Whether to return the result as a string or as an array of `SerializedDataStore` objects
|
|
73
|
-
*/
|
|
74
|
-
serialize(useEncoding?: boolean, stringified?: true): Promise<string>;
|
|
75
|
-
/**
|
|
76
|
-
* Serializes the data stores into a string.
|
|
77
|
-
* @param useEncoding Whether to encode the data using each DataStore's `encodeData()` method
|
|
78
|
-
* @param stringified Whether to return the result as a string or as an array of `SerializedDataStore` objects
|
|
79
|
-
*/
|
|
80
|
-
serialize(useEncoding?: boolean, stringified?: false): Promise<SerializedDataStore[]>;
|
|
81
|
-
/**
|
|
82
|
-
* Deserializes the data exported via {@linkcode serialize()} and imports only a subset into the DataStore instances.
|
|
83
|
-
* Also triggers the migration process if the data format has changed.
|
|
84
|
-
*/
|
|
85
|
-
deserializePartial(stores: StoreFilter, data: string | SerializedDataStore[]): Promise<void>;
|
|
86
|
-
/**
|
|
87
|
-
* Deserializes the data exported via {@linkcode serialize()} and imports the data into all matching DataStore instances.
|
|
88
|
-
* Also triggers the migration process if the data format has changed.
|
|
89
|
-
*/
|
|
90
|
-
deserialize(data: string | SerializedDataStore[]): Promise<void>;
|
|
91
|
-
/**
|
|
92
|
-
* Loads the persistent data of the DataStore instances into the in-memory cache.
|
|
93
|
-
* Also triggers the migration process if the data format has changed.
|
|
94
|
-
* @param stores An array of store IDs or a function that takes the store IDs and returns a boolean - if omitted, all stores will be loaded
|
|
95
|
-
* @returns Returns a PromiseSettledResult array with the results of each DataStore instance in the format `{ id: string, data: object }`
|
|
96
|
-
*/
|
|
97
|
-
loadStoresData(stores?: StoreFilter): Promise<PromiseSettledResult<LoadStoresDataResult>[]>;
|
|
98
|
-
/**
|
|
99
|
-
* Resets the persistent and in-memory data of the DataStore instances to their default values.
|
|
100
|
-
* @param stores An array of store IDs or a function that takes the store IDs and returns a boolean - if omitted, all stores will be affected
|
|
101
|
-
*/
|
|
102
|
-
resetStoresData(stores?: StoreFilter): Promise<PromiseSettledResult<void>[]>;
|
|
103
|
-
/**
|
|
104
|
-
* Deletes the persistent data of the DataStore instances.
|
|
105
|
-
* Leaves the in-memory data untouched.
|
|
106
|
-
* @param stores An array of store IDs or a function that takes the store IDs and returns a boolean - if omitted, all stores will be affected
|
|
107
|
-
*/
|
|
108
|
-
deleteStoresData(stores?: StoreFilter): Promise<PromiseSettledResult<void>[]>;
|
|
109
|
-
/** Checks if a given value is an array of SerializedDataStore objects */
|
|
110
|
-
static isSerializedDataStoreObjArray(obj: unknown): obj is SerializedDataStore[];
|
|
111
|
-
/** Checks if a given value is a SerializedDataStore object */
|
|
112
|
-
static isSerializedDataStoreObj(obj: unknown): obj is SerializedDataStore;
|
|
113
|
-
/** Returns the DataStore instances whose IDs match the provided array or function */
|
|
114
|
-
protected getStoresFiltered(stores?: StoreFilter): DataStore[];
|
|
115
|
-
}
|
package/dist/lib/Debouncer.d.ts
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module lib/Debouncer
|
|
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
|
-
*/
|
|
5
|
-
import { NanoEmitter } from "./NanoEmitter.js";
|
|
6
|
-
/**
|
|
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
|
-
* - `immediate` - (default & recommended) - calls the listeners at the very first call ("rising" edge) and queues the latest call until the timeout expires
|
|
9
|
-
* - Pros:
|
|
10
|
-
* - First call is let through immediately
|
|
11
|
-
* - Cons:
|
|
12
|
-
* - After all calls stop, the JS engine's event loop will continue to run until the last timeout expires (doesn't really matter on the web, but could cause a process exit delay in Node.js)
|
|
13
|
-
* - `idle` - queues all calls until there are no more calls in the given timeout duration ("falling" edge), and only then executes the very last call
|
|
14
|
-
* - Pros:
|
|
15
|
-
* - Makes sure there are zero calls in the given `timeoutDuration` before executing the last call
|
|
16
|
-
* - Cons:
|
|
17
|
-
* - Calls are always delayed by at least `1 * timeoutDuration`
|
|
18
|
-
* - Calls could get stuck in the queue indefinitely if there is no downtime between calls that is greater than the `timeoutDuration`
|
|
19
|
-
*/
|
|
20
|
-
export type DebouncerType = "immediate" | "idle";
|
|
21
|
-
type AnyFunc = (...args: any) => any;
|
|
22
|
-
/** The debounced function type that is returned by the {@linkcode debounce} function */
|
|
23
|
-
export type DebouncedFunction<TFunc extends AnyFunc> = ((...args: Parameters<TFunc>) => ReturnType<TFunc>) & {
|
|
24
|
-
debouncer: Debouncer<TFunc>;
|
|
25
|
-
};
|
|
26
|
-
/** Event map for the {@linkcode Debouncer} */
|
|
27
|
-
export type DebouncerEventMap<TFunc extends AnyFunc> = {
|
|
28
|
-
/** Emitted when the debouncer calls all registered listeners, as a pub-sub alternative */
|
|
29
|
-
call: TFunc;
|
|
30
|
-
/** Emitted when the timeout or edge type is changed after the instance was created */
|
|
31
|
-
change: (timeout: number, type: DebouncerType) => void;
|
|
32
|
-
};
|
|
33
|
-
/**
|
|
34
|
-
* A debouncer that calls all listeners after a specified timeout, discarding all calls in-between.
|
|
35
|
-
* It is very useful for event listeners that fire quickly, like `input` or `mousemove`, to prevent the listeners from being called too often and hogging resources.
|
|
36
|
-
* The exact behavior can be customized with the `type` parameter.
|
|
37
|
-
*
|
|
38
|
-
* The instance inherits from {@linkcode NanoEmitter} and emits the following events:
|
|
39
|
-
* - `call` - emitted when the debouncer calls all listeners - use this as a pub-sub alternative to the default callback-style listeners
|
|
40
|
-
* - `change` - emitted when the timeout or edge type is changed after the instance was created
|
|
41
|
-
*/
|
|
42
|
-
export declare class Debouncer<TFunc extends AnyFunc> extends NanoEmitter<DebouncerEventMap<TFunc>> {
|
|
43
|
-
protected timeout: number;
|
|
44
|
-
protected type: DebouncerType;
|
|
45
|
-
/** All registered listener functions and the time they were attached */
|
|
46
|
-
protected listeners: TFunc[];
|
|
47
|
-
/** The currently active timeout */
|
|
48
|
-
protected activeTimeout: ReturnType<typeof setTimeout> | undefined;
|
|
49
|
-
/** The latest queued call */
|
|
50
|
-
protected queuedCall: (() => void) | undefined;
|
|
51
|
-
/**
|
|
52
|
-
* Creates a new debouncer with the specified timeout and edge type.
|
|
53
|
-
* @param timeout Timeout in milliseconds between letting through calls - defaults to 200
|
|
54
|
-
* @param type The edge type to use for the debouncer - see {@linkcode DebouncerType} for details or [the documentation for an explanation and diagram](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#debouncer) - defaults to "immediate"
|
|
55
|
-
*/
|
|
56
|
-
constructor(timeout?: number, type?: DebouncerType);
|
|
57
|
-
/** Adds a listener function that will be called on timeout */
|
|
58
|
-
addListener(fn: TFunc): void;
|
|
59
|
-
/** Removes the listener with the specified function reference */
|
|
60
|
-
removeListener(fn: TFunc): void;
|
|
61
|
-
/** Removes all listeners */
|
|
62
|
-
removeAllListeners(): void;
|
|
63
|
-
/** Returns all registered listeners */
|
|
64
|
-
getListeners(): TFunc[];
|
|
65
|
-
/** Sets the timeout for the debouncer */
|
|
66
|
-
setTimeout(timeout: number): void;
|
|
67
|
-
/** Returns the current timeout */
|
|
68
|
-
getTimeout(): number;
|
|
69
|
-
/** Whether the timeout is currently active, meaning any latest call to the {@linkcode call()} method will be queued */
|
|
70
|
-
isTimeoutActive(): boolean;
|
|
71
|
-
/** Sets the edge type for the debouncer */
|
|
72
|
-
setType(type: DebouncerType): void;
|
|
73
|
-
/** Returns the current edge type */
|
|
74
|
-
getType(): DebouncerType;
|
|
75
|
-
/** Use this to call the debouncer with the specified arguments that will be passed to all listener functions registered with {@linkcode addListener()} */
|
|
76
|
-
call(...args: Parameters<TFunc>): void;
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* Creates a {@linkcode Debouncer} instance with the specified timeout and edge type and attaches the passed function as a listener.
|
|
80
|
-
* The returned function can be called with any arguments and will execute the `call()` method of the debouncer.
|
|
81
|
-
* The debouncer instance is accessible via the `debouncer` property of the returned function.
|
|
82
|
-
*
|
|
83
|
-
* Refer to the {@linkcode Debouncer} class definition or the [Debouncer documentation](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#debouncer) for more information.
|
|
84
|
-
*/
|
|
85
|
-
export declare function debounce<TFunc extends (...args: any[]) => any>(fn: TFunc, timeout?: number, type?: DebouncerType): DebouncedFunction<TFunc>;
|
|
86
|
-
export {};
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module lib/NanoEmitter
|
|
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
|
-
*/
|
|
5
|
-
import { type DefaultEvents, type Emitter, type EventsMap, type Unsubscribe } from "nanoevents";
|
|
6
|
-
export interface NanoEmitterOptions {
|
|
7
|
-
/** If set to true, allows emitting events through the public method emit() */
|
|
8
|
-
publicEmit: boolean;
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* Class that can be extended or instantiated by itself to create a lightweight event emitter with helper methods and a strongly typed event map.
|
|
12
|
-
* If extended from, you can use `this.events.emit()` to emit events, even if the `emit()` method doesn't work because `publicEmit` is not set to true in the constructor.
|
|
13
|
-
*/
|
|
14
|
-
export declare class NanoEmitter<TEvtMap extends EventsMap = DefaultEvents> {
|
|
15
|
-
protected readonly events: Emitter<TEvtMap>;
|
|
16
|
-
protected eventUnsubscribes: Unsubscribe[];
|
|
17
|
-
protected emitterOptions: NanoEmitterOptions;
|
|
18
|
-
/** Creates a new instance of NanoEmitter - a lightweight event emitter with helper methods and a strongly typed event map */
|
|
19
|
-
constructor(options?: Partial<NanoEmitterOptions>);
|
|
20
|
-
/**
|
|
21
|
-
* Subscribes to an event and calls the callback when it's emitted.
|
|
22
|
-
* @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 "_")
|
|
23
|
-
* @returns Returns a function that can be called to unsubscribe the event listener
|
|
24
|
-
* @example ```ts
|
|
25
|
-
* const emitter = new NanoEmitter<{
|
|
26
|
-
* foo: (bar: string) => void;
|
|
27
|
-
* }>({
|
|
28
|
-
* publicEmit: true,
|
|
29
|
-
* });
|
|
30
|
-
*
|
|
31
|
-
* let i = 0;
|
|
32
|
-
* const unsub = emitter.on("foo", (bar) => {
|
|
33
|
-
* // unsubscribe after 10 events:
|
|
34
|
-
* if(++i === 10) unsub();
|
|
35
|
-
* console.log(bar);
|
|
36
|
-
* });
|
|
37
|
-
*
|
|
38
|
-
* emitter.emit("foo", "bar");
|
|
39
|
-
* ```
|
|
40
|
-
*/
|
|
41
|
-
on<TKey extends keyof TEvtMap>(event: TKey | "_", cb: TEvtMap[TKey]): () => void;
|
|
42
|
-
/**
|
|
43
|
-
* Subscribes to an event and calls the callback or resolves the Promise only once when it's emitted.
|
|
44
|
-
* @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 "_")
|
|
45
|
-
* @param cb The callback to call when the event is emitted - if provided or not, the returned Promise will resolve with the event arguments
|
|
46
|
-
* @returns Returns a Promise that resolves with the event arguments when the event is emitted
|
|
47
|
-
* @example ```ts
|
|
48
|
-
* const emitter = new NanoEmitter<{
|
|
49
|
-
* foo: (bar: string) => void;
|
|
50
|
-
* }>();
|
|
51
|
-
*
|
|
52
|
-
* // Promise syntax:
|
|
53
|
-
* const [bar] = await emitter.once("foo");
|
|
54
|
-
* console.log(bar);
|
|
55
|
-
*
|
|
56
|
-
* // Callback syntax:
|
|
57
|
-
* emitter.once("foo", (bar) => console.log(bar));
|
|
58
|
-
* ```
|
|
59
|
-
*/
|
|
60
|
-
once<TKey extends keyof TEvtMap>(event: TKey | "_", cb?: TEvtMap[TKey]): Promise<Parameters<TEvtMap[TKey]>>;
|
|
61
|
-
/**
|
|
62
|
-
* Emits an event on this instance.
|
|
63
|
-
* ⚠️ Needs `publicEmit` to be set to true in the NanoEmitter constructor or super() call!
|
|
64
|
-
* @param event The event to emit
|
|
65
|
-
* @param args The arguments to pass to the event listeners
|
|
66
|
-
* @returns Returns true if `publicEmit` is true and the event was emitted successfully
|
|
67
|
-
*/
|
|
68
|
-
emit<TKey extends keyof TEvtMap>(event: TKey, ...args: Parameters<TEvtMap[TKey]>): boolean;
|
|
69
|
-
/** Unsubscribes all event listeners from this instance */
|
|
70
|
-
unsubscribeAll(): void;
|
|
71
|
-
}
|
package/dist/lib/array.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module lib/array
|
|
3
|
-
* This module contains various functions for working with arrays - [see the documentation for more info](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#arrays)
|
|
4
|
-
*/
|
|
5
|
-
/** Describes an array with at least one item */
|
|
6
|
-
export type NonEmptyArray<TArray = unknown> = [TArray, ...TArray[]];
|
|
7
|
-
/** Returns a random item from the passed array */
|
|
8
|
-
export declare function randomItem<TItem = unknown>(array: TItem[]): TItem | undefined;
|
|
9
|
-
/**
|
|
10
|
-
* Returns a tuple of a random item and its index from the passed array
|
|
11
|
-
* Returns `[undefined, undefined]` if the passed array is empty
|
|
12
|
-
*/
|
|
13
|
-
export declare function randomItemIndex<TItem = unknown>(array: TItem[]): [item?: TItem, index?: number];
|
|
14
|
-
/** Returns a random item from the passed array and mutates the array to remove the item */
|
|
15
|
-
export declare function takeRandomItem<TItem = unknown>(arr: TItem[]): TItem | undefined;
|
|
16
|
-
/** Returns a copy of the array with its items in a random order */
|
|
17
|
-
export declare function randomizeArray<TItem = unknown>(array: TItem[]): TItem[];
|
package/dist/lib/colors.d.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module lib/colors
|
|
3
|
-
* This module contains various functions for working with colors - [see the documentation for more info](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#colors)
|
|
4
|
-
*/
|
|
5
|
-
/**
|
|
6
|
-
* Converts a hex color string in the format `#RRGGBB`, `#RRGGBBAA` (or even `RRGGBB` and `RGB`) to a tuple.
|
|
7
|
-
* @returns Returns a tuple array where R, G and B are an integer from 0-255 and alpha is a float from 0 to 1, or undefined if no alpha channel exists.
|
|
8
|
-
*/
|
|
9
|
-
export declare function hexToRgb(hex: string): [red: number, green: number, blue: number, alpha?: number];
|
|
10
|
-
/** Converts RGB or RGBA number values to a hex color string in the format `#RRGGBB` or `#RRGGBBAA` */
|
|
11
|
-
export declare function rgbToHex(red: number, green: number, blue: number, alpha?: number, withHash?: boolean, upperCase?: boolean): string;
|
|
12
|
-
/**
|
|
13
|
-
* Lightens a CSS color value (in #HEX, rgb() or rgba() format) by a given percentage.
|
|
14
|
-
* Will not exceed the maximum range (00-FF or 0-255).
|
|
15
|
-
* @returns Returns the new color value in the same format as the input
|
|
16
|
-
* @throws Throws if the color format is invalid or not supported
|
|
17
|
-
*/
|
|
18
|
-
export declare function lightenColor(color: string, percent: number, upperCase?: boolean): string;
|
|
19
|
-
/**
|
|
20
|
-
* Darkens a CSS color value (in #HEX, rgb() or rgba() format) by a given percentage.
|
|
21
|
-
* Will not exceed the maximum range (00-FF or 0-255).
|
|
22
|
-
* @returns Returns the new color value in the same format as the input
|
|
23
|
-
* @throws Throws if the color format is invalid or not supported
|
|
24
|
-
*/
|
|
25
|
-
export declare function darkenColor(color: string, percent: number, upperCase?: boolean): string;
|
package/dist/lib/crypto.d.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module lib/crypto
|
|
3
|
-
* This module contains various cryptographic functions using the [Web Crypto API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API) - [see the documentation for more info](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#table-of-contents)
|
|
4
|
-
*/
|
|
5
|
-
/** Compresses a string or an ArrayBuffer using the provided {@linkcode compressionFormat} and returns it as a base64 string */
|
|
6
|
-
export declare function compress(input: string | ArrayBuffer, compressionFormat: CompressionFormat, outputType?: "string"): Promise<string>;
|
|
7
|
-
/** Compresses a string or an ArrayBuffer using the provided {@linkcode compressionFormat} and returns it as an ArrayBuffer */
|
|
8
|
-
export declare function compress(input: string | ArrayBuffer, compressionFormat: CompressionFormat, outputType: "arrayBuffer"): Promise<ArrayBuffer>;
|
|
9
|
-
/** Decompresses a previously compressed base64 string or ArrayBuffer, with the format passed by {@linkcode compressionFormat}, converted to a string */
|
|
10
|
-
export declare function decompress(input: string | ArrayBuffer, compressionFormat: CompressionFormat, outputType?: "string"): Promise<string>;
|
|
11
|
-
/** Decompresses a previously compressed base64 string or ArrayBuffer, with the format passed by {@linkcode compressionFormat}, converted to an ArrayBuffer */
|
|
12
|
-
export declare function decompress(input: string | ArrayBuffer, compressionFormat: CompressionFormat, outputType: "arrayBuffer"): Promise<ArrayBuffer>;
|
|
13
|
-
/**
|
|
14
|
-
* Creates a hash / checksum of the given {@linkcode input} string or ArrayBuffer using the specified {@linkcode algorithm} ("SHA-256" by default).
|
|
15
|
-
*
|
|
16
|
-
* - ⚠️ Uses the SubtleCrypto API so it needs to run in a secure context (HTTPS).
|
|
17
|
-
* - ⚠️ If you use this for cryptography, make sure to use a secure algorithm (under no circumstances use SHA-1) and to [salt](https://en.wikipedia.org/wiki/Salt_(cryptography)) your input data.
|
|
18
|
-
*/
|
|
19
|
-
export declare function computeHash(input: string | ArrayBuffer, algorithm?: string): Promise<string>;
|
|
20
|
-
/**
|
|
21
|
-
* Generates a random ID with the specified length and radix (16 characters and hexadecimal by default)
|
|
22
|
-
*
|
|
23
|
-
* - ⚠️ Not suitable for generating anything related to cryptography! Use [SubtleCrypto's `generateKey()`](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/generateKey) for that instead.
|
|
24
|
-
* @param length The length of the ID to generate (defaults to 16)
|
|
25
|
-
* @param radix The [radix](https://en.wikipedia.org/wiki/Radix) of each digit (defaults to 16 which is hexadecimal. Use 2 for binary, 10 for decimal, 36 for alphanumeric, etc.)
|
|
26
|
-
* @param enhancedEntropy If set to true, uses [`crypto.getRandomValues()`](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues) for better cryptographic randomness (this also makes it take longer to generate)
|
|
27
|
-
* @param randomCase If set to false, the generated ID will be lowercase only - also makes use of the `enhancedEntropy` parameter unless the output doesn't contain letters
|
|
28
|
-
*/
|
|
29
|
-
export declare function randomId(length?: number, radix?: number, enhancedEntropy?: boolean, randomCase?: boolean): string;
|
package/dist/lib/errors.d.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module lib/errors
|
|
3
|
-
* Contains custom error classes
|
|
4
|
-
*/
|
|
5
|
-
/** Base class for all UserUtils errors - adds a `date` prop set to the error throw time */
|
|
6
|
-
export declare class UUError extends Error {
|
|
7
|
-
readonly date: Date;
|
|
8
|
-
constructor(message: string, options?: ErrorOptions);
|
|
9
|
-
}
|
|
10
|
-
/** Error while validating checksum */
|
|
11
|
-
export declare class ChecksumMismatchError extends UUError {
|
|
12
|
-
constructor(message: string, options?: ErrorOptions);
|
|
13
|
-
}
|
|
14
|
-
/** Error while migrating data */
|
|
15
|
-
export declare class MigrationError extends UUError {
|
|
16
|
-
constructor(message: string, options?: ErrorOptions);
|
|
17
|
-
}
|
|
18
|
-
/** Error due to the platform, like using a feature that's not supported by the browser */
|
|
19
|
-
export declare class PlatformError extends UUError {
|
|
20
|
-
constructor(message: string, options?: ErrorOptions);
|
|
21
|
-
}
|
package/dist/lib/math.d.ts
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module lib/math
|
|
3
|
-
* This module contains various math functions - [see the documentation for more info](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#math)
|
|
4
|
-
*/
|
|
5
|
-
import type { Stringifiable } from "./types.js";
|
|
6
|
-
/** Ensures the passed {@linkcode value} always stays between {@linkcode min} and {@linkcode max} */
|
|
7
|
-
export declare function clamp(value: number, min: number, max: number): number;
|
|
8
|
-
/** Ensures the passed {@linkcode value} always stays between 0 and {@linkcode max} */
|
|
9
|
-
export declare function clamp(value: number, max: number): number;
|
|
10
|
-
/**
|
|
11
|
-
* Transforms the value parameter from the numerical range `range1min` to `range1max` to the numerical range `range2min` to `range2max`
|
|
12
|
-
* For example, you can map the value 2 in the range of 0-5 to the range of 0-10 and you'd get a 4 as a result.
|
|
13
|
-
*/
|
|
14
|
-
export declare function mapRange(value: number, range1min: number, range1max: number, range2min: number, range2max: number): number;
|
|
15
|
-
/**
|
|
16
|
-
* Transforms the value parameter from the numerical range `0` to `range1max` to the numerical range `0` to `range2max`
|
|
17
|
-
* For example, you can map the value 2 in the range of 0-5 to the range of 0-10 and you'd get a 4 as a result.
|
|
18
|
-
*/
|
|
19
|
-
export declare function mapRange(value: number, range1max: number, range2max: number): number;
|
|
20
|
-
/**
|
|
21
|
-
* Returns a random number between {@linkcode min} and {@linkcode max} (inclusive)
|
|
22
|
-
* Set {@linkcode enhancedEntropy} to true to use `crypto.getRandomValues()` for better cryptographic randomness (this also makes it take longer to generate)
|
|
23
|
-
*/
|
|
24
|
-
export declare function randRange(min: number, max: number, enhancedEntropy?: boolean): number;
|
|
25
|
-
/**
|
|
26
|
-
* Returns a random number between 0 and {@linkcode max} (inclusive)
|
|
27
|
-
* Set {@linkcode enhancedEntropy} to true to use `crypto.getRandomValues()` for better cryptographic randomness (this also makes it take longer to generate)
|
|
28
|
-
*/
|
|
29
|
-
export declare function randRange(max: number, enhancedEntropy?: boolean): number;
|
|
30
|
-
/**
|
|
31
|
-
* Calculates the amount of digits in the given number - the given number or string will be passed to the `Number()` constructor.
|
|
32
|
-
* Returns NaN if the number is invalid.
|
|
33
|
-
* @param num The number to count the digits of
|
|
34
|
-
* @param withDecimals Whether to count the decimal places as well (defaults to true)
|
|
35
|
-
* @example ```ts
|
|
36
|
-
* digitCount(); // NaN
|
|
37
|
-
* digitCount(0); // 1
|
|
38
|
-
* digitCount(123); // 3
|
|
39
|
-
* digitCount(123.456); // 6
|
|
40
|
-
* digitCount(Infinity); // Infinity
|
|
41
|
-
* ```
|
|
42
|
-
*/
|
|
43
|
-
export declare function digitCount(num: number | Stringifiable, withDecimals?: boolean): number;
|
|
44
|
-
/**
|
|
45
|
-
* Rounds {@linkcode num} to a fixed amount of decimal places, specified by {@linkcode fractionDigits} (supports negative values to round to the nearest power of 10).
|
|
46
|
-
* @example ```ts
|
|
47
|
-
* roundFixed(234.567, -2); // 200
|
|
48
|
-
* roundFixed(234.567, -1); // 230
|
|
49
|
-
* roundFixed(234.567, 0); // 235
|
|
50
|
-
* roundFixed(234.567, 1); // 234.6
|
|
51
|
-
* roundFixed(234.567, 2); // 234.57
|
|
52
|
-
* roundFixed(234.567, 3); // 234.567
|
|
53
|
-
* ```
|
|
54
|
-
*/
|
|
55
|
-
export declare function roundFixed(num: number, fractionDigits: number): number;
|
|
56
|
-
/**
|
|
57
|
-
* Checks if the {@linkcode bitSet} has the {@linkcode checkVal} set
|
|
58
|
-
* @example ```ts
|
|
59
|
-
* // the two vertically adjacent bits are tested for:
|
|
60
|
-
* bitSetHas(
|
|
61
|
-
* 0b1110,
|
|
62
|
-
* 0b0010,
|
|
63
|
-
* ); // true
|
|
64
|
-
*
|
|
65
|
-
* bitSetHas(
|
|
66
|
-
* 0b1110,
|
|
67
|
-
* 0b0001,
|
|
68
|
-
* ); // false
|
|
69
|
-
*
|
|
70
|
-
* // with TS enums (or JS maps):
|
|
71
|
-
* enum MyEnum {
|
|
72
|
-
* A = 1, B = 2, C = 4,
|
|
73
|
-
* D = 8, E = 16, F = 32,
|
|
74
|
-
* }
|
|
75
|
-
*
|
|
76
|
-
* const myBitSet = MyEnum.A | MyEnum.B;
|
|
77
|
-
* bitSetHas(myBitSet, MyEnum.B); // true
|
|
78
|
-
* bitSetHas(myBitSet, MyEnum.F); // false
|
|
79
|
-
* ```
|
|
80
|
-
*/
|
|
81
|
-
export declare function bitSetHas<TType extends number | bigint>(bitSet: TType, checkVal: TType): boolean;
|