@catstral/levy-utils 1.0.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 +1370 -59
- package/dist/array/index.d.ts +20 -7
- 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/sum/index.d.ts +13 -0
- package/dist/array/utils/toggle/index.d.ts +2 -2
- package/dist/callback/index.d.ts +8 -3
- package/dist/callback/utils/compose/index.d.ts +43 -0
- 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 +15 -0
- 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 -5
- 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 +22 -0
- 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 +47 -0
- 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 +13 -0
- 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 -5
- package/dist/object/utils/extend/index.d.ts +21 -0
- 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 +8 -13
- package/dist/levyUtils.cjs +0 -5
- package/dist/levyUtils.js +0 -1198
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { Key } from "~/types";
|
|
2
|
+
export type ReadOnlyTarget = Record<Key, unknown> | readonly unknown[] | unknown[] | Map<unknown, unknown> | Set<unknown>;
|
|
3
|
+
export type ReadOnly<T> = T extends Record<Key, unknown> ? Readonly<T> : T extends readonly (infer U)[] ? readonly U[] : T extends ReadonlyMap<infer K, infer V> ? ReadonlyMap<K, V> : T extends ReadonlySet<infer U> ? ReadonlySet<U> : T;
|
|
4
|
+
export type DeepReadOnly<T> = T extends Record<Key, unknown> ? {
|
|
5
|
+
readonly [K in keyof T]: DeepReadOnly<T[K]>;
|
|
6
|
+
} : T extends readonly (infer U)[] ? readonly DeepReadOnly<U>[] : T extends ReadonlyMap<infer K, infer V> ? ReadonlyMap<DeepReadOnly<K>, DeepReadOnly<V>> : T extends ReadonlySet<infer U> ? ReadonlySet<DeepReadOnly<U>> : T;
|
|
7
|
+
/**
|
|
8
|
+
* Checks if the given value is read-only.
|
|
9
|
+
*
|
|
10
|
+
* Whether an object's existing property attributes and values cannot be modified, and new properties cannot be added.
|
|
11
|
+
*
|
|
12
|
+
* The time complexity for this is `O(1)`.
|
|
13
|
+
*
|
|
14
|
+
* @template {Record<Key, unknown> | unknown[] | readonly unknown[]} T
|
|
15
|
+
* @param {T} value The value to check if it is read-only
|
|
16
|
+
* @returns {value is Readonly<T>} A boolean that signals if the given value is read-only
|
|
17
|
+
*/
|
|
18
|
+
export declare function isReadOnly<const T extends Record<Key, unknown> | unknown[] | readonly unknown[]>(value: T): value is Readonly<T>;
|
|
19
|
+
/**
|
|
20
|
+
* Returns a shallow read-only copy of an object, array, map, or set.
|
|
21
|
+
*
|
|
22
|
+
* Only `value` itself is copied and frozen — nested objects/arrays/maps/sets keep the exact same
|
|
23
|
+
* references as `value` and remain mutable.
|
|
24
|
+
*
|
|
25
|
+
* The time complexity for this is `O(n)` where `n` is the amount of top-level entries in `value`.
|
|
26
|
+
*
|
|
27
|
+
* @template {ReadOnlyTarget} T
|
|
28
|
+
* @param {T} value The object, array, map, or set to return a frozen shallow copy of
|
|
29
|
+
* @param {false} [deepFreeze] Whether nested objects, arrays, maps, and sets should also be cloned and frozen
|
|
30
|
+
* @returns {Readonly<T>} A frozen shallow copy of `value`
|
|
31
|
+
*/
|
|
32
|
+
export declare function readOnly<const T extends ReadOnlyTarget>(value: T, deepFreeze?: false): Readonly<T>;
|
|
33
|
+
/**
|
|
34
|
+
* Returns a deep read-only copy of an object, array, map, or set.
|
|
35
|
+
*
|
|
36
|
+
* Every nested object, array, map, and set is recursively cloned and frozen, so nothing in the
|
|
37
|
+
* result is shared with `value`.
|
|
38
|
+
*
|
|
39
|
+
* The time complexity for this is `O(n)` where `n` is the total amount of nested entries in `value`.
|
|
40
|
+
*
|
|
41
|
+
* @template {ReadOnlyTarget} T
|
|
42
|
+
* @param {T} value The object, array, map, or set to return a frozen deep copy of
|
|
43
|
+
* @param {true} deepFreeze Whether nested objects, arrays, maps, and sets should also be cloned and frozen
|
|
44
|
+
* @returns {DeepReadOnly<T>} A deeply frozen copy of `value`
|
|
45
|
+
*/
|
|
46
|
+
export declare function readOnly<const T extends ReadOnlyTarget>(value: T, deepFreeze: true): DeepReadOnly<T>;
|
|
47
|
+
export declare function readOnly<const T extends ReadOnlyTarget>(value: T, deepFreeze: boolean): Readonly<T> | DeepReadOnly<T>;
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* The time complexity for this is `O(1)`.
|
|
5
5
|
*
|
|
6
|
-
* @param {number}
|
|
6
|
+
* @param {number} delayMs The delay to wait to resolve the returned promise
|
|
7
7
|
* @returns {Promise<void>} A Promise that resolved after the specified delay has elapsed
|
|
8
8
|
*/
|
|
9
|
-
export declare function sleep(
|
|
9
|
+
export declare function sleep(delayMs: number): Promise<void>;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { UtilError } from "~/error";
|
|
2
|
+
import type { Computable } from "../compute";
|
|
3
|
+
export declare class TimeoutUtilError extends UtilError {
|
|
4
|
+
readonly util = "timeout";
|
|
5
|
+
}
|
|
6
|
+
export declare class TimeoutError extends Error {
|
|
7
|
+
constructor(delayMs: number);
|
|
8
|
+
}
|
|
9
|
+
export interface TimeoutOptions {
|
|
10
|
+
/**
|
|
11
|
+
* A callback that runs when the delay has elapsed.
|
|
12
|
+
*
|
|
13
|
+
* Throwing a value inside this callback rejects the promise with that value instead of a {@link TimeoutError}.
|
|
14
|
+
*
|
|
15
|
+
* @param {number} delayMs
|
|
16
|
+
* @returns {void}
|
|
17
|
+
*/
|
|
18
|
+
onTimeout?: (delayMs: number) => void;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Wraps the given promise into one that rejects after a given delay has elapsed.
|
|
22
|
+
*
|
|
23
|
+
* The time complexity for this is `O(1)`.
|
|
24
|
+
*
|
|
25
|
+
* @template T
|
|
26
|
+
* @param {Computable<Promise<T>, [signal: AbortSignal]>} value The promise or callback that returns a promise to handle
|
|
27
|
+
* @param {number} delayMs The delay in milliseconds to wait before rejecting the promise
|
|
28
|
+
* @param {TimeoutOptions<T>} [options] The options given to the timeout
|
|
29
|
+
* @returns {Promise<T>} A promise that resolves with the given promise's result and rejects after the specified delay has elapsed
|
|
30
|
+
* @throws {TimeoutUtilError} If the given delay is smaller than 0
|
|
31
|
+
* @throws {TimeoutError} If the given delay has elapsed
|
|
32
|
+
*/
|
|
33
|
+
export declare function timeout<const T>(value: Computable<Promise<T>, [signal: AbortSignal]>, delayMs: number, options?: TimeoutOptions): Promise<T>;
|
|
34
|
+
/**
|
|
35
|
+
* Checks if the given value is a timeout error.
|
|
36
|
+
*
|
|
37
|
+
* @param {?} value The value to check if it is a timeout error.
|
|
38
|
+
* @returns {boolean} A boolean that signals if the given value is an instance of {@link TimeoutError}
|
|
39
|
+
*/
|
|
40
|
+
export declare function isTimeoutError(value: unknown): value is TimeoutError;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Key } from "~/types";
|
|
2
|
+
export type ValuesTarget = ArrayLike<unknown> | Record<Key, unknown> | Map<unknown, unknown> | Set<unknown> | Iterable<unknown>;
|
|
3
|
+
export type Values<T extends ValuesTarget> = T extends ArrayLike<infer V> ? V[] : T extends Record<Key, infer V> ? V[] : T extends Map<unknown, infer V> ? V[] : T extends Set<infer V> ? V[] : T extends Iterable<infer V> ? V[] : never;
|
|
4
|
+
/**
|
|
5
|
+
* Returns an array of values based on the given target.
|
|
6
|
+
*
|
|
7
|
+
* The time complexity for this is `O(n)` where `n` is the amount of values in the given `target`.
|
|
8
|
+
*
|
|
9
|
+
* @template {ValuesTarget} T
|
|
10
|
+
* @param target The given value to extract values from
|
|
11
|
+
* @returns {Values<T>} An array of values associated with `target`
|
|
12
|
+
*/
|
|
13
|
+
export declare function values<const T extends ValuesTarget>(target: T): Values<T>;
|
package/dist/number/index.d.ts
CHANGED
|
@@ -1,2 +1,13 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
1
|
+
export * from "./math";
|
|
2
|
+
export * from "./types";
|
|
3
|
+
export * from "./utils/clamp";
|
|
4
|
+
export * from "./utils/isDecimal";
|
|
5
|
+
export * from "./utils/isEven";
|
|
6
|
+
export * from "./utils/isInteger";
|
|
7
|
+
export * from "./utils/isOdd";
|
|
8
|
+
export * from "./utils/negative";
|
|
9
|
+
export * from "./utils/positive";
|
|
10
|
+
export * from "./utils/sign";
|
|
11
|
+
export * from "./utils/toFloat";
|
|
12
|
+
export * from "./utils/toInt";
|
|
13
|
+
export * from "./utils/trunc";
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if the given value is a prime number.
|
|
3
|
+
*
|
|
4
|
+
* The time complexity for this is `O(√n)` where `n` is the given number.
|
|
5
|
+
*
|
|
6
|
+
* @param {number} value The value to check if it is a prime number
|
|
7
|
+
* @returns {boolean} A boolean that signals if the given number is a prime number
|
|
8
|
+
*/
|
|
9
|
+
export declare function isPrime(value: number): boolean;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the modulo of a dividend and a divisor.
|
|
3
|
+
*
|
|
4
|
+
* The time complexity for this is `O(1)`.
|
|
5
|
+
*
|
|
6
|
+
* @param {number} dividend The number to be divided
|
|
7
|
+
* @param {number} divisor The number to divide by
|
|
8
|
+
* @returns {number} The modulo of the division
|
|
9
|
+
*/
|
|
10
|
+
export declare function mod(dividend: number, divisor: number): number;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export type Zero = 0 | -0;
|
|
2
|
+
export type Integer<T extends number> = `${T}` extends `${infer V extends number}.${number}` ? V : T;
|
|
3
|
+
export type IsInteger<T extends number> = number extends T ? boolean : T extends Integer<T> ? true : false;
|
|
4
|
+
export type Positive<T extends number> = T extends Zero ? 0 : `${T}` extends `-${infer V extends number}` ? V : T;
|
|
5
|
+
export type IsPositive<T extends number> = number extends T ? boolean : T extends Positive<T> ? true : false;
|
|
6
|
+
export type Negative<T extends number> = T extends Zero ? -0 : `-${T}` extends `${infer N extends number}` ? N : T;
|
|
7
|
+
export type IsNegative<T extends number> = number extends T ? boolean : T extends Negative<T> ? true : false;
|
|
8
|
+
export type PositiveInteger<T extends number> = Integer<Positive<T>>;
|
|
9
|
+
export type NegativeInteger<T extends number> = Integer<Negative<T>>;
|
|
10
|
+
export type NonZero<T extends number> = Exclude<T, Zero>;
|
|
11
|
+
export type IsNonZero<T extends number> = number extends T ? boolean : T extends NonZero<T> ? true : false;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Clamps a number between a minimum and maximum value.
|
|
3
|
+
*
|
|
4
|
+
* Returns `NaN` if the minimum and maximum contradict each other.
|
|
5
|
+
*
|
|
6
|
+
* The time complexity for this is `O(1)`.
|
|
7
|
+
*
|
|
8
|
+
* @param {number} min The minimum value
|
|
9
|
+
* @param {number} value The value to be clamped between `min` and `max`
|
|
10
|
+
* @param {number} max The maximum value
|
|
11
|
+
* @returns {number} The clamped number
|
|
12
|
+
*/
|
|
13
|
+
export declare function clamp(min: number, value: number, max: number): number;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export type IsDecimal<T extends number> = number extends T ? boolean : `${T}` extends `${number}.${number}` ? true : false;
|
|
2
|
+
/**
|
|
3
|
+
* Checks if the given value is a number with decimals.
|
|
4
|
+
*
|
|
5
|
+
* The time complexity for this is `O(1)`.
|
|
6
|
+
*
|
|
7
|
+
* @template {number} T
|
|
8
|
+
* @param {T} value The number to check if it has decimals
|
|
9
|
+
* @returns {IsDecimal<T>} A boolean that signals if the given number has decimals
|
|
10
|
+
*/
|
|
11
|
+
export declare function isDecimal<const T extends number>(value: T): IsDecimal<T>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if the given value is an even number.
|
|
3
|
+
*
|
|
4
|
+
* The time complexity for this is `O(1)`.
|
|
5
|
+
*
|
|
6
|
+
* @param {number} value The value to check if it is an even number
|
|
7
|
+
* @returns {boolean} A boolean that signals if the given number is even
|
|
8
|
+
*/
|
|
9
|
+
export declare function isEven(value: number): boolean;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { IsInteger } from "~/number";
|
|
2
|
+
/**
|
|
3
|
+
* Checks if the given value is an integer.
|
|
4
|
+
*
|
|
5
|
+
* The time complexity for this is `O(1)`.
|
|
6
|
+
*
|
|
7
|
+
* @template {number} T
|
|
8
|
+
* @param {T} value The number to check if it is an integer
|
|
9
|
+
* @returns {IsInteger<T>} A boolean that signals if the given number is an integer
|
|
10
|
+
*/
|
|
11
|
+
export declare function isInteger<const T extends number>(value: T): IsInteger<T>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if the given value is an odd number.
|
|
3
|
+
*
|
|
4
|
+
* The time complexity for this is `O(1)`.
|
|
5
|
+
*
|
|
6
|
+
* @param {number} value The value to check if it is an odd number
|
|
7
|
+
* @returns {boolean} A boolean that signals if the given number is odd
|
|
8
|
+
*/
|
|
9
|
+
export declare function isOdd(value: number): boolean;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { IsNegative, Negative } from "~/number";
|
|
2
|
+
/**
|
|
3
|
+
* Converts a number into a negative number.
|
|
4
|
+
*
|
|
5
|
+
* The time complexity for this is `O(1)`.
|
|
6
|
+
*
|
|
7
|
+
* @template {number} T
|
|
8
|
+
* @param {Negative<T>} value The value to convert into a negative number
|
|
9
|
+
* @returns {Negative<T>} A negative number
|
|
10
|
+
*/
|
|
11
|
+
export declare function negative<const T extends number>(value: T): Negative<T>;
|
|
12
|
+
/**
|
|
13
|
+
* Checks if the given value is a negative number.
|
|
14
|
+
*
|
|
15
|
+
* The time complexity for this is `O(1)`.
|
|
16
|
+
*
|
|
17
|
+
* @template {number} T
|
|
18
|
+
* @param {T} value The value to check if it is a negative number
|
|
19
|
+
* @returns {IsNegative<T>} A boolean that signals if the given number is negative
|
|
20
|
+
*/
|
|
21
|
+
export declare function isNegative<const T extends number>(value: T): IsNegative<T>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { IsPositive, Positive } from "~/number";
|
|
2
|
+
/**
|
|
3
|
+
* Converts a number into a positive number.
|
|
4
|
+
*
|
|
5
|
+
* The time complexity for this is `O(1)`.
|
|
6
|
+
*
|
|
7
|
+
* @template {number} T
|
|
8
|
+
* @param {Positive<T>} value The value to convert into a positive number
|
|
9
|
+
* @returns {Positive<T>} A positive number
|
|
10
|
+
*/
|
|
11
|
+
export declare function positive<const T extends number>(value: T): Positive<T>;
|
|
12
|
+
/**
|
|
13
|
+
* Checks if the given value is a positive number.
|
|
14
|
+
*
|
|
15
|
+
* The time complexity for this is `O(1)`.
|
|
16
|
+
*
|
|
17
|
+
* @template {number} T
|
|
18
|
+
* @param {T} value The value to check if it is a positive number
|
|
19
|
+
* @returns {IsPositive<T>} A boolean that signals if the given number is positive
|
|
20
|
+
*/
|
|
21
|
+
export declare function isPositive<const T extends number>(value: T): IsPositive<T>;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { IsNegative, Zero } from "~/number";
|
|
2
|
+
export type Sign<T extends number> = number extends T ? -1 | 0 | 1 : T extends Zero ? 0 : IsNegative<T> extends true ? -1 : 1;
|
|
3
|
+
/**
|
|
4
|
+
* Returns the sign of the given number.
|
|
5
|
+
*
|
|
6
|
+
* `NaN` is converted into zero.
|
|
7
|
+
*
|
|
8
|
+
* The time complexity for this is `O(1)`.
|
|
9
|
+
*
|
|
10
|
+
* @template {number} T
|
|
11
|
+
* @param {T} value The number to return the sign of
|
|
12
|
+
* @returns {Sign<T>} A number indicating whether the given number is positive, negative or zero
|
|
13
|
+
*/
|
|
14
|
+
export declare function sign<const T extends number>(value: T): Sign<T>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { PositiveInteger } from "~/number/types";
|
|
2
|
+
/**
|
|
3
|
+
* Truncates a number to the specified number of fraction digits.
|
|
4
|
+
*
|
|
5
|
+
* If no fraction digits are specified, the value is truncated to an integer.
|
|
6
|
+
*
|
|
7
|
+
* The time complexity for this is `O(1)`.
|
|
8
|
+
*
|
|
9
|
+
* @template {number} T
|
|
10
|
+
* @param {number} value The number to truncate
|
|
11
|
+
* @param {PositiveInteger<T>} [fractionDigits=0] The number of digits to preserve after the decimal point
|
|
12
|
+
* @returns {number} The truncated number
|
|
13
|
+
* @throws {RangeError} If `fractionDigits` is less than `0` or greater than `100`
|
|
14
|
+
*/
|
|
15
|
+
export declare function trunc(value: number): number;
|
|
16
|
+
export declare function trunc<const T extends number>(value: number, fractionDigits: PositiveInteger<T>): number;
|
package/dist/object/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
5
|
-
export * from
|
|
1
|
+
export * from "./utils/entries";
|
|
2
|
+
export * from "./utils/extend";
|
|
3
|
+
export * from "./utils/isObject";
|
|
4
|
+
export * from "./utils/keys";
|
|
5
|
+
export * from "./utils/omit";
|
|
6
|
+
export * from "./utils/pick";
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Key } from "~/types";
|
|
2
|
+
/**
|
|
3
|
+
* @private
|
|
4
|
+
*/
|
|
5
|
+
type Mask<T extends Record<Key, unknown>> = (Partial<T> & Record<Key, unknown>) | Record<Key, unknown>;
|
|
6
|
+
export type Extend<T extends Record<Key, unknown>, U extends Mask<T>> = Omit<T, keyof U> & U;
|
|
7
|
+
/**
|
|
8
|
+
* Extends an object with another object.
|
|
9
|
+
*
|
|
10
|
+
* The time complexity for this is `O(n + m)` where:
|
|
11
|
+
* - `n` is the amount of keys in `value`.
|
|
12
|
+
* - `m` is the amount of keys in `other`.
|
|
13
|
+
*
|
|
14
|
+
* @template {Record<Key, unknown>} T
|
|
15
|
+
* @template {Partial<T> & Record<Key, unknown>} U
|
|
16
|
+
* @param {T} value The object to extend
|
|
17
|
+
* @param {U} other The object to override or extend properties of `value` with
|
|
18
|
+
* @returns {Extend<T, U>} An object where `value` is extended by `other`
|
|
19
|
+
*/
|
|
20
|
+
export declare function extend<const T extends Record<Key, unknown>, const U extends Mask<T>>(value: T, other: U): Extend<T, U>;
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a boolean based on the given chance percentage.
|
|
3
|
+
*
|
|
4
|
+
* The percentage specified should be on a scale of 0 - 1, meaning if you want a 50% chance
|
|
5
|
+
* the percentage should be specified to be 0.5.
|
|
6
|
+
*
|
|
7
|
+
* If the percentage is more than or equal to 1, the returned value will always be true.
|
|
8
|
+
* If the percentage is less than or equal to 0, the returned value will always be false.
|
|
9
|
+
*
|
|
10
|
+
* @param {number} percentage The percentage chance it has of being true (scale: 0 - 1)
|
|
11
|
+
* @returns {boolean} The random boolean based on the specified chance
|
|
12
|
+
*/
|
|
13
|
+
export declare function chance(percentage: number): boolean;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a random item from a given array or `undefined` if the array length is 0.
|
|
3
|
+
*
|
|
4
|
+
* The time complexity for this is `O(1)`.
|
|
5
|
+
*
|
|
6
|
+
* @template T
|
|
7
|
+
* @param {T[]} list The array to pick a random item from
|
|
8
|
+
* @returns {T | undefined} The random item
|
|
9
|
+
*/
|
|
10
|
+
export declare function draw<T>(list: [T, ...T[]]): T;
|
|
11
|
+
export declare function draw<T>(list: T[]): T | undefined;
|
|
12
|
+
export declare function draw<T>(list: readonly [T, ...T[]]): T;
|
|
13
|
+
export declare function draw<T>(list: readonly T[]): T | undefined;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a random number between a given minimum and maximum value.
|
|
3
|
+
*
|
|
4
|
+
* Returns `NaN` if the minimum and maximum contradict each other.
|
|
5
|
+
*
|
|
6
|
+
* NOTE: this utility is **NOT** cryptographically secure.
|
|
7
|
+
*
|
|
8
|
+
* The time complexity for this is `O(1)`.
|
|
9
|
+
*
|
|
10
|
+
* @param {number} [min=0] The minimum random value (default = `0`)
|
|
11
|
+
* @param {number} [max=1] The maximum random value (default = `1`)
|
|
12
|
+
* @returns {number} A random number between the min and max value
|
|
13
|
+
*/
|
|
14
|
+
export declare function random(min?: number, max?: number): number;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {Object} ScatterOptions
|
|
3
|
+
* @prop {"FLOOR" | "ROUND" | "CEIL"} [bias="ROUND"] The bias of the split
|
|
4
|
+
*/
|
|
5
|
+
export interface ScatterOptions {
|
|
6
|
+
/**
|
|
7
|
+
* The bias of the split.
|
|
8
|
+
*
|
|
9
|
+
* @default `"ROUND"`
|
|
10
|
+
*/
|
|
11
|
+
bias?: "FLOOR" | "ROUND" | "CEILING";
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Returns an array split into a specified percentage of the original length with the original values
|
|
15
|
+
* assigned to either side at random.
|
|
16
|
+
*
|
|
17
|
+
* *NOTE: The first array of the returned tuple will be in a random order, while the second array will have the order preserved.*
|
|
18
|
+
*
|
|
19
|
+
* The time complexity of this is `O(n + n**2)` where `n` is the amount of items in the list.
|
|
20
|
+
*
|
|
21
|
+
* *NOTE: The time complexity of this is `O(n)` if the split is outside the specified range.*
|
|
22
|
+
*
|
|
23
|
+
* @template T
|
|
24
|
+
* @param {T[]} list The list to scatter
|
|
25
|
+
* @param {number} [split=0.5] The percentage to split the array into (scale: 0 - 1) (default = `0.5`)
|
|
26
|
+
* @param {ScatterOptions} [options] The options given to scatter the array
|
|
27
|
+
* @returns {[T[], T[]]} A split of the original array
|
|
28
|
+
*/
|
|
29
|
+
export declare function scatter<const T>(list: T[], split?: number, options?: ScatterOptions): [T[], T[]];
|
|
30
|
+
export declare function scatter<const T>(list: readonly T[], split?: number, options?: ScatterOptions): [T[], T[]];
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a new Array from a given array, with every item moved to a random position.
|
|
3
|
+
*
|
|
4
|
+
* The time complexity for this is `O(2n)` where `n` is the amount of values in the list.
|
|
5
|
+
*
|
|
6
|
+
* @template T
|
|
7
|
+
* @param {T[]} list The array to shuffle
|
|
8
|
+
* @returns {T[]} The shuffled array
|
|
9
|
+
*/
|
|
10
|
+
export declare function shuffle<const T>(list: T[]): T[];
|
|
11
|
+
export declare function shuffle<const T>(list: readonly T[]): T[];
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a cryptographically secure random string of characters, based on a given charset, grown to a specified length.
|
|
3
|
+
*
|
|
4
|
+
* The time complexity for this is `O(n log8(m))` where:
|
|
5
|
+
* - `n` is the length of the uid.
|
|
6
|
+
* - `m` is the length of the charset.
|
|
7
|
+
*
|
|
8
|
+
* @param {number} [length=8] The length of de uid (default = `8`)
|
|
9
|
+
* @param {string} [charset="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"] The characters used for generation (default = `"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"`)
|
|
10
|
+
* @returns {string} A random string of characters from the charset, with the specified length
|
|
11
|
+
*/
|
|
12
|
+
export declare function uid(length?: number, charset?: string): string;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export type UuidRNG = () => Uint8Array;
|
|
2
|
+
/**
|
|
3
|
+
* @typedef {Object} UuidRNGOptions
|
|
4
|
+
* @prop {UuidRNG} [rng] A function that returns a list of randomly generated bytes
|
|
5
|
+
* @prop {Uint8Array} [random] A list of randomly generated bytes
|
|
6
|
+
*/
|
|
7
|
+
export interface UuidRNGOptions {
|
|
8
|
+
/**
|
|
9
|
+
* A function that returns a list of randomly generated bytes.
|
|
10
|
+
*/
|
|
11
|
+
rng?: UuidRNG;
|
|
12
|
+
/**
|
|
13
|
+
* A list of randomly generated bytes.
|
|
14
|
+
*/
|
|
15
|
+
random?: Uint8Array;
|
|
16
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { UtilError } from "~/error";
|
|
2
|
+
import type { UuidRNGOptions } from "../../types";
|
|
3
|
+
export declare class UuidV1UtilError extends UtilError {
|
|
4
|
+
readonly util = "uuidV1";
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* @typedef {Object} UuidV1Options
|
|
8
|
+
* @extends UuidRNGOptions
|
|
9
|
+
* @prop {number} [milliseconds=Date.now()] The amount of milliseconds that have past since the EPOCH (01-01-1970)
|
|
10
|
+
* @prop {number} [nanoseconds=0] The amount of nanoseconds that have elapsed in the current millisecond
|
|
11
|
+
* @prop {number} [clockSequence] The clock sequence used for generation
|
|
12
|
+
* @prop {Uint8Array} [node] A list of random bytes used to complete the UUID
|
|
13
|
+
*/
|
|
14
|
+
export interface UuidV1Options extends UuidRNGOptions {
|
|
15
|
+
/**
|
|
16
|
+
* The amount of milliseconds that have past since the EPOCH (01-01-1970).
|
|
17
|
+
*
|
|
18
|
+
* @default `Date.now()`
|
|
19
|
+
*/
|
|
20
|
+
milliseconds?: number;
|
|
21
|
+
/**
|
|
22
|
+
* The amount of nanoseconds that have elapsed in the current millisecond.
|
|
23
|
+
*
|
|
24
|
+
* @default `0`
|
|
25
|
+
*/
|
|
26
|
+
nanoseconds?: number;
|
|
27
|
+
/**
|
|
28
|
+
* The clock sequence used for generation.
|
|
29
|
+
*/
|
|
30
|
+
clockSequence?: number;
|
|
31
|
+
/**
|
|
32
|
+
* A list of random bytes used to complete the UUID.
|
|
33
|
+
*/
|
|
34
|
+
node?: Uint8Array;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Returns a Cryptographically secure random V1 UUID string.
|
|
38
|
+
*
|
|
39
|
+
* The time complexity for this is `O(1)`.
|
|
40
|
+
*
|
|
41
|
+
* @param {UuidV1Options} [options] The options to decide how to generate the UUID
|
|
42
|
+
* @returns {string} A V1 UUID string
|
|
43
|
+
* @throws {UuidV1UtilError} If the length of the specified random bytes is less then 16
|
|
44
|
+
*/
|
|
45
|
+
export declare function uuidV1(options?: UuidV1Options): string;
|
|
46
|
+
export { uuidV1 as v1 };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { UtilError } from "~/error";
|
|
2
|
+
import type { UuidRNGOptions } from "../../types";
|
|
3
|
+
export declare class UuidV4UtilError extends UtilError {
|
|
4
|
+
readonly util = "uuidV4";
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* @typedef {Object} UuidV4Options
|
|
8
|
+
* @extends UuidRNGOptions
|
|
9
|
+
*/
|
|
10
|
+
export interface UuidV4Options extends UuidRNGOptions {
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Returns a Cryptographically secure random V4 UUID string.
|
|
14
|
+
*
|
|
15
|
+
* The time complexity for this is `O(1)`.
|
|
16
|
+
*
|
|
17
|
+
* @param {UuidV1Options} [options] The options to decide how to generate the UUID
|
|
18
|
+
* @returns {string} A V4 UUID string
|
|
19
|
+
* @throws {UuidV4UtilError} If the length of the specified random bytes is less then 16
|
|
20
|
+
*/
|
|
21
|
+
export declare function uuidV4(options?: UuidV4Options): string;
|
|
22
|
+
export { uuidV4 as v4, uuidV4 as uuid };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { UtilError } from "~/error";
|
|
2
|
+
import type { UuidRNGOptions } from "../../types";
|
|
3
|
+
export declare class UuidV7UtilError extends UtilError {
|
|
4
|
+
readonly util = "uuidV7";
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* @typedef {Object} UuidV7Options
|
|
8
|
+
* @extends UuidRNGOptions
|
|
9
|
+
* @prop {number} [milliseconds=Date.now()] The amount of milliseconds that have past since the EPOCH (01-01-1970)
|
|
10
|
+
* @prop {number} [clockSequence] The clock sequence used for generation
|
|
11
|
+
*/
|
|
12
|
+
export interface UuidV7Options extends UuidRNGOptions {
|
|
13
|
+
/**
|
|
14
|
+
* The amount of milliseconds that have past since the EPOCH (01-01-1970).
|
|
15
|
+
*
|
|
16
|
+
* @default `Date.now()`
|
|
17
|
+
*/
|
|
18
|
+
milliseconds?: number;
|
|
19
|
+
/**
|
|
20
|
+
* The clock sequence used for generation.
|
|
21
|
+
*/
|
|
22
|
+
sequence?: number;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Returns a Cryptographically secure random V7 UUID string.
|
|
26
|
+
*
|
|
27
|
+
* The time complexity for this is `O(1)`.
|
|
28
|
+
*
|
|
29
|
+
* @param {UuidV7Options} [options] The options to decide how to generate the UUID
|
|
30
|
+
* @returns {string} A V7 UUID string
|
|
31
|
+
* @throws {UuidV7UtilError} If the length of the specified random bytes is less then 16
|
|
32
|
+
*/
|
|
33
|
+
export declare function uuidV7(options?: UuidV7Options): string;
|
|
34
|
+
export { uuidV7 as v7 };
|