@catstral/levy-utils 1.1.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1188 -17
- package/dist/array/index.d.ts +20 -8
- package/dist/array/utils/cluster/index.d.ts +2 -3
- package/dist/array/utils/concat/index.d.ts +14 -0
- package/dist/array/utils/counting/index.d.ts +2 -2
- package/dist/array/utils/filter/index.d.ts +55 -0
- package/dist/array/utils/filterMap/index.d.ts +14 -0
- package/dist/array/utils/first/index.d.ts +13 -0
- package/dist/array/utils/group/index.d.ts +14 -0
- package/dist/array/utils/hasItems/index.d.ts +13 -0
- package/dist/array/utils/isArray/index.d.ts +9 -0
- package/dist/array/utils/last/index.d.ts +14 -0
- package/dist/array/utils/list/index.d.ts +2 -2
- package/dist/array/utils/map/index.d.ts +29 -0
- package/dist/array/utils/max/index.d.ts +24 -0
- package/dist/array/utils/min/index.d.ts +24 -0
- package/dist/array/utils/overflow/index.d.ts +20 -0
- package/dist/array/utils/reduce/index.d.ts +44 -0
- package/dist/array/utils/select/index.d.ts +126 -9
- package/dist/array/utils/sift/index.d.ts +1 -1
- package/dist/array/utils/toggle/index.d.ts +2 -2
- package/dist/callback/index.d.ts +8 -5
- package/dist/callback/utils/noop/index.d.ts +8 -0
- package/dist/callback/utils/pipe/index.d.ts +12 -0
- package/dist/callback/utils/preset/index.d.ts +6 -4
- package/dist/callback/utils/retry/index.d.ts +2 -2
- package/dist/callback/utils/task/index.d.ts +12 -0
- package/dist/index.d.ts +9 -8
- package/dist/index.js +5 -0
- package/dist/misc/index.d.ts +17 -8
- package/dist/misc/utils/identity/index.d.ts +10 -0
- package/dist/misc/utils/isNull/index.d.ts +9 -0
- package/dist/misc/utils/isNullish/index.d.ts +13 -0
- package/dist/misc/utils/isPrimitive/index.d.ts +2 -2
- package/dist/misc/utils/isPromise/index.d.ts +9 -0
- package/dist/misc/utils/isPromiseLike/index.d.ts +9 -0
- package/dist/misc/utils/isUndefined/index.d.ts +9 -0
- package/dist/misc/utils/match/index.d.ts +1 -1
- package/dist/misc/utils/option/index.d.ts +54 -0
- package/dist/misc/utils/raise/index.d.ts +19 -0
- package/dist/misc/utils/range/index.d.ts +218 -11
- package/dist/misc/utils/readOnly/index.d.ts +1 -1
- package/dist/misc/utils/sleep/index.d.ts +2 -2
- package/dist/misc/utils/timeout/index.d.ts +40 -0
- package/dist/misc/utils/values/index.d.ts +1 -1
- package/dist/number/index.d.ts +13 -2
- package/dist/number/math/index.d.ts +2 -0
- package/dist/number/math/utils/isPrime/index.d.ts +9 -0
- package/dist/number/math/utils/mod/index.d.ts +10 -0
- package/dist/number/types.d.ts +11 -0
- package/dist/number/utils/clamp/index.d.ts +13 -0
- package/dist/number/utils/isDecimal/index.d.ts +11 -0
- package/dist/number/utils/isEven/index.d.ts +9 -0
- package/dist/number/utils/isInteger/index.d.ts +11 -0
- package/dist/number/utils/isOdd/index.d.ts +9 -0
- package/dist/number/utils/negative/index.d.ts +21 -0
- package/dist/number/utils/positive/index.d.ts +21 -0
- package/dist/number/utils/sign/index.d.ts +14 -0
- package/dist/number/utils/toInt/index.d.ts +1 -1
- package/dist/number/utils/trunc/index.d.ts +16 -0
- package/dist/object/index.d.ts +6 -6
- package/dist/object/utils/extend/index.d.ts +11 -4
- package/dist/random/helper.d.ts +6 -0
- package/dist/random/index.d.ts +6 -0
- package/dist/random/utils/chance/index.d.ts +13 -0
- package/dist/random/utils/draw/index.d.ts +13 -0
- package/dist/random/utils/maybe/index.d.ts +8 -0
- package/dist/random/utils/random/index.d.ts +14 -0
- package/dist/random/utils/scatter/index.d.ts +30 -0
- package/dist/random/utils/shuffle/index.d.ts +11 -0
- package/dist/random/utils/uid/index.d.ts +12 -0
- package/dist/random/uuid/helpers.d.ts +4 -0
- package/dist/random/uuid/index.d.ts +5 -0
- package/dist/random/uuid/types.d.ts +16 -0
- package/dist/random/uuid/utils/max/index.d.ts +8 -0
- package/dist/random/uuid/utils/nil/index.d.ts +8 -0
- package/dist/random/uuid/utils/v1/index.d.ts +46 -0
- package/dist/random/uuid/utils/v4/index.d.ts +22 -0
- package/dist/random/uuid/utils/v7/index.d.ts +34 -0
- package/dist/string/casing/index.d.ts +11 -11
- package/dist/string/casing/utils/camel/index.d.ts +1 -1
- package/dist/string/casing/utils/custom/index.d.ts +1 -1
- package/dist/string/casing/utils/http/index.d.ts +1 -1
- package/dist/string/casing/utils/kebab/index.d.ts +1 -1
- package/dist/string/casing/utils/pascal/index.d.ts +1 -1
- package/dist/string/casing/utils/snake/index.d.ts +1 -1
- package/dist/string/casing/utils/title/index.d.ts +1 -1
- package/dist/string/index.d.ts +4 -4
- package/dist/types.d.ts +2 -0
- package/package.json +9 -14
- package/dist/levyUtils.cjs +0 -5
- package/dist/levyUtils.js +0 -1244
package/dist/array/index.d.ts
CHANGED
|
@@ -1,8 +1,20 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
5
|
-
export * from
|
|
6
|
-
export * from
|
|
7
|
-
export * from
|
|
8
|
-
export * from
|
|
1
|
+
export * from "./utils/cluster";
|
|
2
|
+
export * from "./utils/concat";
|
|
3
|
+
export * from "./utils/counting";
|
|
4
|
+
export * from "./utils/filter";
|
|
5
|
+
export * from "./utils/filterMap";
|
|
6
|
+
export * from "./utils/first";
|
|
7
|
+
export * from "./utils/fork";
|
|
8
|
+
export * from "./utils/group";
|
|
9
|
+
export * from "./utils/hasItems";
|
|
10
|
+
export * from "./utils/last";
|
|
11
|
+
export * from "./utils/list";
|
|
12
|
+
export * from "./utils/map";
|
|
13
|
+
export * from "./utils/max";
|
|
14
|
+
export * from "./utils/min";
|
|
15
|
+
export * from "./utils/overflow";
|
|
16
|
+
export * from "./utils/reduce";
|
|
17
|
+
export * from "./utils/select";
|
|
18
|
+
export * from "./utils/sift";
|
|
19
|
+
export * from "./utils/sum";
|
|
20
|
+
export * from "./utils/toggle";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { UtilError } from
|
|
1
|
+
import { UtilError } from "~/error";
|
|
2
2
|
export declare class ClusterUtilError extends UtilError {
|
|
3
3
|
readonly util = "cluster";
|
|
4
4
|
}
|
|
@@ -11,7 +11,6 @@ export declare class ClusterUtilError extends UtilError {
|
|
|
11
11
|
* @param {T[]} items The items to cluster
|
|
12
12
|
* @param size The max size for each list
|
|
13
13
|
* @returns {T[][]} An array of arrays with a max size of the specified size
|
|
14
|
-
* @throws {ClusterUtilError} If the cluster size is less
|
|
15
|
-
* @throws {ClusterUtilError} If the given items are not an array
|
|
14
|
+
* @throws {ClusterUtilError} If the cluster size is less than 1
|
|
16
15
|
*/
|
|
17
16
|
export declare function cluster<const T>(items: T[], size: number): T[][];
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type Concat<T extends (unknown[] | unknown)[]> = T extends [infer V, ...infer R] ? [...(V extends readonly unknown[] ? V : [V]), ...Concat<R>] : [];
|
|
2
|
+
/**
|
|
3
|
+
* Concatenates values and arrays into a new array.
|
|
4
|
+
*
|
|
5
|
+
* Array arguments are flattened by one level.
|
|
6
|
+
* Non-array arguments are added as individual values.
|
|
7
|
+
*
|
|
8
|
+
* The time complexity for this is `O(n)` where `n` is the amount of values.
|
|
9
|
+
*
|
|
10
|
+
* @template {(? | ?[])[]} T
|
|
11
|
+
* @param {...T} values Values or arrays of values to concatenate
|
|
12
|
+
* @returns {Concat<T>} A new array containing all provided values
|
|
13
|
+
*/
|
|
14
|
+
export declare function concat<const T extends (unknown | unknown[])[]>(...values: T): Concat<T>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Key } from
|
|
1
|
+
import type { Key } from "~/types";
|
|
2
2
|
/**
|
|
3
|
-
* Reduces a list down to an object of keys
|
|
3
|
+
* Reduces a list down to an object of keys specified by the given `identity` callback
|
|
4
4
|
* with a number of how many times that identity has been seen.
|
|
5
5
|
*
|
|
6
6
|
* The time complexity for this is `O(n)` where `n` is the amount of items in the list.
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { Promisable } from "~/types";
|
|
2
|
+
/**
|
|
3
|
+
* Returns a new list containing the items of a given list that pass a predicate.
|
|
4
|
+
*
|
|
5
|
+
* If the predicate returns a promise for any item, a promise is returned that resolves once all of them have.
|
|
6
|
+
*
|
|
7
|
+
* The time complexity for this is `O(n)` where `n` is the amount of items in the given list.
|
|
8
|
+
*
|
|
9
|
+
* @template T
|
|
10
|
+
* @template {T} R
|
|
11
|
+
* @param {readonly T[]} list The array to filter values from
|
|
12
|
+
* @param {(item: T, index: number, list: readonly T[]) => item is R} predicate A callback that returns whether an item is filtered
|
|
13
|
+
* @returns {R[]} A new list containing the filtered items, or a promise of it if the predicate can return a promise
|
|
14
|
+
*/
|
|
15
|
+
export declare function filter<const T, const R extends T>(list: readonly T[], predicate: (item: T, index: number, list: readonly T[]) => item is R): R[];
|
|
16
|
+
/**
|
|
17
|
+
* Returns a new list containing the items of a given list that pass a predicate.
|
|
18
|
+
*
|
|
19
|
+
* If the predicate returns a promise for any item, a promise is returned that resolves once all of them have.
|
|
20
|
+
*
|
|
21
|
+
* The time complexity for this is `O(n)` where `n` is the amount of items in the given list.
|
|
22
|
+
*
|
|
23
|
+
* @template T
|
|
24
|
+
* @param {readonly T[]} list The array to filter values from
|
|
25
|
+
* @param {(item: T, index: number, list: readonly T[]) => Promise<unknown>} predicate A callback that returns whether an item is filtered
|
|
26
|
+
* @returns {Promise<T[]>} A new list containing the filtered items, or a promise of it if the predicate can return a promise
|
|
27
|
+
*/
|
|
28
|
+
export declare function filter<const T>(list: readonly T[], predicate: (item: T, index: number, list: readonly T[]) => Promise<unknown>): Promise<T[]>;
|
|
29
|
+
/**
|
|
30
|
+
* Returns a new list containing the items of a given list that pass a predicate.
|
|
31
|
+
*
|
|
32
|
+
* If the predicate returns a promise for any item, a promise is returned that resolves once all of them have.
|
|
33
|
+
*
|
|
34
|
+
* The time complexity for this is `O(n)` where `n` is the amount of items in the given list.
|
|
35
|
+
*
|
|
36
|
+
* @template T
|
|
37
|
+
* @template P
|
|
38
|
+
* @param {readonly T[]} list The array to filter values from
|
|
39
|
+
* @param {(item: T, index: number, list: readonly T[]) => P} predicate A callback that returns whether an item is filtered
|
|
40
|
+
* @returns {[Extract<P, Promise<unknown>>] extends [never] ? T[] : Promisable<T[]>} A new list containing the filtered items, or a promise of it if the predicate can return a promise
|
|
41
|
+
*/
|
|
42
|
+
export declare function filter<const T, const P>(list: readonly T[], predicate: (item: T, index: number, list: readonly T[]) => P): [Extract<P, Promise<unknown>>] extends [never] ? T[] : Promisable<T[]>;
|
|
43
|
+
/**
|
|
44
|
+
* Returns a new list containing the items of a given list that pass a predicate.
|
|
45
|
+
*
|
|
46
|
+
* If the predicate returns a promise for any item, a promise is returned that resolves once all of them have.
|
|
47
|
+
*
|
|
48
|
+
* The time complexity for this is `O(n)` where `n` is the amount of items in the given list.
|
|
49
|
+
*
|
|
50
|
+
* @template T
|
|
51
|
+
* @param {readonly T[]} list The array to filter values from
|
|
52
|
+
* @param {(item: T, index: number, list: readonly T[]) => unknown} predicate A callback that returns whether an item is filtered
|
|
53
|
+
* @returns {T[]} A new list containing the filtered items, or a promise of it if the predicate can return a promise
|
|
54
|
+
*/
|
|
55
|
+
export declare function filter<const T>(list: readonly T[], predicate: (item: T, index: number, list: readonly T[]) => unknown): T[];
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a new list of items based on filtered and mapped values of a given list.
|
|
3
|
+
*
|
|
4
|
+
* The time complexity for this is `O(n)` where `n` is the amount of items in the given list.
|
|
5
|
+
*
|
|
6
|
+
* @template T
|
|
7
|
+
* @template R
|
|
8
|
+
* @param {T[] | readonly T[]} list The list to filter and map values through
|
|
9
|
+
* @param {(item: T, index: number) => boolean} filter A callback to filter out values from the given list
|
|
10
|
+
* @param {(item: T, index: number, originalIndex: number) => R} mapper A mapper to convert the filtered values
|
|
11
|
+
* @returns {R[]} A new list with all filtered and mapped values
|
|
12
|
+
*/
|
|
13
|
+
export declare function filterMap<const T, const V extends T, const R>(list: T[] | readonly T[], filter: (item: T, index: number) => item is V, mapper: (item: V, index: number, originalIndex: number) => R): R[];
|
|
14
|
+
export declare function filterMap<const T, const R>(list: T[] | readonly T[], filter: (item: T, index: number) => boolean, mapper: (item: T, index: number, originalIndex: number) => R): R[];
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export type First<T extends readonly unknown[], F = undefined> = T extends readonly [infer V, ...T[number][]] ? V : T[number] | (T extends readonly [...T[number][], T[number]] ? never : F);
|
|
2
|
+
/**
|
|
3
|
+
* Returns the first item of a list or a fallback if the list is empty.
|
|
4
|
+
*
|
|
5
|
+
* The time complexity for this is `O(1)`.
|
|
6
|
+
*
|
|
7
|
+
* @template T
|
|
8
|
+
* @template [F=undefined]
|
|
9
|
+
* @param list The array to get the first item from
|
|
10
|
+
* @param [fallback] The value to fall back to if the list is empty
|
|
11
|
+
* @returns {First<T, F>} The first item of the list or a fallback
|
|
12
|
+
*/
|
|
13
|
+
export declare function first<const T extends readonly unknown[], const F = undefined>(list: T, fallback?: F): First<T, F>;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Key } from "~/types";
|
|
2
|
+
/**
|
|
3
|
+
* Reduces a list down to an object of keys specified by the given `identity` callback.
|
|
4
|
+
*
|
|
5
|
+
* The time complexity for this is `O(n)` where `n` is the amount of items in the list.
|
|
6
|
+
*
|
|
7
|
+
* @template T
|
|
8
|
+
* @template {Key} K
|
|
9
|
+
* @param {T} list The list to count
|
|
10
|
+
* @param {(item: T) => K} identity A function to identify what value to count
|
|
11
|
+
* @returns {Partial<Record<K, T[]>>} A keyed object with how many times that key showed up.
|
|
12
|
+
* Do note that the keys that didn't show up are not on this object
|
|
13
|
+
*/
|
|
14
|
+
export declare function group<const T, const K extends Key = Key>(list: T[], identity: (item: T) => K): Partial<Record<K, T[]>>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if the given array has at least one item.
|
|
3
|
+
*
|
|
4
|
+
* The time complexity for this is `O(1)`.
|
|
5
|
+
*
|
|
6
|
+
* @templat T
|
|
7
|
+
* @param {T[]} list The array to check
|
|
8
|
+
* @returns {boolean} A boolean to signal if the given array has at least one item
|
|
9
|
+
*/
|
|
10
|
+
export declare function hasItems<const T>(list: [...T[], T]): list is [...T[], T];
|
|
11
|
+
export declare function hasItems<const T>(list: T[]): list is [T, ...T[]];
|
|
12
|
+
export declare function hasItems<const T>(list: readonly [...T[], T]): list is readonly [...T[], T];
|
|
13
|
+
export declare function hasItems<const T>(list: readonly T[]): list is readonly [T, ...T[]];
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if the given item is an array.
|
|
3
|
+
*
|
|
4
|
+
* The time complexity for this is `O(1)`.
|
|
5
|
+
*
|
|
6
|
+
* @param {?} value The value to check
|
|
7
|
+
* @returns {boolean} A boolean to signal if the given item is an array
|
|
8
|
+
*/
|
|
9
|
+
export declare function isArray(value: unknown): value is unknown[];
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type Last<T extends readonly unknown[], F = undefined> = T extends readonly [...T[number][], infer V] ? V : T[number] | (T extends readonly [T[number], ...T[number][]] ? never : F);
|
|
2
|
+
/**
|
|
3
|
+
* Returns the last item of a list or a fallback if the list is empty.
|
|
4
|
+
*
|
|
5
|
+
* The time complexity for this is `O(1)`.
|
|
6
|
+
*
|
|
7
|
+
* @template T
|
|
8
|
+
* @template {readonly T[]} [L=T[]]
|
|
9
|
+
* @template [F=undefined]
|
|
10
|
+
* @param list The array to get the last item from
|
|
11
|
+
* @param [fallback] The value to fall back to if the list is empty
|
|
12
|
+
* @returns {First<T, F>} The last item of the list or a fallback
|
|
13
|
+
*/
|
|
14
|
+
export declare function last<const T extends readonly unknown[], const F = undefined>(list: T, fallback?: F): Last<T, F>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { UtilError } from
|
|
2
|
-
import { Computable } from
|
|
1
|
+
import { UtilError } from "~/error";
|
|
2
|
+
import { type Computable } from "~/misc/utils/compute";
|
|
3
3
|
export declare class ListUtilError extends UtilError {
|
|
4
4
|
readonly util = "list";
|
|
5
5
|
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Promisable } from "~/types";
|
|
2
|
+
/**
|
|
3
|
+
* Returns a new list of items based on mapped values of a given list.
|
|
4
|
+
*
|
|
5
|
+
* If the callback returns a promise for any item, a promise is returned that resolves once all of them have.
|
|
6
|
+
*
|
|
7
|
+
* The time complexity for this is `O(n)` where `n` is the amount of items in the given list.
|
|
8
|
+
*
|
|
9
|
+
* @template T
|
|
10
|
+
* @template R
|
|
11
|
+
* @param {readonly T[]} list The array to map values from
|
|
12
|
+
* @param {(item: T, index: number, list: readonly T[]) => R} callback A mapper to convert the list items
|
|
13
|
+
* @returns {Promise<Awaited<R>[]>} A new array with mapped items from the given list, or a promise of it if the callback can return a promise
|
|
14
|
+
*/
|
|
15
|
+
export declare function map<const T, const R>(list: readonly T[], callback: (item: T, index: number, list: readonly T[]) => Promise<R>): Promise<Awaited<R>[]>;
|
|
16
|
+
/**
|
|
17
|
+
* Returns a new list of items based on mapped values of a given list.
|
|
18
|
+
*
|
|
19
|
+
* If the callback returns a promise for any item, a promise is returned that resolves once all of them have.
|
|
20
|
+
*
|
|
21
|
+
* The time complexity for this is `O(n)` where `n` is the amount of items in the given list.
|
|
22
|
+
*
|
|
23
|
+
* @template T
|
|
24
|
+
* @template R
|
|
25
|
+
* @param {readonly T[]} list The array to map values from
|
|
26
|
+
* @param {(item: T, index: number, list: readonly T[]) => R} callback A mapper to convert the list items
|
|
27
|
+
* @returns {[Extract<R, Promise<unknown>>] extends [never] ? R[] : Promisable<Awaited<R>[]>} A new array with mapped items from the given list, or a promise of it if the callback can return a promise
|
|
28
|
+
*/
|
|
29
|
+
export declare function map<const T, const R>(list: readonly T[], callback: (item: T, index: number, list: readonly T[]) => R): [Extract<R, Promise<unknown>>] extends [never] ? R[] : Promisable<Awaited<R>[]>;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export interface MaxOptions {
|
|
2
|
+
/**
|
|
3
|
+
* The direction the list should be checked in.
|
|
4
|
+
*
|
|
5
|
+
* @default "ASCENDING"
|
|
6
|
+
*/
|
|
7
|
+
direction?: "ASCENDING" | "DESCENDING";
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Returns the largest item from a list.
|
|
11
|
+
*
|
|
12
|
+
* If no mapper is specified, non-numeric values within a list aren't compared.
|
|
13
|
+
*
|
|
14
|
+
* The time complexity for this is `O(n)` where `n` is the amount of items in the list.
|
|
15
|
+
*
|
|
16
|
+
* @template T
|
|
17
|
+
* @param {T[]} list The list to
|
|
18
|
+
* @param {(item: T) => number} mapper A callback to convert an item to a number
|
|
19
|
+
* @param {MaxOptions} options Options to define how the largest number is decided
|
|
20
|
+
* @returns {T | undefined} The smallest item from a list
|
|
21
|
+
*/
|
|
22
|
+
export declare function max<const T>(list: [T, ...T[]], mapper: (item: T) => number, options?: MaxOptions): T;
|
|
23
|
+
export declare function max<const T extends number>(list: [T, ...T[]], mapper?: (item: T) => number, options?: MaxOptions): T;
|
|
24
|
+
export declare function max<const T>(list: T[], mapper?: (item: T) => number, options?: MaxOptions): T | undefined;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export interface MinOptions {
|
|
2
|
+
/**
|
|
3
|
+
* The direction the list should be checked in.
|
|
4
|
+
*
|
|
5
|
+
* @default "ASCENDING"
|
|
6
|
+
*/
|
|
7
|
+
direction?: "ASCENDING" | "DESCENDING";
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Returns the smallest item from a list.
|
|
11
|
+
*
|
|
12
|
+
* If no mapper is specified, non-numeric values within a list aren't compared.
|
|
13
|
+
*
|
|
14
|
+
* The time complexity for this is `O(n)` where `n` is the amount of items in the list.
|
|
15
|
+
*
|
|
16
|
+
* @template T
|
|
17
|
+
* @param {T[]} list The list to
|
|
18
|
+
* @param {(item: T) => number} mapper A callback to convert an item to a number
|
|
19
|
+
* @param {MinOptions} options Options to define how the smallest number is decided
|
|
20
|
+
* @returns {T | undefined} The smallest item from a list
|
|
21
|
+
*/
|
|
22
|
+
export declare function min<const T>(list: [T, ...T[]], mapper: (item: T) => number, options?: MinOptions): T;
|
|
23
|
+
export declare function min<const T extends number>(list: [T, ...T[]], mapper?: (item: T) => number, options?: MinOptions): T;
|
|
24
|
+
export declare function min<const T>(list: T[], mapper?: (item: T) => number, options?: MinOptions): T | undefined;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { UtilError } from "~/error";
|
|
2
|
+
import type { PositiveInteger } from "~/number";
|
|
3
|
+
export declare class OverflowUtilError extends UtilError {
|
|
4
|
+
readonly util = "overflow";
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Returns a tuple of 2 arrays that are constructed from a single list based on the given clamp length.
|
|
8
|
+
* The first item in the tuple is all the items in the list that pass the specified condition,
|
|
9
|
+
* the second item in the tuple contains the rest of the list items.
|
|
10
|
+
*
|
|
11
|
+
* The time complexity for this is `O(n)` where `n` is the amount of items in the list.
|
|
12
|
+
*
|
|
13
|
+
* @template T
|
|
14
|
+
* @template {number} L
|
|
15
|
+
* @param {readonly T[]} list The list to seperate into a clamped and rest list
|
|
16
|
+
* @param {PositiveInteger<L>} clampLength The length of the clamped list
|
|
17
|
+
* @returns {[T[], T[]]} A tuple containing a clamped and rest list based off the given list and length
|
|
18
|
+
* @throws {OverflowUtilError} If the clamp length is less than 1
|
|
19
|
+
*/
|
|
20
|
+
export declare function overflow<const T, const L extends number = number>(list: readonly T[], clampLength: PositiveInteger<L>): [T[], T[]];
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export type ReduceCallback<T, Acc> = (accumulator: Acc, value: T, index: number, list: readonly T[]) => Acc;
|
|
2
|
+
export interface ReduceOptions<Acc = unknown, R = Acc> {
|
|
3
|
+
/**
|
|
4
|
+
* The direction the list should be checked in..
|
|
5
|
+
*
|
|
6
|
+
* @default "ASCENDING"
|
|
7
|
+
*/
|
|
8
|
+
direction?: "ASCENDING" | "DESCENDING";
|
|
9
|
+
/**
|
|
10
|
+
* Transforms the accumulator into a final result.
|
|
11
|
+
*
|
|
12
|
+
* @param accumulator
|
|
13
|
+
* @returns {R}
|
|
14
|
+
*/
|
|
15
|
+
result?: (accumulator: Acc) => R;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Reduces all items of a list down to an accumulated result.
|
|
19
|
+
*
|
|
20
|
+
* The time complexity for this is `O(n)` where `n` is the amount of items in the list.
|
|
21
|
+
*
|
|
22
|
+
* @template T
|
|
23
|
+
* @template Acc
|
|
24
|
+
* @param {readonly T[]} list The array to reduce items from
|
|
25
|
+
* @param {Acc} initial The initial value of the accumulator
|
|
26
|
+
* @param {ReduceCallback<T, Acc>} callback A callback that passed the current item and returns a new accumulator
|
|
27
|
+
* @returns {Acc} The accumulated result of the given list
|
|
28
|
+
*/
|
|
29
|
+
export declare function reduce<const T, Acc>(list: T[], initial: Acc, callback: ReduceCallback<T, Acc>): Acc;
|
|
30
|
+
/**
|
|
31
|
+
* Reduces all items of a list down to an accumulated result.
|
|
32
|
+
*
|
|
33
|
+
* The time complexity for this is `O(n)` where `n` is the amount of items in the list.
|
|
34
|
+
*
|
|
35
|
+
* @template T
|
|
36
|
+
* @template Acc
|
|
37
|
+
* @template [R=Acc]
|
|
38
|
+
* @param {readonly T[]} list The array to reduce items from
|
|
39
|
+
* @param {Acc} initial The initial value of the accumulator
|
|
40
|
+
* @param {ReduceCallback<T, Acc>} callback A callback that passed the current item and returns a new accumulator
|
|
41
|
+
* @param {ReduceOptions<Acc, R>} options Options to define how the list should be reduced (see {@link ReduceOptions} for more details)
|
|
42
|
+
* @returns {R} The accumulated result of the given list
|
|
43
|
+
*/
|
|
44
|
+
export declare function reduce<const T, Acc, const R = Acc>(list: readonly T[], initial: Acc, callback: ReduceCallback<T, Acc>, options?: ReduceOptions<Acc, R>): R;
|
|
@@ -1,14 +1,131 @@
|
|
|
1
|
+
import { UtilError } from "~/error";
|
|
2
|
+
export interface SelectDiscardAction {
|
|
3
|
+
type: "DISCARD";
|
|
4
|
+
}
|
|
5
|
+
export interface SelectKeepAction {
|
|
6
|
+
type: "KEEP";
|
|
7
|
+
}
|
|
8
|
+
export interface SelectFilterAction {
|
|
9
|
+
type: "FILTER";
|
|
10
|
+
keep: boolean;
|
|
11
|
+
}
|
|
12
|
+
export interface SelectMapAction<R> {
|
|
13
|
+
type: "MAP";
|
|
14
|
+
value: R;
|
|
15
|
+
}
|
|
16
|
+
export interface SelectFlatAction<R> {
|
|
17
|
+
type: "FLAT";
|
|
18
|
+
list: R[];
|
|
19
|
+
}
|
|
20
|
+
export interface SelectSpreadAction<R> {
|
|
21
|
+
type: "SPREAD";
|
|
22
|
+
values: R[];
|
|
23
|
+
}
|
|
24
|
+
export type SelectAction<R> = SelectDiscardAction | SelectKeepAction | SelectFilterAction | SelectMapAction<R> | SelectSpreadAction<R> | SelectFlatAction<R>;
|
|
25
|
+
export interface SelectContext<T, R = T> {
|
|
26
|
+
/**
|
|
27
|
+
* The current item in the given list.
|
|
28
|
+
*/
|
|
29
|
+
item: T;
|
|
30
|
+
/**
|
|
31
|
+
* The index of the current item of the given list.
|
|
32
|
+
*/
|
|
33
|
+
index: number;
|
|
34
|
+
/**
|
|
35
|
+
* The given list.
|
|
36
|
+
*/
|
|
37
|
+
list: readonly T[];
|
|
38
|
+
/**
|
|
39
|
+
* Discards the current item from the resulting list.
|
|
40
|
+
*
|
|
41
|
+
* @returns {SelectDiscardAction} An action that discards the current item.
|
|
42
|
+
*/
|
|
43
|
+
discard(): SelectDiscardAction;
|
|
44
|
+
/**
|
|
45
|
+
* Keeps the current item in the resulting list.
|
|
46
|
+
*
|
|
47
|
+
* @returns {SelectKeepAction} An action that keeps the current item.
|
|
48
|
+
*/
|
|
49
|
+
keep(): SelectKeepAction;
|
|
50
|
+
/**
|
|
51
|
+
* Filters the current item in the resulting list based on the given boolean.
|
|
52
|
+
*
|
|
53
|
+
* @param {boolean} keep Whether the current item should be kept
|
|
54
|
+
* @returns {SelectFilterAction} An action that keeps or discards the current item.
|
|
55
|
+
*/
|
|
56
|
+
filter(keep: boolean): SelectFilterAction;
|
|
57
|
+
/**
|
|
58
|
+
* Maps the current item to the given value in the resulting list.
|
|
59
|
+
*
|
|
60
|
+
* @param {R} value The value to add in place of the current item
|
|
61
|
+
* @returns {SelectMapAction<R>} An action that maps the current item to the given value
|
|
62
|
+
*/
|
|
63
|
+
map(value: R): SelectMapAction<R>;
|
|
64
|
+
/**
|
|
65
|
+
* Flats every item of the given list into the resulting list in place of the current item.
|
|
66
|
+
*
|
|
67
|
+
* @param {R[]} list The values to add in place of the current item
|
|
68
|
+
* @returns {SelectFlatAction<R>} An action that flattens the given values into the resulting list
|
|
69
|
+
*/
|
|
70
|
+
flat(list: R[]): SelectFlatAction<R>;
|
|
71
|
+
/**
|
|
72
|
+
* Spreads every given item into the resulting list in place of the current item.
|
|
73
|
+
*
|
|
74
|
+
* @param {...R[]} values The values to add in place of the current item
|
|
75
|
+
* @returns {SelectSpreadAction<R>} An action that spreads the given values into the resulting list
|
|
76
|
+
*/
|
|
77
|
+
spread(...values: R[]): SelectSpreadAction<R>;
|
|
78
|
+
}
|
|
79
|
+
export declare class SelectUtilError extends UtilError {
|
|
80
|
+
readonly util = "select";
|
|
81
|
+
}
|
|
1
82
|
/**
|
|
2
|
-
* Returns a new list
|
|
83
|
+
* Returns a new list built by deciding what should end up in the result per item.
|
|
3
84
|
*
|
|
4
|
-
* The time complexity for this is `O(n)` where `n` is the amount of items in the
|
|
85
|
+
* The time complexity for this is `O(n)` where `n` is the amount of items in the list.
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* select(
|
|
89
|
+
* [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
|
|
90
|
+
* ({ item, keep, discard, filter }) => {
|
|
91
|
+
* // Keep all numbers less than 3
|
|
92
|
+
* if (item < 3) {
|
|
93
|
+
* return keep();
|
|
94
|
+
* }
|
|
95
|
+
*
|
|
96
|
+
* // Removes all numbers greater than 8
|
|
97
|
+
* if (item > 8) {
|
|
98
|
+
* return discard();
|
|
99
|
+
* }
|
|
100
|
+
*
|
|
101
|
+
* // Filters all even numbers
|
|
102
|
+
* return filter(item % 2 === 0);
|
|
103
|
+
* },
|
|
104
|
+
* );
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* select<number | string[] | null, string>(
|
|
108
|
+
* [1, ["2", "3", "4"], null, ["7", "8", "9"], 10],
|
|
109
|
+
* ({ item, map, flat, spread }) => {
|
|
110
|
+
* if (typeof item === "number") {
|
|
111
|
+
* return map(item.toString());
|
|
112
|
+
* }
|
|
113
|
+
*
|
|
114
|
+
* if (Array.isArray(item)) {
|
|
115
|
+
* return flat(item);
|
|
116
|
+
* }
|
|
117
|
+
*
|
|
118
|
+
* return spread("5", "6");
|
|
119
|
+
* },
|
|
120
|
+
* );
|
|
5
121
|
*
|
|
6
122
|
* @template T
|
|
7
|
-
* @template R
|
|
8
|
-
* @param {
|
|
9
|
-
* @param {(
|
|
10
|
-
* @
|
|
11
|
-
* @
|
|
123
|
+
* @template [R=T]
|
|
124
|
+
* @param {readonly T[]} list The list to select items from
|
|
125
|
+
* @param {(context: SelectContext<T, R>) => SelectAction<R>} callback A callback that returns an action describing what to do with the current item
|
|
126
|
+
* @returns {R[]} A new list with all kept, mapped and flattened values
|
|
127
|
+
* @throws {SelectUtilError} If the callback returns an invalid action
|
|
12
128
|
*/
|
|
13
|
-
export declare function select<
|
|
14
|
-
|
|
129
|
+
export declare function select<T, R = T>(list: readonly T[], callback: (context: [T] extends [R] ? SelectContext<T, R> : Omit<SelectContext<T, R>, "keep" | "filter">) => Exclude<SelectAction<R>, {
|
|
130
|
+
type: [T] extends [R] ? never : "KEEP" | "FILTER";
|
|
131
|
+
}>): R[];
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Key } from
|
|
1
|
+
import type { Key } from "~/types";
|
|
2
2
|
/**
|
|
3
3
|
* @typedef {Object} ToggleOptions
|
|
4
4
|
* @template T
|
|
@@ -23,7 +23,7 @@ export interface ToggleOptions<T> {
|
|
|
23
23
|
/**
|
|
24
24
|
* Toggles a value from an array and returns the new array with the value either removed or added.
|
|
25
25
|
*
|
|
26
|
-
* The time complexity for this is `O(
|
|
26
|
+
* The time complexity for this is `O(2n)` where `n` is the amount of items in the list.
|
|
27
27
|
*
|
|
28
28
|
* @template T
|
|
29
29
|
* @param {T[]} list The list to toggle the item from
|
package/dist/callback/index.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
5
|
-
export * from
|
|
1
|
+
export * from "./utils/chain";
|
|
2
|
+
export * from "./utils/compose";
|
|
3
|
+
export * from "./utils/defer";
|
|
4
|
+
export * from "./utils/noop";
|
|
5
|
+
export * from "./utils/pipe";
|
|
6
|
+
export * from "./utils/preset";
|
|
7
|
+
export * from "./utils/retry";
|
|
8
|
+
export * from "./utils/task";
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pipes a value into a callback and returns its result.
|
|
3
|
+
*
|
|
4
|
+
* The time complexity for this is `O(1)`.
|
|
5
|
+
*
|
|
6
|
+
* @template {?} T
|
|
7
|
+
* @template {?} R
|
|
8
|
+
* @param {T} value The initial value to be passed into the callback
|
|
9
|
+
* @param {(value: T) => R} callback A function that accepts `value` and computes a result
|
|
10
|
+
* @returns {R} The result of the `callback` function
|
|
11
|
+
*/
|
|
12
|
+
export declare function pipe<const T, const R>(value: T, callback: (value: T) => R): R;
|
|
@@ -1,13 +1,15 @@
|
|
|
1
|
+
import { type Computable } from "~/misc";
|
|
1
2
|
/**
|
|
2
|
-
* Presets the parameters of a callback with a callback that computes its arguments.
|
|
3
|
+
* Presets the parameters of a callback with arguments or a callback that computes its arguments.
|
|
3
4
|
*
|
|
4
5
|
* The time complexity for this is `O(1)`.
|
|
5
6
|
*
|
|
6
7
|
* @template {(...args: never[]) => unknown} T
|
|
7
8
|
* @template {unknown[]} [Args=never[]]
|
|
8
|
-
* @template {Parameters<T>} [Params=Parameters<T>]
|
|
9
9
|
* @param {T} callback The callback to preset the parameters for
|
|
10
|
-
* @param {Computable<
|
|
10
|
+
* @param {Computable<Parameters<T>, Args>} parameters The parameters or callback that computes parameters to preset
|
|
11
11
|
* @returns {(...args: Args) => ReturnType<T>} A callback that runs the input `callback` with the preset parameters
|
|
12
12
|
*/
|
|
13
|
-
export declare function preset<const
|
|
13
|
+
export declare function preset<const Args extends unknown[] = never[], const R = unknown>(callback: (...args: Args) => R, parameters: Args): () => R;
|
|
14
|
+
export declare function preset<const Args extends unknown[] = never[], const R = unknown, const P extends unknown[] = never[]>(callback: (...args: Args) => R, parameters: (...args: P) => Args): (...args: P) => R;
|
|
15
|
+
export declare function preset<const Args extends unknown[] = never[], const R = unknown, const P extends unknown[] = never[]>(callback: (...args: Args) => R, parameters: Computable<Args, P>): (...args: P) => R;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Promisable } from "~/types";
|
|
2
|
+
/**
|
|
3
|
+
* Executes the given callback in asynchronous context.
|
|
4
|
+
*
|
|
5
|
+
* If no callback is given, it yields execution to the microtask queue.
|
|
6
|
+
*
|
|
7
|
+
* The time complexity for this is `O(1)`.
|
|
8
|
+
*
|
|
9
|
+
* @param {() => Promisable<void>} [callback] An optional callback to execute
|
|
10
|
+
* @returns {Promise<void>} A Promise that resolves after the callback has completed or execution has yielded to the microtask queue
|
|
11
|
+
*/
|
|
12
|
+
export declare function task(callback?: () => Promisable<void>): Promise<void>;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
5
|
-
export * from
|
|
6
|
-
export * from
|
|
7
|
-
export * from
|
|
8
|
-
export
|
|
1
|
+
export * from "./array";
|
|
2
|
+
export * from "./callback";
|
|
3
|
+
export * from "./error";
|
|
4
|
+
export * from "./misc";
|
|
5
|
+
export * from "./number";
|
|
6
|
+
export * from "./object";
|
|
7
|
+
export * from "./random";
|
|
8
|
+
export * from "./string";
|
|
9
|
+
export type * from "./types";
|