UNPKG

@ryanuo/utils

Version:

提供多种实用工具函数,涵盖算法、浏览器操作、网络请求等多个领域

79 lines (78 loc) 4.07 kB
/** * Generate a unique UUID * UUID (Universally Unique Identifier) is a standard used in distributed systems to uniquely identify information * This function implements a simple UUID generation algorithm, primarily used to generate unique identifiers for use in applications * @example * ```ts twoslash * import { getUuid } from '@ryanuo/utils' * console.log(getUuid()) // xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx * ``` * @returns {string} The generated UUID string in the format 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx' */ export declare function getUuid(): string; /** * @ignore */ export type Curry<F extends (...args: any[]) => any> = F extends (...args: infer A) => infer R ? A extends [] ? R : A extends [infer First, ...infer Rest] ? ((arg: First) => Curry<(...args: Rest) => R>) & ((...args: A) => R) : R : never; /** * Converts a function into a curried function. * The feature of a curried function is that it can receive one or more arguments, * and returns a new function until all required arguments are received, then executes the original function. * @example * ```ts twoslash * import { curry } from '@ryanuo/utils' * const add = (a: number, b: number) => a + b * const curriedAdd = curry(add) * console.log(curriedAdd(1)(2)) // 3 * console.log(curriedAdd(1, 2)) // 3 * ``` * @param fn The function to be converted; can be any type of function. * @returns Returns a curried function capable of gradually receiving arguments until the original function's requirements are satisfied. */ export declare function curry<F extends (...args: any[]) => any>(fn: F): Curry<F>; /** * Safely parses a JSON string * @example * ```ts twoslash * import { safeJSONParse } from '@ryanuo/utils' * const json = '{"name": "John", "age": 30}' * const obj = safeJSONParse(json) * console.log(obj) // { name: 'John', age: 30 } * ``` * @param json The JSON string to be parsed * @returns A successfully parsed JSON object, or null if parsing fails */ export declare function safeJSONParse(json: string): any; /** * Function debouncing * A debouncing function is used to limit the frequency of executing a function within a specified time frame, preventing it from being called too frequently. * If the function is called again within the specified interval, the previous call will be canceled and the timer will reset. * @example * ```ts twoslash * import { debounce } from '@ryanuo/utils' * const debouncedFn = debounce(() => { * console.log('Debounced function executed') * }, 500, true) * debouncedFn() * ``` * @param fn The function to be debounced. * @param delay The delay in milliseconds within which repeated calls to the function will reset the timer. * @param immediate A boolean indicating whether the function should execute immediately on the first call. If set to true, the function executes at the start of the wait period; if false, it executes after the wait time following the last call. * @returns Returns a new debounced function. */ export declare function debounce<T extends (...args: any[]) => any>(fn: T, delay: number, immediate?: boolean): (...args: Parameters<T>) => void; /** * Creates a throttled function that only executes the original function at most once per `delay` milliseconds. * If `immediate` is true, the original function will be executed immediately upon the first call within the `delay` period. * @example * ```ts twoslash * import { throttle } from '@ryanuo/utils' * throttle(() => console.log('Hello, world!'), 1000, true) * throttle(() => console.log('Hello, world!'), 1000, false) * ``` * @param fn The original function to be throttled. * @param delay The minimum interval in milliseconds between executions of the original function. * @param immediate Whether to execute the original function immediately upon the first call within the `delay` period. Defaults to true. * @returns Returns a new throttled function. */ export declare function throttle<T extends (...args: any[]) => any>(fn: T, delay: number, immediate?: boolean): (...args: Parameters<T>) => void;