@omnific/utils 0.0.3 → 0.0.5
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/dist/createChainedFunction.d.ts +19 -0
- package/dist/createChainedFunction.js +23 -0
- package/dist/debounce.d.ts +78 -0
- package/dist/debounce.js +90 -0
- package/dist/hasValue.d.ts +20 -0
- package/dist/hasValue.js +23 -0
- package/dist/isArrayBuffer.d.ts +21 -0
- package/dist/isArrayBuffer.js +24 -0
- package/dist/isBlob.d.ts +21 -0
- package/dist/isBlob.js +24 -0
- package/dist/isFormData.d.ts +21 -0
- package/dist/isFormData.js +24 -0
- package/dist/isPlainObject.d.ts +46 -0
- package/dist/isPlainObject.js +53 -0
- package/dist/isURLSearchParams.d.ts +21 -0
- package/dist/isURLSearchParams.js +24 -0
- package/dist/main.d.ts +12 -1
- package/dist/main.js +14 -3
- package/dist/parseJson.d.ts +10 -0
- package/dist/parseJson.js +18 -0
- package/dist/resolveValue.d.ts +17 -0
- package/dist/resolveValue.js +21 -0
- package/dist/throttle.d.ts +52 -0
- package/dist/throttle.js +58 -0
- package/package.json +1 -1
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
//#region createChainedFunction.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* A function with a bound `this` context and variadic arguments.
|
|
4
|
+
*/
|
|
5
|
+
type ChainedFunction<TArgs extends any[], TThis> = (this: TThis, ...args: TArgs) => void;
|
|
6
|
+
/**
|
|
7
|
+
* Composes multiple callbacks into a single callback while preserving `this`.
|
|
8
|
+
*
|
|
9
|
+
* Nullish callbacks are ignored. When no valid callback is provided, a no-op
|
|
10
|
+
* function is returned so the consumer can call the result unconditionally.
|
|
11
|
+
*
|
|
12
|
+
* @template TArgs
|
|
13
|
+
* @template TThis
|
|
14
|
+
* @param {...(ChainedFunction<TArgs, TThis> | null | undefined)[]} funcs - The callbacks to chain.
|
|
15
|
+
* @returns {ChainedFunction<TArgs, TThis>} A single chained callback.
|
|
16
|
+
*/
|
|
17
|
+
declare function createChainedFunction<TArgs extends any[], TThis>(...funcs: Array<ChainedFunction<TArgs, TThis> | null | undefined>): ChainedFunction<TArgs, TThis>;
|
|
18
|
+
//#endregion
|
|
19
|
+
export { ChainedFunction, createChainedFunction, createChainedFunction as default };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { isFunction } from "./isFunction.js";
|
|
2
|
+
//#region createChainedFunction.ts
|
|
3
|
+
/**
|
|
4
|
+
* Composes multiple callbacks into a single callback while preserving `this`.
|
|
5
|
+
*
|
|
6
|
+
* Nullish callbacks are ignored. When no valid callback is provided, a no-op
|
|
7
|
+
* function is returned so the consumer can call the result unconditionally.
|
|
8
|
+
*
|
|
9
|
+
* @template TArgs
|
|
10
|
+
* @template TThis
|
|
11
|
+
* @param {...(ChainedFunction<TArgs, TThis> | null | undefined)[]} funcs - The callbacks to chain.
|
|
12
|
+
* @returns {ChainedFunction<TArgs, TThis>} A single chained callback.
|
|
13
|
+
*/
|
|
14
|
+
function createChainedFunction(...funcs) {
|
|
15
|
+
const validFuncs = funcs.filter((fn) => isFunction(fn));
|
|
16
|
+
if (validFuncs.length === 0) return function() {};
|
|
17
|
+
if (validFuncs.length === 1) return validFuncs[0];
|
|
18
|
+
return function(...args) {
|
|
19
|
+
for (const fn of validFuncs) fn.apply(this, args);
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
export { createChainedFunction, createChainedFunction as default };
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
//#region debounce.d.ts
|
|
2
|
+
interface DebounceOptions {
|
|
3
|
+
/**
|
|
4
|
+
* An optional AbortSignal to cancel the debounced function.
|
|
5
|
+
*/
|
|
6
|
+
signal?: AbortSignal;
|
|
7
|
+
/**
|
|
8
|
+
* An optional array specifying whether the function should be invoked on the leading edge, trailing edge, or both.
|
|
9
|
+
* If `edges` includes "leading", the function will be invoked at the start of the delay period.
|
|
10
|
+
* If `edges` includes "trailing", the function will be invoked at the end of the delay period.
|
|
11
|
+
* If both "leading" and "trailing" are included, the function will be invoked at both the start and end of the delay period.
|
|
12
|
+
* @default ["trailing"]
|
|
13
|
+
*/
|
|
14
|
+
edges?: Array<'leading' | 'trailing'>;
|
|
15
|
+
}
|
|
16
|
+
interface DebouncedFunction<F extends (...args: any[]) => void> {
|
|
17
|
+
(...args: Parameters<F>): void;
|
|
18
|
+
/**
|
|
19
|
+
* Schedules the execution of the debounced function after the specified debounce delay.
|
|
20
|
+
* This method resets any existing timer, ensuring that the function is only invoked
|
|
21
|
+
* after the delay has elapsed since the last call to the debounced function.
|
|
22
|
+
* It is typically called internally whenever the debounced function is invoked.
|
|
23
|
+
*
|
|
24
|
+
* @returns {void}
|
|
25
|
+
*/
|
|
26
|
+
schedule: () => void;
|
|
27
|
+
/**
|
|
28
|
+
* Cancels any pending execution of the debounced function.
|
|
29
|
+
* This method clears the active timer and resets any stored context or arguments.
|
|
30
|
+
*/
|
|
31
|
+
cancel: () => void;
|
|
32
|
+
/**
|
|
33
|
+
* Immediately invokes the debounced function if there is a pending execution.
|
|
34
|
+
* This method executes the function right away if there is a pending execution.
|
|
35
|
+
*/
|
|
36
|
+
flush: () => void;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Creates a debounced function that delays invoking the provided function until after `debounceMs` milliseconds
|
|
40
|
+
* have elapsed since the last time the debounced function was invoked. The debounced function also has a `cancel`
|
|
41
|
+
* method to cancel any pending execution.
|
|
42
|
+
*
|
|
43
|
+
* @template F - The type of function.
|
|
44
|
+
* @param {F} func - The function to debounce.
|
|
45
|
+
* @param {number} debounceMs - The number of milliseconds to delay.
|
|
46
|
+
* @param {DebounceOptions} options - The options object
|
|
47
|
+
* @param {AbortSignal} options.signal - An optional AbortSignal to cancel the debounced function.
|
|
48
|
+
* @returns A new debounced function with a `cancel` method.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* const debouncedFunction = debounce(() => {
|
|
52
|
+
* console.log('Function executed');
|
|
53
|
+
* }, 1000);
|
|
54
|
+
*
|
|
55
|
+
* // Will log 'Function executed' after 1 second if not called again in that time
|
|
56
|
+
* debouncedFunction();
|
|
57
|
+
*
|
|
58
|
+
* // Will not log anything as the previous call is canceled
|
|
59
|
+
* debouncedFunction.cancel();
|
|
60
|
+
*
|
|
61
|
+
* // With AbortSignal
|
|
62
|
+
* const controller = new AbortController();
|
|
63
|
+
* const signal = controller.signal;
|
|
64
|
+
* const debouncedWithSignal = debounce(() => {
|
|
65
|
+
* console.log('Function executed');
|
|
66
|
+
* }, 1000, { signal });
|
|
67
|
+
*
|
|
68
|
+
* debouncedWithSignal();
|
|
69
|
+
*
|
|
70
|
+
* // Will cancel the debounced function call
|
|
71
|
+
* controller.abort();
|
|
72
|
+
*/
|
|
73
|
+
declare function debounce<F extends (...args: any[]) => void>(func: F, debounceMs: number, {
|
|
74
|
+
signal,
|
|
75
|
+
edges
|
|
76
|
+
}?: DebounceOptions): DebouncedFunction<F>;
|
|
77
|
+
//#endregion
|
|
78
|
+
export { DebounceOptions, DebouncedFunction, debounce };
|
package/dist/debounce.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { isUndefined } from "./isUndefined.js";
|
|
2
|
+
//#region debounce.ts
|
|
3
|
+
/**
|
|
4
|
+
* Creates a debounced function that delays invoking the provided function until after `debounceMs` milliseconds
|
|
5
|
+
* have elapsed since the last time the debounced function was invoked. The debounced function also has a `cancel`
|
|
6
|
+
* method to cancel any pending execution.
|
|
7
|
+
*
|
|
8
|
+
* @template F - The type of function.
|
|
9
|
+
* @param {F} func - The function to debounce.
|
|
10
|
+
* @param {number} debounceMs - The number of milliseconds to delay.
|
|
11
|
+
* @param {DebounceOptions} options - The options object
|
|
12
|
+
* @param {AbortSignal} options.signal - An optional AbortSignal to cancel the debounced function.
|
|
13
|
+
* @returns A new debounced function with a `cancel` method.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* const debouncedFunction = debounce(() => {
|
|
17
|
+
* console.log('Function executed');
|
|
18
|
+
* }, 1000);
|
|
19
|
+
*
|
|
20
|
+
* // Will log 'Function executed' after 1 second if not called again in that time
|
|
21
|
+
* debouncedFunction();
|
|
22
|
+
*
|
|
23
|
+
* // Will not log anything as the previous call is canceled
|
|
24
|
+
* debouncedFunction.cancel();
|
|
25
|
+
*
|
|
26
|
+
* // With AbortSignal
|
|
27
|
+
* const controller = new AbortController();
|
|
28
|
+
* const signal = controller.signal;
|
|
29
|
+
* const debouncedWithSignal = debounce(() => {
|
|
30
|
+
* console.log('Function executed');
|
|
31
|
+
* }, 1000, { signal });
|
|
32
|
+
*
|
|
33
|
+
* debouncedWithSignal();
|
|
34
|
+
*
|
|
35
|
+
* // Will cancel the debounced function call
|
|
36
|
+
* controller.abort();
|
|
37
|
+
*/
|
|
38
|
+
function debounce(func, debounceMs, { signal, edges } = {}) {
|
|
39
|
+
let pendingInvoke;
|
|
40
|
+
const leading = !isUndefined(edges) && edges.includes("leading");
|
|
41
|
+
const trailing = isUndefined(edges) || edges.includes("trailing");
|
|
42
|
+
const invoke = () => {
|
|
43
|
+
if (!isUndefined(pendingInvoke)) {
|
|
44
|
+
pendingInvoke();
|
|
45
|
+
pendingInvoke = void 0;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
const onTimerEnd = () => {
|
|
49
|
+
if (trailing) invoke();
|
|
50
|
+
cancel();
|
|
51
|
+
};
|
|
52
|
+
let timeoutId;
|
|
53
|
+
const schedule = () => {
|
|
54
|
+
if (!isUndefined(timeoutId)) clearTimeout(timeoutId);
|
|
55
|
+
timeoutId = setTimeout(() => {
|
|
56
|
+
timeoutId = void 0;
|
|
57
|
+
onTimerEnd();
|
|
58
|
+
}, debounceMs);
|
|
59
|
+
};
|
|
60
|
+
const cancelTimer = () => {
|
|
61
|
+
if (!isUndefined(timeoutId)) {
|
|
62
|
+
clearTimeout(timeoutId);
|
|
63
|
+
timeoutId = void 0;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
const cancel = () => {
|
|
67
|
+
cancelTimer();
|
|
68
|
+
pendingInvoke = void 0;
|
|
69
|
+
};
|
|
70
|
+
const flush = () => {
|
|
71
|
+
cancelTimer();
|
|
72
|
+
invoke();
|
|
73
|
+
};
|
|
74
|
+
const debounced = function(...args) {
|
|
75
|
+
if (signal?.aborted) return;
|
|
76
|
+
pendingInvoke = () => {
|
|
77
|
+
func.apply(this, args);
|
|
78
|
+
};
|
|
79
|
+
const isFirstCall = isUndefined(timeoutId);
|
|
80
|
+
schedule();
|
|
81
|
+
if (leading && isFirstCall) invoke();
|
|
82
|
+
};
|
|
83
|
+
debounced.schedule = schedule;
|
|
84
|
+
debounced.cancel = cancel;
|
|
85
|
+
debounced.flush = flush;
|
|
86
|
+
signal?.addEventListener("abort", cancel, { once: true });
|
|
87
|
+
return debounced;
|
|
88
|
+
}
|
|
89
|
+
//#endregion
|
|
90
|
+
export { debounce };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
//#region hasValue.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Checks whether a value is neither `null` nor `undefined`.
|
|
4
|
+
*
|
|
5
|
+
* This helper acts as a non-nullish type guard, allowing TypeScript to narrow
|
|
6
|
+
* the checked value to `NonNullable<T>`.
|
|
7
|
+
*
|
|
8
|
+
* @template T
|
|
9
|
+
* @param {T} value - The value to check.
|
|
10
|
+
* @returns {value is NonNullable<T>} `true` when the value is present.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const value = maybeValue();
|
|
14
|
+
* if (hasValue(value)) {
|
|
15
|
+
* value;
|
|
16
|
+
* }
|
|
17
|
+
*/
|
|
18
|
+
declare function hasValue<T>(value: T): value is NonNullable<T>;
|
|
19
|
+
//#endregion
|
|
20
|
+
export { hasValue as default, hasValue };
|
package/dist/hasValue.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { isNil } from "./isNil.js";
|
|
2
|
+
//#region hasValue.ts
|
|
3
|
+
/**
|
|
4
|
+
* Checks whether a value is neither `null` nor `undefined`.
|
|
5
|
+
*
|
|
6
|
+
* This helper acts as a non-nullish type guard, allowing TypeScript to narrow
|
|
7
|
+
* the checked value to `NonNullable<T>`.
|
|
8
|
+
*
|
|
9
|
+
* @template T
|
|
10
|
+
* @param {T} value - The value to check.
|
|
11
|
+
* @returns {value is NonNullable<T>} `true` when the value is present.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const value = maybeValue();
|
|
15
|
+
* if (hasValue(value)) {
|
|
16
|
+
* value;
|
|
17
|
+
* }
|
|
18
|
+
*/
|
|
19
|
+
function hasValue(value) {
|
|
20
|
+
return !isNil(value);
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
export { hasValue as default, hasValue };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
//#region isArrayBuffer.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Checks if a given value is `ArrayBuffer`.
|
|
4
|
+
*
|
|
5
|
+
* This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `ArrayBuffer`.
|
|
6
|
+
*
|
|
7
|
+
* @param {unknown} value The value to check if it is `ArrayBuffer`.
|
|
8
|
+
* @returns {value is ArrayBuffer} Returns `true` if `value` is `ArrayBuffer`, else `false`.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* const value1 = new ArrayBuffer(8);
|
|
12
|
+
* const value2 = new Blob(['hello']);
|
|
13
|
+
* const value3 = {};
|
|
14
|
+
*
|
|
15
|
+
* console.log(isArrayBuffer(value1)); // true
|
|
16
|
+
* console.log(isArrayBuffer(value2)); // false
|
|
17
|
+
* console.log(isArrayBuffer(value3)); // false
|
|
18
|
+
*/
|
|
19
|
+
declare function isArrayBuffer(value: unknown): value is ArrayBuffer;
|
|
20
|
+
//#endregion
|
|
21
|
+
export { isArrayBuffer };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import hasValue from "./hasValue.js";
|
|
2
|
+
//#region isArrayBuffer.ts
|
|
3
|
+
/**
|
|
4
|
+
* Checks if a given value is `ArrayBuffer`.
|
|
5
|
+
*
|
|
6
|
+
* This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `ArrayBuffer`.
|
|
7
|
+
*
|
|
8
|
+
* @param {unknown} value The value to check if it is `ArrayBuffer`.
|
|
9
|
+
* @returns {value is ArrayBuffer} Returns `true` if `value` is `ArrayBuffer`, else `false`.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const value1 = new ArrayBuffer(8);
|
|
13
|
+
* const value2 = new Blob(['hello']);
|
|
14
|
+
* const value3 = {};
|
|
15
|
+
*
|
|
16
|
+
* console.log(isArrayBuffer(value1)); // true
|
|
17
|
+
* console.log(isArrayBuffer(value2)); // false
|
|
18
|
+
* console.log(isArrayBuffer(value3)); // false
|
|
19
|
+
*/
|
|
20
|
+
function isArrayBuffer(value) {
|
|
21
|
+
return hasValue(value) && value instanceof ArrayBuffer;
|
|
22
|
+
}
|
|
23
|
+
//#endregion
|
|
24
|
+
export { isArrayBuffer };
|
package/dist/isBlob.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
//#region isBlob.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Checks if a given value is `Blob`.
|
|
4
|
+
*
|
|
5
|
+
* This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `Blob`.
|
|
6
|
+
*
|
|
7
|
+
* @param {unknown} value The value to check if it is `Blob`.
|
|
8
|
+
* @returns {value is Blob} Returns `true` if `value` is `Blob`, else `false`.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* const value1 = new Blob(['hello']);
|
|
12
|
+
* const value2 = new ArrayBuffer(8);
|
|
13
|
+
* const value3 = {};
|
|
14
|
+
*
|
|
15
|
+
* console.log(isBlob(value1)); // true
|
|
16
|
+
* console.log(isBlob(value2)); // false
|
|
17
|
+
* console.log(isBlob(value3)); // false
|
|
18
|
+
*/
|
|
19
|
+
declare function isBlob(value: unknown): value is Blob;
|
|
20
|
+
//#endregion
|
|
21
|
+
export { isBlob };
|
package/dist/isBlob.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import hasValue from "./hasValue.js";
|
|
2
|
+
//#region isBlob.ts
|
|
3
|
+
/**
|
|
4
|
+
* Checks if a given value is `Blob`.
|
|
5
|
+
*
|
|
6
|
+
* This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `Blob`.
|
|
7
|
+
*
|
|
8
|
+
* @param {unknown} value The value to check if it is `Blob`.
|
|
9
|
+
* @returns {value is Blob} Returns `true` if `value` is `Blob`, else `false`.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const value1 = new Blob(['hello']);
|
|
13
|
+
* const value2 = new ArrayBuffer(8);
|
|
14
|
+
* const value3 = {};
|
|
15
|
+
*
|
|
16
|
+
* console.log(isBlob(value1)); // true
|
|
17
|
+
* console.log(isBlob(value2)); // false
|
|
18
|
+
* console.log(isBlob(value3)); // false
|
|
19
|
+
*/
|
|
20
|
+
function isBlob(value) {
|
|
21
|
+
return hasValue(value) && value instanceof Blob;
|
|
22
|
+
}
|
|
23
|
+
//#endregion
|
|
24
|
+
export { isBlob };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
//#region isFormData.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Checks if a given value is `FormData`.
|
|
4
|
+
*
|
|
5
|
+
* This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `FormData`.
|
|
6
|
+
*
|
|
7
|
+
* @param {unknown} value The value to check if it is `FormData`.
|
|
8
|
+
* @returns {value is FormData} Returns `true` if `value` is `FormData`, else `false`.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* const value1 = new FormData();
|
|
12
|
+
* const value2 = new URLSearchParams();
|
|
13
|
+
* const value3 = {};
|
|
14
|
+
*
|
|
15
|
+
* console.log(isFormData(value1)); // true
|
|
16
|
+
* console.log(isFormData(value2)); // false
|
|
17
|
+
* console.log(isFormData(value3)); // false
|
|
18
|
+
*/
|
|
19
|
+
declare function isFormData(value: unknown): value is FormData;
|
|
20
|
+
//#endregion
|
|
21
|
+
export { isFormData };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import hasValue from "./hasValue.js";
|
|
2
|
+
//#region isFormData.ts
|
|
3
|
+
/**
|
|
4
|
+
* Checks if a given value is `FormData`.
|
|
5
|
+
*
|
|
6
|
+
* This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `FormData`.
|
|
7
|
+
*
|
|
8
|
+
* @param {unknown} value The value to check if it is `FormData`.
|
|
9
|
+
* @returns {value is FormData} Returns `true` if `value` is `FormData`, else `false`.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const value1 = new FormData();
|
|
13
|
+
* const value2 = new URLSearchParams();
|
|
14
|
+
* const value3 = {};
|
|
15
|
+
*
|
|
16
|
+
* console.log(isFormData(value1)); // true
|
|
17
|
+
* console.log(isFormData(value2)); // false
|
|
18
|
+
* console.log(isFormData(value3)); // false
|
|
19
|
+
*/
|
|
20
|
+
function isFormData(value) {
|
|
21
|
+
return hasValue(value) && value instanceof FormData;
|
|
22
|
+
}
|
|
23
|
+
//#endregion
|
|
24
|
+
export { isFormData };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
//#region isPlainObject.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Checks if a given value is a plain object.
|
|
4
|
+
*
|
|
5
|
+
* @param {object} value - The value to check.
|
|
6
|
+
* @returns {value is Record<PropertyKey, any>} - True if the value is a plain object, otherwise false.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* // ✅ True
|
|
11
|
+
*
|
|
12
|
+
* isPlainObject({}); // ✅
|
|
13
|
+
* isPlainObject({ key: 'value' }); // ✅
|
|
14
|
+
* isPlainObject({ key: new Date() }); // ✅
|
|
15
|
+
* isPlainObject(new Object()); // ✅
|
|
16
|
+
* isPlainObject(Object.create(null)); // ✅
|
|
17
|
+
* isPlainObject({ nested: { key: true } }); // ✅
|
|
18
|
+
* isPlainObject(new Proxy({}, {})); // ✅
|
|
19
|
+
* isPlainObject({ [Symbol('tag')]: 'A' }); // ✅
|
|
20
|
+
*
|
|
21
|
+
* // ✅ (cross-realms, node context, workers, ...)
|
|
22
|
+
* const runInNewContext = await import('node:vm').then(
|
|
23
|
+
* mod => mod.runInNewContext
|
|
24
|
+
* );
|
|
25
|
+
* isPlainObject(runInNewContext('({})')); // ✅
|
|
26
|
+
*
|
|
27
|
+
* // ❌ False
|
|
28
|
+
*
|
|
29
|
+
* class Test {}
|
|
30
|
+
* isPlainObject(new Test()); // ❌
|
|
31
|
+
* isPlainObject(10); // ❌
|
|
32
|
+
* isPlainObject(null); // ❌
|
|
33
|
+
* isPlainObject('hello'); // ❌
|
|
34
|
+
* isPlainObject([]); // ❌
|
|
35
|
+
* isPlainObject(new Date()); // ❌
|
|
36
|
+
* isPlainObject(new Uint8Array([1])); // ❌
|
|
37
|
+
* isPlainObject(Buffer.from('ABC')); // ❌
|
|
38
|
+
* isPlainObject(Promise.resolve({})); // ❌
|
|
39
|
+
* isPlainObject(Object.create({})); // ❌
|
|
40
|
+
* isPlainObject(new (class Cls {})()); // ❌
|
|
41
|
+
* isPlainObject(globalThis); // ❌
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
declare function isPlainObject(value: unknown): value is Record<PropertyKey, any>;
|
|
45
|
+
//#endregion
|
|
46
|
+
export { isPlainObject };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { isNull } from "./isNull.js";
|
|
2
|
+
import { isObject } from "./isObject.js";
|
|
3
|
+
//#region isPlainObject.ts
|
|
4
|
+
/**
|
|
5
|
+
* Checks if a given value is a plain object.
|
|
6
|
+
*
|
|
7
|
+
* @param {object} value - The value to check.
|
|
8
|
+
* @returns {value is Record<PropertyKey, any>} - True if the value is a plain object, otherwise false.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* // ✅ True
|
|
13
|
+
*
|
|
14
|
+
* isPlainObject({}); // ✅
|
|
15
|
+
* isPlainObject({ key: 'value' }); // ✅
|
|
16
|
+
* isPlainObject({ key: new Date() }); // ✅
|
|
17
|
+
* isPlainObject(new Object()); // ✅
|
|
18
|
+
* isPlainObject(Object.create(null)); // ✅
|
|
19
|
+
* isPlainObject({ nested: { key: true } }); // ✅
|
|
20
|
+
* isPlainObject(new Proxy({}, {})); // ✅
|
|
21
|
+
* isPlainObject({ [Symbol('tag')]: 'A' }); // ✅
|
|
22
|
+
*
|
|
23
|
+
* // ✅ (cross-realms, node context, workers, ...)
|
|
24
|
+
* const runInNewContext = await import('node:vm').then(
|
|
25
|
+
* mod => mod.runInNewContext
|
|
26
|
+
* );
|
|
27
|
+
* isPlainObject(runInNewContext('({})')); // ✅
|
|
28
|
+
*
|
|
29
|
+
* // ❌ False
|
|
30
|
+
*
|
|
31
|
+
* class Test {}
|
|
32
|
+
* isPlainObject(new Test()); // ❌
|
|
33
|
+
* isPlainObject(10); // ❌
|
|
34
|
+
* isPlainObject(null); // ❌
|
|
35
|
+
* isPlainObject('hello'); // ❌
|
|
36
|
+
* isPlainObject([]); // ❌
|
|
37
|
+
* isPlainObject(new Date()); // ❌
|
|
38
|
+
* isPlainObject(new Uint8Array([1])); // ❌
|
|
39
|
+
* isPlainObject(Buffer.from('ABC')); // ❌
|
|
40
|
+
* isPlainObject(Promise.resolve({})); // ❌
|
|
41
|
+
* isPlainObject(Object.create({})); // ❌
|
|
42
|
+
* isPlainObject(new (class Cls {})()); // ❌
|
|
43
|
+
* isPlainObject(globalThis); // ❌
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
function isPlainObject(value) {
|
|
47
|
+
if (!isObject(value)) return false;
|
|
48
|
+
const proto = Object.getPrototypeOf(value);
|
|
49
|
+
if (!(isNull(proto) || proto === Object.prototype || isNull(Object.getPrototypeOf(proto)))) return false;
|
|
50
|
+
return Object.prototype.toString.call(value) === "[object Object]";
|
|
51
|
+
}
|
|
52
|
+
//#endregion
|
|
53
|
+
export { isPlainObject };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
//#region isURLSearchParams.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Checks if a given value is `URLSearchParams`.
|
|
4
|
+
*
|
|
5
|
+
* This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `URLSearchParams`.
|
|
6
|
+
*
|
|
7
|
+
* @param {unknown} value The value to check if it is `URLSearchParams`.
|
|
8
|
+
* @returns {value is URLSearchParams} Returns `true` if `value` is `URLSearchParams`, else `false`.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* const value1 = new URLSearchParams('a=1');
|
|
12
|
+
* const value2 = new FormData();
|
|
13
|
+
* const value3 = 'a=1';
|
|
14
|
+
*
|
|
15
|
+
* console.log(isURLSearchParams(value1)); // true
|
|
16
|
+
* console.log(isURLSearchParams(value2)); // false
|
|
17
|
+
* console.log(isURLSearchParams(value3)); // false
|
|
18
|
+
*/
|
|
19
|
+
declare function isURLSearchParams(value: unknown): value is URLSearchParams;
|
|
20
|
+
//#endregion
|
|
21
|
+
export { isURLSearchParams };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import hasValue from "./hasValue.js";
|
|
2
|
+
//#region isURLSearchParams.ts
|
|
3
|
+
/**
|
|
4
|
+
* Checks if a given value is `URLSearchParams`.
|
|
5
|
+
*
|
|
6
|
+
* This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `URLSearchParams`.
|
|
7
|
+
*
|
|
8
|
+
* @param {unknown} value The value to check if it is `URLSearchParams`.
|
|
9
|
+
* @returns {value is URLSearchParams} Returns `true` if `value` is `URLSearchParams`, else `false`.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const value1 = new URLSearchParams('a=1');
|
|
13
|
+
* const value2 = new FormData();
|
|
14
|
+
* const value3 = 'a=1';
|
|
15
|
+
*
|
|
16
|
+
* console.log(isURLSearchParams(value1)); // true
|
|
17
|
+
* console.log(isURLSearchParams(value2)); // false
|
|
18
|
+
* console.log(isURLSearchParams(value3)); // false
|
|
19
|
+
*/
|
|
20
|
+
function isURLSearchParams(value) {
|
|
21
|
+
return hasValue(value) && value instanceof URLSearchParams;
|
|
22
|
+
}
|
|
23
|
+
//#endregion
|
|
24
|
+
export { isURLSearchParams };
|
package/dist/main.d.ts
CHANGED
|
@@ -1,13 +1,24 @@
|
|
|
1
|
+
import { ChainedFunction, createChainedFunction } from "./createChainedFunction.js";
|
|
2
|
+
import { DebounceOptions, DebouncedFunction, debounce } from "./debounce.js";
|
|
1
3
|
import { get } from "./get.js";
|
|
4
|
+
import hasValue from "./hasValue.js";
|
|
2
5
|
import { isArray } from "./isArray.js";
|
|
6
|
+
import { isArrayBuffer } from "./isArrayBuffer.js";
|
|
7
|
+
import { isBlob } from "./isBlob.js";
|
|
3
8
|
import { isBoolean } from "./isBoolean.js";
|
|
9
|
+
import { isFormData } from "./isFormData.js";
|
|
4
10
|
import { isFunction } from "./isFunction.js";
|
|
5
11
|
import { isNil } from "./isNil.js";
|
|
6
12
|
import { isNull } from "./isNull.js";
|
|
7
13
|
import { isNumber } from "./isNumber.js";
|
|
8
14
|
import { isObject } from "./isObject.js";
|
|
15
|
+
import { isPlainObject } from "./isPlainObject.js";
|
|
9
16
|
import { isPromise } from "./isPromise.js";
|
|
10
17
|
import { isString } from "./isString.js";
|
|
11
18
|
import { isSymbol } from "./isSymbol.js";
|
|
19
|
+
import { isURLSearchParams } from "./isURLSearchParams.js";
|
|
12
20
|
import { isUndefined } from "./isUndefined.js";
|
|
13
|
-
|
|
21
|
+
import { parseJson } from "./parseJson.js";
|
|
22
|
+
import { ThrottleOptions, ThrottledFunction, throttle } from "./throttle.js";
|
|
23
|
+
import resolveValue from "./resolveValue.js";
|
|
24
|
+
export { type ChainedFunction, type DebounceOptions, type DebouncedFunction, type ThrottleOptions, type ThrottledFunction, createChainedFunction, debounce, get, hasValue, isArray, isArrayBuffer, isBlob, isBoolean, isFormData, isFunction, isNil, isNull, isNumber, isObject, isPlainObject, isPromise, isString, isSymbol, isURLSearchParams, isUndefined, parseJson, resolveValue, throttle };
|
package/dist/main.js
CHANGED
|
@@ -1,13 +1,24 @@
|
|
|
1
1
|
import { isArray } from "./isArray.js";
|
|
2
2
|
import { isBoolean } from "./isBoolean.js";
|
|
3
|
-
import { isFunction } from "./isFunction.js";
|
|
4
3
|
import { isUndefined } from "./isUndefined.js";
|
|
5
4
|
import { isNull } from "./isNull.js";
|
|
6
5
|
import { isNil } from "./isNil.js";
|
|
6
|
+
import hasValue from "./hasValue.js";
|
|
7
|
+
import { isBlob } from "./isBlob.js";
|
|
8
|
+
import { isFunction } from "./isFunction.js";
|
|
9
|
+
import { createChainedFunction } from "./createChainedFunction.js";
|
|
10
|
+
import { debounce } from "./debounce.js";
|
|
11
|
+
import { isFormData } from "./isFormData.js";
|
|
7
12
|
import { isNumber } from "./isNumber.js";
|
|
8
|
-
import { isString } from "./isString.js";
|
|
9
13
|
import { isObject } from "./isObject.js";
|
|
14
|
+
import { isPlainObject } from "./isPlainObject.js";
|
|
15
|
+
import { isString } from "./isString.js";
|
|
10
16
|
import { isPromise } from "./isPromise.js";
|
|
17
|
+
import { isArrayBuffer } from "./isArrayBuffer.js";
|
|
18
|
+
import { isURLSearchParams } from "./isURLSearchParams.js";
|
|
19
|
+
import { parseJson } from "./parseJson.js";
|
|
11
20
|
import { isSymbol } from "./isSymbol.js";
|
|
12
21
|
import { get } from "./get.js";
|
|
13
|
-
|
|
22
|
+
import { throttle } from "./throttle.js";
|
|
23
|
+
import resolveValue from "./resolveValue.js";
|
|
24
|
+
export { createChainedFunction, debounce, get, hasValue, isArray, isArrayBuffer, isBlob, isBoolean, isFormData, isFunction, isNil, isNull, isNumber, isObject, isPlainObject, isPromise, isString, isSymbol, isURLSearchParams, isUndefined, parseJson, resolveValue, throttle };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
//#region parseJson.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Parses JSON text into a value.
|
|
4
|
+
*
|
|
5
|
+
* Returns `undefined` for blank strings. When `fallback` is provided, invalid JSON
|
|
6
|
+
* returns the fallback instead of throwing.
|
|
7
|
+
*/
|
|
8
|
+
declare function parseJson<T>(text: string, fallback?: T): T | undefined;
|
|
9
|
+
//#endregion
|
|
10
|
+
export { parseJson };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
//#region parseJson.ts
|
|
2
|
+
/**
|
|
3
|
+
* Parses JSON text into a value.
|
|
4
|
+
*
|
|
5
|
+
* Returns `undefined` for blank strings. When `fallback` is provided, invalid JSON
|
|
6
|
+
* returns the fallback instead of throwing.
|
|
7
|
+
*/
|
|
8
|
+
function parseJson(text, fallback) {
|
|
9
|
+
if (!text.trim()) return;
|
|
10
|
+
try {
|
|
11
|
+
return JSON.parse(text);
|
|
12
|
+
} catch (error) {
|
|
13
|
+
if (arguments.length > 1) return fallback;
|
|
14
|
+
throw error;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
//#endregion
|
|
18
|
+
export { parseJson };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
//#region resolveValue.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Resolves either a plain value or a lazily evaluated value factory.
|
|
4
|
+
*
|
|
5
|
+
* @template TValue
|
|
6
|
+
* @template TArgs
|
|
7
|
+
* @param {TValue | ((...args: TArgs) => TValue)} value - The value or resolver.
|
|
8
|
+
* @param {...TArgs} args - Arguments passed to the resolver when needed.
|
|
9
|
+
* @returns {TValue} The resolved value.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* resolveValue('ready');
|
|
13
|
+
* resolveValue(status => status === 'error' ? 'retry' : 'ok', 'error');
|
|
14
|
+
*/
|
|
15
|
+
declare function resolveValue<TValue, TArgs extends any[]>(value: TValue | ((...args: TArgs) => TValue), ...args: TArgs): TValue;
|
|
16
|
+
//#endregion
|
|
17
|
+
export { resolveValue as default, resolveValue };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { isFunction } from "./isFunction.js";
|
|
2
|
+
//#region resolveValue.ts
|
|
3
|
+
/**
|
|
4
|
+
* Resolves either a plain value or a lazily evaluated value factory.
|
|
5
|
+
*
|
|
6
|
+
* @template TValue
|
|
7
|
+
* @template TArgs
|
|
8
|
+
* @param {TValue | ((...args: TArgs) => TValue)} value - The value or resolver.
|
|
9
|
+
* @param {...TArgs} args - Arguments passed to the resolver when needed.
|
|
10
|
+
* @returns {TValue} The resolved value.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* resolveValue('ready');
|
|
14
|
+
* resolveValue(status => status === 'error' ? 'retry' : 'ok', 'error');
|
|
15
|
+
*/
|
|
16
|
+
function resolveValue(value, ...args) {
|
|
17
|
+
if (isFunction(value)) return value(...args);
|
|
18
|
+
return value;
|
|
19
|
+
}
|
|
20
|
+
//#endregion
|
|
21
|
+
export { resolveValue as default, resolveValue };
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
//#region throttle.d.ts
|
|
2
|
+
interface ThrottleOptions {
|
|
3
|
+
/**
|
|
4
|
+
* An optional AbortSignal to cancel the throttled function.
|
|
5
|
+
*/
|
|
6
|
+
signal?: AbortSignal;
|
|
7
|
+
/**
|
|
8
|
+
* An optional array specifying whether the function should be invoked on the leading edge, trailing edge, or both.
|
|
9
|
+
* If `edges` includes "leading", the function will be invoked at the start of the delay period.
|
|
10
|
+
* If `edges` includes "trailing", the function will be invoked at the end of the delay period.
|
|
11
|
+
* If both "leading" and "trailing" are included, the function will be invoked at both the start and end of the delay period.
|
|
12
|
+
* @default ["leading", "trailing"]
|
|
13
|
+
*/
|
|
14
|
+
edges?: Array<'leading' | 'trailing'>;
|
|
15
|
+
}
|
|
16
|
+
interface ThrottledFunction<F extends (...args: any[]) => void> {
|
|
17
|
+
(...args: Parameters<F>): void;
|
|
18
|
+
cancel: () => void;
|
|
19
|
+
flush: () => void;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Creates a throttled function that only invokes the provided function at most once
|
|
23
|
+
* per every `throttleMs` milliseconds. Subsequent calls to the throttled function
|
|
24
|
+
* within the wait time will not trigger the execution of the original function.
|
|
25
|
+
*
|
|
26
|
+
* @template F - The type of function.
|
|
27
|
+
* @param {F} func - The function to throttle.
|
|
28
|
+
* @param {number} throttleMs - The number of milliseconds to throttle executions to.
|
|
29
|
+
* @returns {(...args: Parameters<F>) => void} A new throttled function that accepts the same parameters as the original function.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* const throttledFunction = throttle(() => {
|
|
33
|
+
* console.log('Function executed');
|
|
34
|
+
* }, 1000);
|
|
35
|
+
*
|
|
36
|
+
* // Will log 'Function executed' immediately
|
|
37
|
+
* throttledFunction();
|
|
38
|
+
*
|
|
39
|
+
* // Will not log anything as it is within the throttle time
|
|
40
|
+
* throttledFunction();
|
|
41
|
+
*
|
|
42
|
+
* // After 1 second
|
|
43
|
+
* setTimeout(() => {
|
|
44
|
+
* throttledFunction(); // Will log 'Function executed'
|
|
45
|
+
* }, 1000);
|
|
46
|
+
*/
|
|
47
|
+
declare function throttle<F extends (...args: any[]) => void>(func: F, throttleMs: number, {
|
|
48
|
+
signal,
|
|
49
|
+
edges
|
|
50
|
+
}?: ThrottleOptions): ThrottledFunction<F>;
|
|
51
|
+
//#endregion
|
|
52
|
+
export { ThrottleOptions, ThrottledFunction, throttle };
|
package/dist/throttle.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { isUndefined } from "./isUndefined.js";
|
|
2
|
+
import { debounce } from "./debounce.js";
|
|
3
|
+
//#region throttle.ts
|
|
4
|
+
/**
|
|
5
|
+
* Creates a throttled function that only invokes the provided function at most once
|
|
6
|
+
* per every `throttleMs` milliseconds. Subsequent calls to the throttled function
|
|
7
|
+
* within the wait time will not trigger the execution of the original function.
|
|
8
|
+
*
|
|
9
|
+
* @template F - The type of function.
|
|
10
|
+
* @param {F} func - The function to throttle.
|
|
11
|
+
* @param {number} throttleMs - The number of milliseconds to throttle executions to.
|
|
12
|
+
* @returns {(...args: Parameters<F>) => void} A new throttled function that accepts the same parameters as the original function.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* const throttledFunction = throttle(() => {
|
|
16
|
+
* console.log('Function executed');
|
|
17
|
+
* }, 1000);
|
|
18
|
+
*
|
|
19
|
+
* // Will log 'Function executed' immediately
|
|
20
|
+
* throttledFunction();
|
|
21
|
+
*
|
|
22
|
+
* // Will not log anything as it is within the throttle time
|
|
23
|
+
* throttledFunction();
|
|
24
|
+
*
|
|
25
|
+
* // After 1 second
|
|
26
|
+
* setTimeout(() => {
|
|
27
|
+
* throttledFunction(); // Will log 'Function executed'
|
|
28
|
+
* }, 1000);
|
|
29
|
+
*/
|
|
30
|
+
function throttle(func, throttleMs, { signal, edges = ["leading", "trailing"] } = {}) {
|
|
31
|
+
let pendingAt;
|
|
32
|
+
const debounced = debounce(function(...args) {
|
|
33
|
+
pendingAt = Date.now();
|
|
34
|
+
func.apply(this, args);
|
|
35
|
+
}, throttleMs, {
|
|
36
|
+
signal,
|
|
37
|
+
edges
|
|
38
|
+
});
|
|
39
|
+
const throttled = function(...args) {
|
|
40
|
+
if (isUndefined(pendingAt)) pendingAt = Date.now();
|
|
41
|
+
if (Date.now() - pendingAt >= throttleMs) {
|
|
42
|
+
pendingAt = Date.now();
|
|
43
|
+
func.apply(this, args);
|
|
44
|
+
debounced.cancel();
|
|
45
|
+
debounced.schedule();
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
debounced.apply(this, args);
|
|
49
|
+
};
|
|
50
|
+
throttled.cancel = () => {
|
|
51
|
+
pendingAt = void 0;
|
|
52
|
+
debounced.cancel();
|
|
53
|
+
};
|
|
54
|
+
throttled.flush = debounced.flush;
|
|
55
|
+
return throttled;
|
|
56
|
+
}
|
|
57
|
+
//#endregion
|
|
58
|
+
export { throttle };
|