UNPKG

@dimplesyj/util

Version:

[![NPM version](https://img.shields.io/npm/v/@dimplesyj/util?color=a1b858&label=)](https://www.npmjs.com/package/@dimplesyj/util) [![Docs](https://www.paka.dev/badges/v0/cute.svg)](https://www.paka.dev/npm/@dimplesyj/util)

647 lines (630 loc) 14.2 kB
export { debounce, throttle } from 'throttle-debounce'; /** * 移除数组中的某个元素 * * @category Array */ declare const removeAt: <T>(arr: T[], el: T) => void; /** * 将值插入到指定索引之后 * * @category Array * * @example * ``` * let otherArray = [2, 10]; * insertAt(otherArray, 0, 4, 6, 8); // otherArray = [2, 4, 6, 8, 10] * ``` * */ declare const insertAt: <T>(arr: T[], index: number, ...v: T[]) => T[]; /** * 返回数组中的最后一个元素 * * @category Array * * @example * ``` * last([1, 2, 3]); // 3 * last([]); // undefined * last(null); // undefined * last(undefined); // undefined * ``` */ declare const last: <T>(arr: T[]) => T | undefined; /** * 返回数组中的最后 n 个元素 * * @category Array * * @example lastN(['a', 'b', 'c', 'd'], 2); // ['c', 'd'] */ declare const lastN: <T>(arr: T[], n: number) => T[]; /** * 布尔全等判断 * * @category Array * * @example all([4, 2, 3], x => x > 1) => true */ declare function all(arr: unknown[], fn?: BooleanConstructor): boolean; /** * 检查数组各项相等 * * @category Array * * @example * ``` * allEqual([4, 4, 4]) => true * allEqual([4, 2, 3]) => false * ``` */ declare function allEqual(arr: unknown[]): boolean; /** * @category Object * * 深度拷贝对象 */ declare function clone(obj: object): object; /** * @category Object * * 清除对象中 undefined,null,[]空数组 */ declare function clearNull(obj: any): any; declare const hasOwn: (val: object, key: string | symbol) => key is never; declare const objectToString: () => string; /** * @category Object * * 对象类型 */ declare const toTypeString: (value: unknown) => string; /** * @category Object * * 比较一个值是否改变 */ declare const hasChanged: (value: any, oldValue: any) => boolean; /** * Function */ type Fn<T = void> = (...args: any[]) => T; /** * @category Promise * * 睡眠 * @param ms - 毫秒数 * @param callback - 回调函数 */ declare function sleep(ms: number, callback?: Fn<any>): Promise<void>; /** * @category String * * 驼峰化 * @param str - 字符串 * @example user-info => userInfo */ declare const camelize: (str: string) => string; /** * @category String * * 将字符串转换为 pascal * * @param str - 字符串 * @example user-info => UserInfo * @example some_database_field_name => SomeDatabaseFieldName * @example Some label that needs to be pascalized => SomeLabelThatNeedsToBePascalized * @example some-mixed_string with spaces_underscores-and-hyphens => SomeMixedStringWithSpacesUnderscoresAndHyphens */ declare const toPascalCase: (str: string) => string; /** * @category String * * 将字符串转换为 camel * @param str * @example hello-world => helloWorld * @example hello_world => helloWorld * @example hello world => helloWorld */ declare const toCamelCase: (str: string) => string; /** * @category String * 将字符串转换为 kebab * * @param str * * @example helloWorld => hello-world * @example hello_world => hello-world * @example hello world => hello-world */ declare const toKebabCase: (str: string) => string; /** * @category String * * 将字符串转换为 snake * * @param str * * @example helloWorld => hello_world * @example hello_world => hello_world * @example hello world => hello_world */ declare const toSnakeCase: (str: string) => string; /** * @category String * * 字符数组 * * @param s * @example hello => ['h', 'e', 'l', 'l', 'o'] */ declare const toCharArray: (s: string) => string[]; /** * @category String * * 首字母大写 * * @param str - 字符串 * @example userInfo => UserInfo */ declare const capitalize: (str: string) => string; /** * @category String * * 大写字母 转为 小写-连接 * * @param str - 字符串 * @example UserInfo => user-info */ declare const hyphenate: (str: string) => string; /** * @category String * * 替换所有相同字符串 * * @param text - 需要处理的字符串 * @param repstr - 被替换的字符 * @param newstr - 替换后的字符 */ declare function replaceAll(text: string, repstr: string, newstr: string): string; /** * @category String * * 去左右空格 * @param value - 需要处理的字符串 */ declare function trim(value: string): string; /** * @category String * * 去所有空格 * @param value - 需要处理的字符串 */ declare function trimAll(value: string): string; /** * @category String * * 根据数字获取对应的汉字 * @param num - 数字(0-10) */ declare function getHanByNumber(num: number): string; /** * @category String * * 插入字符串 * @param str - 原字符串 * @param start - 插入位置 * @param insertStr - 插入字符串 */ declare function insertStr(str: string, start: number, insertStr: string): string; /** * @category String * * 转义HTML字符 * @param str - 字符串 * @example '<a href="#">Me & you</a>' => '&lt;a href="#"&gt;Me &amp; you&lt;/a&gt;' */ declare function escapeHTML(str: string): string; /** * @category String * * 移除空格 * @param str - 字符串 * @example ' Hello \nWorld ' => 'Hello World' */ declare const removeWhitespace: (str: string) => string; /** * @category Version * * 对比 version 的版本号 * @param v1 - 版本号1 * @param v2 - 版本号2 * @returns -1: v1 < v2; 0: v1 = v2; 1: v1 > v2 */ declare function compareVersion(v1: string, v2: string): number; declare enum FormatType { toMinute = "YYYY-MM-DD HH:mm", toHour = "YYYY-MM-DD HH", toDay = "YYYY-MM-DD", toMonth = "YYYY-MM", toYear = "YYYY", toSecond = "YYYY-MM-DD HH:mm:ss" } type DateFormat = FormatType | string; type IDate = Date | string; /** * 格式化日期 * * @category Date * */ declare function formatDate(date?: IDate, format?: DateFormat): string; /** * 获取当前时间 * * @category Date */ declare function getNow(format?: DateFormat): string; /** * 获取月第一天 * * @category Date */ declare function getFirstDayOfMonth(date?: IDate, format?: DateFormat): string; /** * 获取月最后一天 * * @category Date */ declare function getLastDayOfMonth(date?: IDate, format?: DateFormat): string; /** * 获取整月 * * @category Date */ declare function getDaysOfMonth(date?: IDate, format?: DateFormat): string[]; /** * 获取上个月 * * @category Date */ declare function getDaysOfLastMonth(format?: FormatType): string[]; /** * 获取月第一天 到 现在 * * @category Date */ declare function getDaysToNowOfMonth(date?: IDate, format?: FormatType): string[]; /** * 获取年第一天 * * @category Date */ declare function getFirstDayOfYear(date?: IDate, format?: FormatType): string; /** * 本周 * * @category Date */ declare function getDaysOfWeek(format?: FormatType): string[]; /** * d2是否在d1之后 * * @category Date */ declare function isAfter(d1: IDate, d2?: IDate): boolean; /** * d2是否在d1之前 * * @category Date */ declare function isBefore(d1: IDate, d2?: IDate): boolean; /** * d3是否在d1与d2之间 * * @category Date */ declare function isBetween(d1: IDate, d2: IDate, d3?: IDate): boolean; /** * 加几天 * * @category Date */ declare function addDays(days?: number, d?: IDate, format?: FormatType): IDate; /** * 减几天 * * @category Date */ declare function subDays(days?: number, d?: IDate, format?: FormatType): IDate; /** * 转换成 Date * * @category Date */ declare function toDate(date: string | string[]): Date | Date[]; /** * @category RegExp * * 转义字符串以在正则表达式中使用 * @param string - 要转义的字符串 */ declare function escapeRegExp(string: string): string; /** * @category Number * * 转为数字 */ declare const toNumber: (val: any) => any; /** * @category Number * 保留小数点后面n位数字,四舍五入 * * @returns {string} */ declare const toFix: (value: string, n: number) => string; /** * @category Is * * 是否为数组 */ declare function isArray(obj: any): boolean; /** * @category Is * */ declare const isMap: (val: unknown) => val is Map<any, any>; /** * @category Is * */ declare const isSet: (val: unknown) => val is Set<any>; /** * @category Is * */ declare const isString: (val: unknown) => val is string; /** * @category Is * */ declare const isDate: (val: unknown) => val is Date; /** * @category Is * */ declare const isFunction: (val: unknown) => val is Fn; /** * @category Is * */ declare const isSymbol: (val: unknown) => val is symbol; /** * @category Is * */ declare const isObject: (val: unknown) => val is Record<any, any>; /** * @category Is * */ declare const isPromise: <T = any>(val: unknown) => val is Promise<T>; /** * @category Is * */ declare const isNumber: (val: any) => val is number; /** * @category Is * */ declare const isNull: (val: unknown) => val is null; /** * @category Is * */ declare const isUndefined: (val: unknown) => val is undefined; /** * @category Is * */ declare const isRegExp: (val: unknown) => val is RegExp; /** * @category Is * */ declare const isFile: (val: unknown) => val is File; /** * @category Is * * 是否为纯粹的对象 * * @example ``` * isObject([]) 是 true ,因为 type [] 为 'object' * isPlainObject([]) 则是false * ``` */ declare const isPlainObject: (val: unknown) => val is object; /** * @category Is * */ declare function isUndef(v: unknown): boolean; /** * 是否为空字符串 */ declare function isEmptyString(v: unknown): boolean; /** * @category Is * * 是否为空 * @example ``` * isEmpty(null) // true * isEmpty(undefined) // true * isEmpty('') // true * isEmpty([]) // true * isEmpty({}) // true * isEmpty(' ') // false * isEmpty(123) // true * ``` */ declare function isEmpty(val: any): boolean; /** * @category Misc * * 执行数组里的函数 */ declare const invokeArrayFns: (fns: Fn[], arg?: any) => void; /** * @category Misc * * 字符串哈希 */ declare const stringHash: (str: string) => number; /** * @category Misc * * uuid */ declare const uuid: () => string; /** * @category Misc * * nanoid */ declare const nanoid: (defaultSize?: number, alphabet?: string) => string; /** * @category Misc * * 手机号码中间4位隐藏星号 */ declare function hideMobile(mobile: string): string; /** * @category Misc * * 键值对拼接成URL参数 */ declare const params2Url: (obj: Record<string, any>) => string; /** * @category Misc * * 将总秒数转换成 时:分:秒 */ declare const seconds2Time: (seconds: number) => string; /** * @category Misc * * 将总秒数转换成 日:时:分:秒 */ declare const seconds2DayTime: (seconds: number) => string; /** * @category Misc * * 下载文件 * * @example downloadFile('http://www.baidu.com/img/bd_logo1.png', 'logo.png') */ declare function download(link: string, name?: string): void; /** * @category Misc * * 浏览器下载静态文件 * * @example downloadFile('1.json',JSON.stringify({name:'hahahha'})) * @example downloadFile('1.json',new Blob([ data ])) */ declare function downloadFile(name: string, content: any): void; interface IImageVerifyOptions { /** * canvas dom 对象 */ dom: HTMLCanvasElement; /** * canvas宽度 */ width: number; /** * canvas高度 */ height: number; } /** * 绘制图形验证码 * * @category Image * */ declare function drawImageVerify({ dom, width, height }: IImageVerifyOptions): string; /** * rgb转hex * * @category Color * */ declare function rgbToHex(r: string | number, g: string | number, b: string | number): string; /** * 数字转16进制 * * @category Color */ declare function toHex(n: string | number): string; /** * 十六进制颜色转RGB颜色 * * @category Color */ declare function hexToRGB(hex: string): { r: number; g: number; b: number; } | null; /** * @category Random * * 随机十六进制颜色 */ declare function randomHexColorCode(): string; /** * @category Random * * 随机 rgb 颜色 * @param min - 最小值 * @param max - 最大值 */ declare function randomRgbColor(min?: number, max?: number): string; /** * @category Random * * 随机布尔值 */ declare function randomBoolean(): boolean; /** * @category Random * * 生成指定范围的随机整数 * @param min - 最小值 * @param max - 最大值 */ declare function randomIntegerInRange(min: number, max: number): number; /** * @category Random * * 生成指定范围的随机小数 * @param min - 最小值 * @param max - 最大值 * * @example (0,5) => 3.0211363285087005 */ declare function randomNumberInRange(min: number, max: number): number; /** * @category Vendor * * @see https://github.com/scopsy/await-to-js/blob/master/src/await-to-js.ts * * @param { Readonly<Promise<T>> } promise * @param { Object } errorExt - Additional Information you can pass to the err object * @return { Promise } */ declare function to<T, U = Error>(promise: Readonly<Promise<T>>, errorExt?: object): Promise<[U, undefined] | [null, T]>; export { FormatType, addDays, all, allEqual, camelize, capitalize, clearNull, clone, compareVersion, download, downloadFile, drawImageVerify, escapeHTML, escapeRegExp, formatDate, getDaysOfLastMonth, getDaysOfMonth, getDaysOfWeek, getDaysToNowOfMonth, getFirstDayOfMonth, getFirstDayOfYear, getHanByNumber, getLastDayOfMonth, getNow, hasChanged, hasOwn, hexToRGB, hideMobile, hyphenate, insertAt, insertStr, invokeArrayFns, isAfter, isArray, isBefore, isBetween, isDate, isEmpty, isEmptyString, isFile, isFunction, isMap, isNull, isNumber, isObject, isPlainObject, isPromise, isRegExp, isSet, isString, isSymbol, isUndef, isUndefined, last, lastN, nanoid, objectToString, params2Url, randomBoolean, randomHexColorCode, randomIntegerInRange, randomNumberInRange, randomRgbColor, removeAt, removeWhitespace, replaceAll, rgbToHex, seconds2DayTime, seconds2Time, sleep, stringHash, subDays, to, toCamelCase, toCharArray, toDate, toFix, toHex, toKebabCase, toNumber, toPascalCase, toSnakeCase, toTypeString, trim, trimAll, uuid };