/** * 一些不好分类的工具函数 * * @filename packages/utils/src/utils/utils.ts * @author Mr Prince * @date 2023-04-20 09:50:23 */ import { GeneralFunction } from '../interfaces'; /** * 处理url请求参数 * 需要手动加? * * @public */ export declare const objectToUrlParams: (params: object) => string; /** * 请求参数转对象 * * @public */ export declare const urlParamsToObject: (urlParams: string) => { [key: string]: string; }; /** * 防抖 * * @public */ export declare function debounce(callback: Function, timeout: number): Function; declare type Options = { leading: boolean; timeout: number; }; /** * 节流 * * @public */ export declare function throttle(callback: Function, options?: Options): (...params: any[]) => any; /** * 立即执行节流 * * @public */ export declare function leading(callback: Function, timeout: number): (...params: any[]) => any; /** * 延迟节流 * * @public */ export declare function trailing(callback: Function, timeout: number): (...params: any[]) => any; /** * 是否是整数 * * @public */ export declare function isInteger(number: any): boolean; /** * 获取对象上的key * * @public * @param object - 目标对象 * @param unenumerable - 是否获取全部的key,包括不可枚举的 * @returns - 如果是null 或 undefined 或 非对象类型的 返回空字符串 */ export declare const keys: (object: Object, unenumerable?: boolean) => string[]; /** * 获取对象的所有 symbol 属性 * * @public */ export declare const symbols: (object: Object) => symbol[]; /** * 矩阵转置 不需要行列相等 * * @public */ export declare const matrixTransposition: (arr: T[][]) => T[][]; /** * rgb 转 hex * * @public */ export declare const rgbToHex: (r: number, g: number, b: number) => string; /** * 尾递归优化 * 需要传入的函数最后返回一个新的函数或非函数的结果 * * @public */ export declare const trampoline: (fn: T) => T; /** * 精度转换 * 超过 Number.MAX_SAFE_INTEGER 后无法处理 * * @public * @description 如果 fractionDigits 为小数,向下取整,不支持 BigInt * @param number 要转换的数值 * @param fractionDigits 小数点后位数, 负数会将整数部分改成0 */ export declare const fixed: (number: number, fractionDigits: number) => string; /** * 获取全局对象 * * @public */ export declare const getGlobalThis: () => any; /** * 深层比较两个值是否相等 * * @public */ export declare function deepEqual(a: unknown, b: unknown): boolean; /** * 获取嵌套对象或数组中的值 * * @public */ export declare function getNestValue(values: unknown, indexs: number[], defaultValue?: T): T | undefined; /** * 判断是否是短路径 * * 不以 http|https|// 开头的路径 * * @public */ export declare function isShortPath(path: string): boolean; /** * 尝试调用 * * @public */ export declare function attempt any>(fn: T, ...args: Parameters): ReturnType | Error; /** * 解析URI * * [协议名]://[用户名]:[密码]@[主机名]:[端口]/[路径]?[查询参数]#[片段ID] * * 如果端口不是protocol 的默认端口 * host需要加上对应的端口号, 否则不用 * * [] 内部不能出现 [@:/?#] 等字符,否则解析可能会出现问题 * * @public */ export declare function parseURI(uri: string): { protocol: string; username: string; password: string; hostname: string; port: string; host: string; pathname: string; search: string; hash: string; href: string; origin: string; }; /** * 全排列 * 要求输入数据各不相同 * * @public */ export declare function permute(values: T[]): T[][]; /** * json转化 * * @public */ export declare function jsonStringify(object: T): string; /** * 缓存函数 */ export declare function memoize any>(fn: T): T; /** * 比上面更高效的缓存函数 */ export declare function linearMemoize any>(fn: T): T; /** * 对象 diff */ export declare function objDiff(obj1: any, obj2: any): any; /** * bem 模块名工具 */ export declare const createBEM: (block: string) => (element?: string, modifier?: string) => string; /** * 能够重复调用的函数 * * 重复调用不再次执行,直接返回上一次的执行结果 */ export declare const reuse: (func: T) => T; /** * 懒初始化 * 一个事件依赖另外一个数据 * 但是数据是通过异步加载,两者之间比较难建立联系 * * @example * * const [promise,resolve, reject] = lazyInit(); * * promise.then(data => { * console.log(data); * }); * * resolve(1); */ export declare const lazyInit: () => [Promise, (value: T) => void, (reason?: any) => void]; /** * 是否是双击事件 * * @param delta 两次事件间隔在 delta 内 返回 true */ export declare const isDbclick: (delta?: number) => () => boolean; /** * 极简加密 */ export declare const rot13: (content: string) => string; /** * Vigenère cipher */ export declare const vigenere: (plaintext: string, key: string) => string; /** * 判断是否是手机号 * * 中国的手机号一般是 11 位 */ export declare const isPhoneNumber: (numbers: string) => boolean; /** * 判断是否是 2 的 n 次幂 */ export declare const isPowerOfTwo: (value: number) => boolean; export declare const getEnglishAlphabet: () => string[]; export declare const getUppercaseEnglishAlphabet: () => string[]; /** * @param shift 偏移 */ export declare const getCipherMap: (alphabet: string[], shift: number) => Record; /** * caesar cipher */ export declare const caesarCipherEncrypt: (str: string, shift: number, alphabet?: string[]) => string; export declare const caesarCipherDecrypt: (str: string, shift: number, alphabet?: string[]) => string; export declare const generateKeyMatrix: (key: string) => unknown[]; /** * 根据消息内容生成矩阵 */ export declare const generateMessageVector: (message: string) => unknown[]; export declare const hillCipherEncrypt: (message: string, key: string) => string; /** * end of hill cipher */ export declare class PolynomialHash { private static readonly DEFAULT_BASE; private static readonly DEFAULT_MODULUS; base: number; modules: number; constructor({ base, modules, }: { base?: number; modules?: number; }); hash(word: string): number; roll(prevHash: number, prevWord: string, newWord: string): number; private charToNumber; } /** * demo 不能直接使用 */ export declare class SimplePolynomialHash { private base; private static readonly DEFAULT_BASE; constructor(base?: number); hash(word: string): number; roll(prevHash: number, prevWord: string, newWord: string): number; } /** * 简单实现 */ export declare class BloomFilter { private size; storage: ReturnType; constructor(size?: number); insert(key: string): void; contains(key: string): boolean; private createStore; private hash1; private hash2; private hash3; private getHashValues; } /** * 矩阵的 shape */ export declare const shape: (matrix: unknown[][]) => number[]; /** * 检查是否是一个矩阵 */ export declare const validateType: (matrix: unknown[][]) => void; export declare const validate2D: (matrix: unknown[][]) => void; export declare const validateSameShape: (a: unknown[][], b: unknown[][]) => void; /** * 矩阵生成 */ export declare const generate: (msp: number[], fill: (index: number[]) => number) => unknown[]; export declare const zeros: (msp: number[]) => unknown[]; /** * 矩阵叉乘 */ export declare const dot: (a: number[][], b: number[][]) => number[][]; /** * 矩阵遍历 */ export declare const walk: (matrix: unknown[][], visit: (indexs: number[], value: unknown) => void) => void; /** * 获取指定位置的值 */ export declare const getCellAtIndex: (matrix: unknown[][], cellIndices: number[]) => any; /** * 更新指定位置的值 */ export declare const updateCellAtIndex: (matrix: unknown[][], cellIndices: number[], value: unknown) => void; /** * 两个矩阵对应位置相加 */ export declare const add: (a: number[][], b: number[][]) => any; export declare const mul: (a: number[][], b: number[][]) => any; export declare const sub: (a: number[][], b: number[][]) => any; export declare class RailFenceCipher { private static readonly DIRECTIONS; private buildFence; private getNextDirection; private addCharToRail; private fillEncodeFence; private fillDecodeFence; decodeFence({ strLen, fence, currentRail, direction, code, }: { railCount?: number; strLen: number; fence: string[][]; currentRail: number; direction: number; code: string[]; }): string; encodeRailFenceCipher(message: string, railCount: number): string; decodeRailFenceCipher(message: string, railCount: number): string; } declare type Callbacks = { allowTraversal?: (vertices: { currentVertex: Vertex; nextVertex: Vertex; previousVertex: Vertex; }) => boolean; enterVeter?: (vertices: { currentVertex?: Vertex; nextVertex?: Vertex; previousVertex?: Vertex; }) => void; leaveVertex?: (vertices: { currentVertex?: Vertex; nextVertex?: Vertex; previousVertex?: Vertex; }) => void; }; export declare const initCallbacks: (callbacks?: Callbacks) => Callbacks; declare class Vertex { getKey(): string; } interface Graph { } export declare const dfsRecursive: (graph: Graph, currentVertex: Vertex, previousVertex: Vertex, callbacks: Required) => void; /** * 牛顿法计算给定精度的数字的平方根 * @param tolerance - 小数点后的有效位数 */ export declare const squareRoot: (value: number, tolerance?: number) => number; export declare const degreeToRadian: (degree: number) => number; export declare const radianToDegree: (radian: number) => number; /** * 二进制宽度 */ export declare const bitWidth: (value: number) => number; /** * a -> b 需要改变的位数 */ export declare const bitsDiff: (a: number, b: number) => number; /** * 二进制表示中有多少个1 */ export declare const bit1Count: (value: number) => number; export declare const clearBit: (value: number, bitPosotion: number) => number; export declare const divideByTow: (value: number) => number; export declare const getBit: (value: number, bitPosition: number) => number; export declare const setBit: (value: number, bitPosition: number) => number; export declare const updateBit: (value: number, bitPosition: number, bitValue: number) => number; /** * 汉明距离 */ export declare const hammingDistance: (a: string, b: string) => number; export {};