@omnific/utils 0.0.3 → 0.0.4
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/hasValue.d.ts +20 -0
- package/dist/hasValue.js +23 -0
- package/dist/main.d.ts +4 -1
- package/dist/main.js +4 -1
- package/dist/resolveValue.d.ts +17 -0
- package/dist/resolveValue.js +21 -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,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 };
|
package/dist/main.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import { ChainedFunction, createChainedFunction } from "./createChainedFunction.js";
|
|
1
2
|
import { get } from "./get.js";
|
|
3
|
+
import hasValue from "./hasValue.js";
|
|
2
4
|
import { isArray } from "./isArray.js";
|
|
3
5
|
import { isBoolean } from "./isBoolean.js";
|
|
4
6
|
import { isFunction } from "./isFunction.js";
|
|
@@ -10,4 +12,5 @@ import { isPromise } from "./isPromise.js";
|
|
|
10
12
|
import { isString } from "./isString.js";
|
|
11
13
|
import { isSymbol } from "./isSymbol.js";
|
|
12
14
|
import { isUndefined } from "./isUndefined.js";
|
|
13
|
-
|
|
15
|
+
import resolveValue from "./resolveValue.js";
|
|
16
|
+
export { type ChainedFunction, createChainedFunction, get, hasValue, isArray, isBoolean, isFunction, isNil, isNull, isNumber, isObject, isPromise, isString, isSymbol, isUndefined, resolveValue };
|
package/dist/main.js
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
import { isArray } from "./isArray.js";
|
|
2
2
|
import { isBoolean } from "./isBoolean.js";
|
|
3
3
|
import { isFunction } from "./isFunction.js";
|
|
4
|
+
import { createChainedFunction } from "./createChainedFunction.js";
|
|
4
5
|
import { isUndefined } from "./isUndefined.js";
|
|
5
6
|
import { isNull } from "./isNull.js";
|
|
6
7
|
import { isNil } from "./isNil.js";
|
|
8
|
+
import hasValue from "./hasValue.js";
|
|
7
9
|
import { isNumber } from "./isNumber.js";
|
|
8
10
|
import { isString } from "./isString.js";
|
|
9
11
|
import { isObject } from "./isObject.js";
|
|
10
12
|
import { isPromise } from "./isPromise.js";
|
|
11
13
|
import { isSymbol } from "./isSymbol.js";
|
|
12
14
|
import { get } from "./get.js";
|
|
13
|
-
|
|
15
|
+
import resolveValue from "./resolveValue.js";
|
|
16
|
+
export { createChainedFunction, get, hasValue, isArray, isBoolean, isFunction, isNil, isNull, isNumber, isObject, isPromise, isString, isSymbol, isUndefined, resolveValue };
|
|
@@ -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 };
|