@omnific/utils 0.1.1 → 0.2.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.
@@ -8,6 +8,9 @@ type GetFieldTypeOfNarrowedByLKR<T, L, K, R> = '' extends L ? '' extends R ? Get
8
8
  type GetFieldTypeOfNarrowed<T, X, XT extends 'DotPath' | 'Key' | 'Path'> = XT extends 'Key' ? GetFieldTypeOfNarrowedByKey<T, X> : XT extends 'DotPath' ? GetFieldTypeOfNarrowedByDotPath<T, X> : X extends `${infer L}['${infer K}']${infer R}` ? GetFieldTypeOfNarrowedByLKR<T, L, K, R> : X extends `${infer L}["${infer K}"]${infer R}` ? GetFieldTypeOfNarrowedByLKR<T, L, K, R> : X extends `${infer L}[${infer K}]${infer R}` ? GetFieldTypeOfNarrowedByLKR<T, L, K, R> : GetFieldTypeOfNarrowedByDotPath<T, X>;
9
9
  type GetFieldTypeOfObject<T, X, XT extends 'DotPath' | 'Key' | 'Path'> = Extract<T, unknown[]> extends never ? GetFieldTypeOfNarrowed<T, X, XT> : GetFieldTypeOfNarrowed<Exclude<T, unknown[]>, X, XT> | GetFieldTypeOfNarrowed<Extract<T, unknown[]>, X, XT>;
10
10
  type GetFieldTypeOfPrimitive<T, X, XT extends 'DotPath' | 'Key' | 'Path'> = Extract<T, string> extends never ? T extends never ? never : undefined : (Exclude<T, string> extends never ? never : undefined) | GetFieldTypeOfNarrowed<Extract<T, string>, X, XT>;
11
+ /**
12
+ * 解析指定键、点路径或括号路径对应的值类型。
13
+ */
11
14
  type GetFieldType<T, X, XT extends 'DotPath' | 'Key' | 'Path' = 'Path'> = Extract<T, object> extends never ? GetFieldTypeOfPrimitive<T, X, XT> : GetFieldTypeOfPrimitive<Exclude<T, object>, X, XT> | GetFieldTypeOfObject<Extract<T, object>, X, XT>;
12
15
  //#endregion
13
16
  export { GetFieldType };
@@ -1,4 +1,7 @@
1
1
  //#region @types/Many.d.ts
2
+ /**
3
+ * 表示单个值或只读值列表。
4
+ */
2
5
  type Many<T> = T | readonly T[];
3
6
  //#endregion
4
7
  export { Many };
@@ -1,6 +1,9 @@
1
1
  import { Many } from "./Many.js";
2
2
 
3
3
  //#region @types/PropertyPath.d.ts
4
+ /**
5
+ * 用于对象路径访问的单个属性键或只读属性键列表。
6
+ */
4
7
  type PropertyPath = Many<PropertyKey>;
5
8
  //#endregion
6
9
  export { PropertyPath };
@@ -1,6 +1,9 @@
1
1
  import { isString } from "../isString.js";
2
2
  import { isSymbol } from "../isSymbol.js";
3
3
  //#region _internal/toKey.ts
4
+ /**
5
+ * 将属性路径片段规范化为字符串键。
6
+ */
4
7
  function normalizeKey(path) {
5
8
  return Object.is(path?.valueOf?.(), -0) ? "-0" : String(path);
6
9
  }
@@ -2,7 +2,7 @@
2
2
  /**
3
3
  * A function with a bound `this` context and variadic arguments.
4
4
  */
5
- type ChainedFunction<TArgs extends any[], TThis> = (this: TThis, ...args: TArgs) => void;
5
+ type ChainedFunction<TArguments extends any[], TThis> = (this: TThis, ...arguments_: TArguments) => void;
6
6
  /**
7
7
  * Composes multiple callbacks into a single callback while preserving `this`.
8
8
  *
@@ -14,6 +14,6 @@ type ChainedFunction<TArgs extends any[], TThis> = (this: TThis, ...args: TArgs)
14
14
  * @param {...(ChainedFunction<TArgs, TThis> | null | undefined)[]} funcs - The callbacks to chain.
15
15
  * @returns {ChainedFunction<TArgs, TThis>} A single chained callback.
16
16
  */
17
- declare function createChainedFunction<TArgs extends any[], TThis>(...funcs: Array<ChainedFunction<TArgs, TThis> | null | undefined>): ChainedFunction<TArgs, TThis>;
17
+ declare function createChainedFunction<TArguments extends any[], TThis>(...funcs: Array<ChainedFunction<TArguments, TThis> | null | undefined>): ChainedFunction<TArguments, TThis>;
18
18
  //#endregion
19
19
  export { ChainedFunction, createChainedFunction, createChainedFunction as default };
@@ -12,11 +12,11 @@ import { isFunction } from "./isFunction.js";
12
12
  * @returns {ChainedFunction<TArgs, TThis>} A single chained callback.
13
13
  */
14
14
  function createChainedFunction(...funcs) {
15
- const validFuncs = funcs.filter((fn) => isFunction(fn));
15
+ const validFuncs = funcs.filter((function_) => isFunction(function_));
16
16
  if (validFuncs.length === 0) return function() {};
17
17
  if (validFuncs.length === 1) return validFuncs[0];
18
- return function(...args) {
19
- for (const fn of validFuncs) fn.apply(this, args);
18
+ return function(...arguments_) {
19
+ for (const function_ of validFuncs) function_.apply(this, arguments_);
20
20
  };
21
21
  }
22
22
  //#endregion
@@ -1,4 +1,7 @@
1
1
  //#region debounce.d.ts
2
+ /**
3
+ * 控制防抖函数何时调用或取消的选项。
4
+ */
2
5
  interface DebounceOptions {
3
6
  /**
4
7
  * An optional AbortSignal to cancel the debounced function.
@@ -13,8 +16,11 @@ interface DebounceOptions {
13
16
  */
14
17
  edges?: Array<'leading' | 'trailing'>;
15
18
  }
16
- interface DebouncedFunction<F extends (...args: any[]) => void> {
17
- (...args: Parameters<F>): void;
19
+ /**
20
+ * 带有调度、取消和立即执行控制能力的防抖函数。
21
+ */
22
+ interface DebouncedFunction<F extends (...arguments_: any[]) => void> {
23
+ (...arguments_: Parameters<F>): void;
18
24
  /**
19
25
  * Schedules the execution of the debounced function after the specified debounce delay.
20
26
  * This method resets any existing timer, ensuring that the function is only invoked
@@ -41,7 +47,7 @@ interface DebouncedFunction<F extends (...args: any[]) => void> {
41
47
  * method to cancel any pending execution.
42
48
  *
43
49
  * @template F - The type of function.
44
- * @param {F} func - The function to debounce.
50
+ * @param {F} function_ - The function to debounce.
45
51
  * @param {number} debounceMs - The number of milliseconds to delay.
46
52
  * @param {DebounceOptions} options - The options object
47
53
  * @param {AbortSignal} options.signal - An optional AbortSignal to cancel the debounced function.
@@ -70,7 +76,7 @@ interface DebouncedFunction<F extends (...args: any[]) => void> {
70
76
  * // Will cancel the debounced function call
71
77
  * controller.abort();
72
78
  */
73
- declare function debounce<F extends (...args: any[]) => void>(func: F, debounceMs: number, {
79
+ declare function debounce<F extends (...arguments_: any[]) => void>(function_: F, debounceMs: number, {
74
80
  signal,
75
81
  edges
76
82
  }?: DebounceOptions): DebouncedFunction<F>;
package/dist/debounce.js CHANGED
@@ -8,7 +8,7 @@ import { isNil } from "./isNil.js";
8
8
  * method to cancel any pending execution.
9
9
  *
10
10
  * @template F - The type of function.
11
- * @param {F} func - The function to debounce.
11
+ * @param {F} function_ - The function to debounce.
12
12
  * @param {number} debounceMs - The number of milliseconds to delay.
13
13
  * @param {DebounceOptions} options - The options object
14
14
  * @param {AbortSignal} options.signal - An optional AbortSignal to cancel the debounced function.
@@ -37,7 +37,7 @@ import { isNil } from "./isNil.js";
37
37
  * // Will cancel the debounced function call
38
38
  * controller.abort();
39
39
  */
40
- function debounce(func, debounceMs, { signal, edges } = {}) {
40
+ function debounce(function_, debounceMs, { signal, edges } = {}) {
41
41
  let pendingInvoke;
42
42
  const leading = hasValue(edges) && edges.includes("leading");
43
43
  const trailing = isUndefined(edges) || edges.includes("trailing");
@@ -71,10 +71,10 @@ function debounce(func, debounceMs, { signal, edges } = {}) {
71
71
  cancelTimer();
72
72
  invoke();
73
73
  };
74
- const debounced = function(...args) {
74
+ const debounced = function(...arguments_) {
75
75
  if (signal?.aborted) return;
76
76
  pendingInvoke = () => {
77
- func.apply(this, args);
77
+ function_.apply(this, arguments_);
78
78
  };
79
79
  const isFirstCall = isUndefined(timeoutId);
80
80
  schedule();
@@ -12,6 +12,6 @@
12
12
  * isFunction(Proxy); // true
13
13
  * isFunction(Int8Array); // true
14
14
  */
15
- declare function isFunction(value: any): value is (...args: any[]) => any;
15
+ declare function isFunction(value: any): value is (...arguments_: any[]) => any;
16
16
  //#endregion
17
17
  export { isFunction };
@@ -45,8 +45,8 @@ import { isObject } from "./isObject.js";
45
45
  */
46
46
  function isPlainObject(value) {
47
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;
48
+ const prototype = Object.getPrototypeOf(value);
49
+ if (!(isNull(prototype) || prototype === Object.prototype || isNull(Object.getPrototypeOf(prototype)))) return false;
50
50
  return Object.prototype.toString.call(value) === "[object Object]";
51
51
  }
52
52
  //#endregion
@@ -1,4 +1,4 @@
1
- //#region isURLSearchParams.d.ts
1
+ //#region isURLSearchParameters.d.ts
2
2
  /**
3
3
  * Checks if a given value is `URLSearchParams`.
4
4
  *
@@ -12,10 +12,10 @@
12
12
  * const value2 = new FormData();
13
13
  * const value3 = 'a=1';
14
14
  *
15
- * console.log(isURLSearchParams(value1)); // true
16
- * console.log(isURLSearchParams(value2)); // false
17
- * console.log(isURLSearchParams(value3)); // false
15
+ * console.log(isURLSearchParameters(value1)); // true
16
+ * console.log(isURLSearchParameters(value2)); // false
17
+ * console.log(isURLSearchParameters(value3)); // false
18
18
  */
19
- declare function isURLSearchParams(value: unknown): value is URLSearchParams;
19
+ declare function isURLSearchParameters(value: unknown): value is URLSearchParams;
20
20
  //#endregion
21
- export { isURLSearchParams };
21
+ export { isURLSearchParameters };
@@ -1,5 +1,5 @@
1
1
  import hasValue from "./hasValue.js";
2
- //#region isURLSearchParams.ts
2
+ //#region isURLSearchParameters.ts
3
3
  /**
4
4
  * Checks if a given value is `URLSearchParams`.
5
5
  *
@@ -13,12 +13,12 @@ import hasValue from "./hasValue.js";
13
13
  * const value2 = new FormData();
14
14
  * const value3 = 'a=1';
15
15
  *
16
- * console.log(isURLSearchParams(value1)); // true
17
- * console.log(isURLSearchParams(value2)); // false
18
- * console.log(isURLSearchParams(value3)); // false
16
+ * console.log(isURLSearchParameters(value1)); // true
17
+ * console.log(isURLSearchParameters(value2)); // false
18
+ * console.log(isURLSearchParameters(value3)); // false
19
19
  */
20
- function isURLSearchParams(value) {
20
+ function isURLSearchParameters(value) {
21
21
  return hasValue(value) && value instanceof URLSearchParams;
22
22
  }
23
23
  //#endregion
24
- export { isURLSearchParams };
24
+ export { isURLSearchParameters };
package/dist/main.d.ts CHANGED
@@ -16,10 +16,10 @@ import { isPlainObject } from "./isPlainObject.js";
16
16
  import { isPromise } from "./isPromise.js";
17
17
  import { isString } from "./isString.js";
18
18
  import { isSymbol } from "./isSymbol.js";
19
- import { isURLSearchParams } from "./isURLSearchParams.js";
19
+ import { isURLSearchParameters } from "./isURLSearchParameters.js";
20
20
  import { isUndefined } from "./isUndefined.js";
21
21
  import { parseJson } from "./parseJson.js";
22
22
  import { ThrottleOptions, ThrottledFunction, throttle } from "./throttle.js";
23
23
  import resolveValue from "./resolveValue.js";
24
24
  import { uniqueId } from "./uniqueId.js";
25
- 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, uniqueId };
25
+ 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, isURLSearchParameters, isURLSearchParameters as isURLSearchParams, isUndefined, parseJson, resolveValue, throttle, uniqueId };
package/dist/main.js CHANGED
@@ -15,11 +15,11 @@ import { isPlainObject } from "./isPlainObject.js";
15
15
  import { isString } from "./isString.js";
16
16
  import { isPromise } from "./isPromise.js";
17
17
  import { isArrayBuffer } from "./isArrayBuffer.js";
18
- import { isURLSearchParams } from "./isURLSearchParams.js";
18
+ import { isURLSearchParameters } from "./isURLSearchParameters.js";
19
19
  import { parseJson } from "./parseJson.js";
20
20
  import { isSymbol } from "./isSymbol.js";
21
21
  import { get } from "./get.js";
22
22
  import { throttle } from "./throttle.js";
23
23
  import resolveValue from "./resolveValue.js";
24
24
  import { uniqueId } from "./uniqueId.js";
25
- export { createChainedFunction, debounce, get, hasValue, isArray, isArrayBuffer, isBlob, isBoolean, isFormData, isFunction, isNil, isNull, isNumber, isObject, isPlainObject, isPromise, isString, isSymbol, isURLSearchParams, isUndefined, parseJson, resolveValue, throttle, uniqueId };
25
+ export { createChainedFunction, debounce, get, hasValue, isArray, isArrayBuffer, isBlob, isBoolean, isFormData, isFunction, isNil, isNull, isNumber, isObject, isPlainObject, isPromise, isString, isSymbol, isURLSearchParameters, isURLSearchParameters as isURLSearchParams, isUndefined, parseJson, resolveValue, throttle, uniqueId };
@@ -12,6 +12,6 @@
12
12
  * resolveValue('ready');
13
13
  * resolveValue(status => status === 'error' ? 'retry' : 'ok', 'error');
14
14
  */
15
- declare function resolveValue<TValue, TArgs extends any[]>(value: TValue | ((...args: TArgs) => TValue), ...args: TArgs): TValue;
15
+ declare function resolveValue<TValue, TArguments extends any[]>(value: TValue | ((...arguments_: TArguments) => TValue), ...arguments_: TArguments): TValue;
16
16
  //#endregion
17
17
  export { resolveValue as default, resolveValue };
@@ -13,8 +13,8 @@ import { isFunction } from "./isFunction.js";
13
13
  * resolveValue('ready');
14
14
  * resolveValue(status => status === 'error' ? 'retry' : 'ok', 'error');
15
15
  */
16
- function resolveValue(value, ...args) {
17
- if (isFunction(value)) return value(...args);
16
+ function resolveValue(value, ...arguments_) {
17
+ if (isFunction(value)) return value(...arguments_);
18
18
  return value;
19
19
  }
20
20
  //#endregion
@@ -1,4 +1,7 @@
1
1
  //#region throttle.d.ts
2
+ /**
3
+ * 控制节流函数何时调用或取消的选项。
4
+ */
2
5
  interface ThrottleOptions {
3
6
  /**
4
7
  * An optional AbortSignal to cancel the throttled function.
@@ -13,8 +16,11 @@ interface ThrottleOptions {
13
16
  */
14
17
  edges?: Array<'leading' | 'trailing'>;
15
18
  }
16
- interface ThrottledFunction<F extends (...args: any[]) => void> {
17
- (...args: Parameters<F>): void;
19
+ /**
20
+ * 带有取消和立即执行控制能力的节流函数。
21
+ */
22
+ interface ThrottledFunction<F extends (...arguments_: any[]) => void> {
23
+ (...arguments_: Parameters<F>): void;
18
24
  cancel: () => void;
19
25
  flush: () => void;
20
26
  }
@@ -24,7 +30,7 @@ interface ThrottledFunction<F extends (...args: any[]) => void> {
24
30
  * within the wait time will not trigger the execution of the original function.
25
31
  *
26
32
  * @template F - The type of function.
27
- * @param {F} func - The function to throttle.
33
+ * @param {F} function_ - The function to throttle.
28
34
  * @param {number} throttleMs - The number of milliseconds to throttle executions to.
29
35
  * @returns {(...args: Parameters<F>) => void} A new throttled function that accepts the same parameters as the original function.
30
36
  *
@@ -44,7 +50,7 @@ interface ThrottledFunction<F extends (...args: any[]) => void> {
44
50
  * throttledFunction(); // Will log 'Function executed'
45
51
  * }, 1000);
46
52
  */
47
- declare function throttle<F extends (...args: any[]) => void>(func: F, throttleMs: number, {
53
+ declare function throttle<F extends (...arguments_: any[]) => void>(function_: F, throttleMs: number, {
48
54
  signal,
49
55
  edges
50
56
  }?: ThrottleOptions): ThrottledFunction<F>;
package/dist/throttle.js CHANGED
@@ -7,7 +7,7 @@ import { debounce } from "./debounce.js";
7
7
  * within the wait time will not trigger the execution of the original function.
8
8
  *
9
9
  * @template F - The type of function.
10
- * @param {F} func - The function to throttle.
10
+ * @param {F} function_ - The function to throttle.
11
11
  * @param {number} throttleMs - The number of milliseconds to throttle executions to.
12
12
  * @returns {(...args: Parameters<F>) => void} A new throttled function that accepts the same parameters as the original function.
13
13
  *
@@ -27,25 +27,25 @@ import { debounce } from "./debounce.js";
27
27
  * throttledFunction(); // Will log 'Function executed'
28
28
  * }, 1000);
29
29
  */
30
- function throttle(func, throttleMs, { signal, edges = ["leading", "trailing"] } = {}) {
30
+ function throttle(function_, throttleMs, { signal, edges = ["leading", "trailing"] } = {}) {
31
31
  let pendingAt;
32
- const debounced = debounce(function(...args) {
32
+ const debounced = debounce(function(...arguments_) {
33
33
  pendingAt = Date.now();
34
- func.apply(this, args);
34
+ function_.apply(this, arguments_);
35
35
  }, throttleMs, {
36
36
  signal,
37
37
  edges
38
38
  });
39
- const throttled = function(...args) {
39
+ const throttled = function(...arguments_) {
40
40
  if (isUndefined(pendingAt)) pendingAt = Date.now();
41
41
  if (Date.now() - pendingAt >= throttleMs) {
42
42
  pendingAt = Date.now();
43
- func.apply(this, args);
43
+ function_.apply(this, arguments_);
44
44
  debounced.cancel();
45
45
  debounced.schedule();
46
46
  return;
47
47
  }
48
- debounced.apply(this, args);
48
+ debounced.apply(this, arguments_);
49
49
  };
50
50
  throttled.cancel = () => {
51
51
  pendingAt = void 0;
package/dist/uniqueId.js CHANGED
@@ -1,5 +1,5 @@
1
1
  //#region uniqueId.ts
2
- let idCounter = 0;
2
+ const uniqueIdCounter = { value: 0 };
3
3
  /**
4
4
  * Generates a unique ID.
5
5
  *
@@ -14,7 +14,7 @@ let idCounter = 0;
14
14
  * uniqueId('contact_'); // 'contact_3'
15
15
  */
16
16
  function uniqueId(prefix = "") {
17
- return `${prefix}${++idCounter}`;
17
+ return `${prefix}${++uniqueIdCounter.value}`;
18
18
  }
19
19
  //#endregion
20
20
  export { uniqueId };
@@ -28,7 +28,7 @@ function toPath(deepKey) {
28
28
  let index = 0;
29
29
  let key = "";
30
30
  let quoteChar = "";
31
- let bracket = false;
31
+ let isBracket = false;
32
32
  if (deepKey.codePointAt(0) === 46) {
33
33
  result.push("");
34
34
  index++;
@@ -40,14 +40,14 @@ function toPath(deepKey) {
40
40
  key += deepKey[index];
41
41
  } else if (char === quoteChar) quoteChar = "";
42
42
  else key += char;
43
- else if (bracket) if (char === "\"" || char === "'") quoteChar = char;
43
+ else if (isBracket) if (char === "\"" || char === "'") quoteChar = char;
44
44
  else if (char === "]") {
45
- bracket = false;
45
+ isBracket = false;
46
46
  result.push(key);
47
47
  key = "";
48
48
  } else key += char;
49
49
  else if (char === "[") {
50
- bracket = true;
50
+ isBracket = true;
51
51
  if (key) {
52
52
  result.push(key);
53
53
  key = "";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@omnific/utils",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "Shared utility predicates for Omnific packages",
5
5
  "license": "MIT",
6
6
  "type": "module",