UNPKG

ts-api-core

Version:

Nodejs api framework core

139 lines (138 loc) 6.97 kB
declare global { interface Number { /** 转换为字符串,并保持指定最小长度,不够长度时在前面自动补0, `len` 参数最值为 `20` */ toPrefixString: (len: number) => string; } interface String { /** 是否为空, '' 和 undefined 、 null 都返回 true */ isEmpty: () => boolean; toInt: (defaultValue?: number) => number; toFloat: (defaultValue?: number) => number; toArray: () => any[]; } interface Date { /** 日期时间格式化,默认为 `YYYY-MM-DD HH:mm:ss` */ format: (format?: string) => string; /** 增加秒数 */ incSecond: (hourNum: number) => Date; /** 增加秒数 */ incMinutes: (hourNum: number) => Date; /** 增加小时 */ incHours: (hourNum: number) => Date; /** 增加天数 */ incDay: (dayNum: number) => Date; } interface Array<T> { /** 数组去重 */ distinct: (fieldKey?: string) => T[]; /** 数组转换 */ convert: <E>(newItem: (item: T) => E, destArray?: E[]) => E[]; } interface Function { random: (len?: number) => string; randomCode: (len?: number) => string; str: (value?: string | number | any) => string; toDate: (value: any | undefined, defaultValue?: Date | number | string) => Date; } } /** 公共函数和方法 */ export declare class Utils { /** * MD5加密 * @returns * */ static md5(password: string): string; /** 当前时间戳, 毫秒 */ static currentTimeMillis(): number; /** 将日期转为时间戳(毫秒),不传参数时返回当前时间戳 */ static getTime(date?: Date | number | string | undefined): number; /** 延迟多少毫秒继续执行 */ static sleep<T>(ms: number, data?: T | undefined): Promise<T | undefined>; /** 获取ip地址 */ static getIPAddress(): string | undefined; /** 获取枚举值列表 */ static enumValues(enumType: any): (number | string)[]; /** 获取枚举Key列表 */ static enumKeys(enumType: any): string[]; /** 获取 uuid */ static uuid(): string; private static readonly _num2; private static readonly _twoNum; /** 格式化日期时间, YYYY-MM-DD HH:mm:ss.SSS */ static formatDateTime(time: number | Date, haveMilliseconds?: boolean, invalidDateValue?: string): string; /** 格式化日期时间, HH:mm:ss.SSS */ static formatTime(time: number | Date, haveMilliseconds?: boolean): string; static getNowString(haveMilliseconds?: boolean): string; static getTimeString(haveMilliseconds?: boolean): string; /** 判断两个时间相差的小时数 */ static hourBetween(date1: Date | number, date2: Date | number): number; /** 判断两个时间相差的分钟数 */ static minutesBetween(date1: Date | number, date2: Date | number): number; /** 判断两个时间相差的毫秒数 */ static milliseBetween(date1: Date | number, date2: Date | number): number; static parseInt(text: string | undefined, defaultValue?: number): number; static parseFloat(text: string | undefined, defaultValue?: number): number; /** 判断两个数字是否相等 */ static equalNumber(a1: number | string | undefined, a2: number | string | undefined): boolean; /** 检查是否是一个有效的日期时间 */ static invalidDate(value: Date | number | string | undefined): boolean; /** 处理一个日期时间数据,无效时返回 `defaultValue` (默认为0), 否则返回 `Date` 类型 */ static parseDate(value: Date | number | string | undefined, defaultValue?: Date | number): Date | number; /** 指定日期时间增加 year 年 */ static dateIncYear(value: Date | number | string, year: number, clearTime?: boolean): Date; /** 获取指定日期中当前日的时间范围 */ static timeRangeDay(date?: Date | number | string): [Date, Date]; /** 获取指定日期中当前周的时间范围 */ static timeRangeWeek(date?: Date | number | string): [Date, Date]; /** 获取指定日期中当前月的时间范围 */ static timeRangeMonth(date?: Date | number | string): [Date, Date]; /** * 获取指定日期下的时间范围 * @param type 0 指定的时间,1 今日,2 本周,3 本月 * @param date 指定的日期,默认为当前时间 * @param beginTime * @param endTime */ static getDateRange(type?: number, date?: Date | number | string, beginTime?: Date | number | string, endTime?: Date | number | string): [Date, Date]; /** * 转换数组为目标类型数组 * @param src 源数组 * @param newItem 转换回调函数 * @param destArray 输出的目标数组(如果设置,会追加转换后的数据) * @returns 返回新的数组 */ static convert<T, E>(src: T[] | any | undefined, newItem: (item: T) => E, destArray?: E[]): E[]; /** * 将一个列表转化为树形列表, 列表数据需要本身支持转化成数型 * @param list 列表数据 * @param equalLevalItem 判断两个列表项是否同级且不相等的回调函数 * @param listFirstIsRoot 使用列表中的第1项作为树的root节点 * @param isRootItem 判断列表项是否为root节点的回调函数,listFirstIsRoot = false 时有效 * @param childrenField 列表项中,存放 children 列表的字段名称 * @param childrenMust 子列表字段是否必须存在,为 true 时,如果没有子列表,会将其设置为 [] * @param item 当前列表项, 调用者传 undefined * @returns */ static toTreeList<T>(list?: T[], equalLevalItem?: (a: T | undefined, item: T) => boolean, listFirstIsRoot?: boolean, isRootItem?: (e: T, index: number) => boolean, childrenField?: string, childrenMust?: boolean, item?: T): T[]; /** * 数组去重 * @param array * @param distinctKey */ static distinct(array: any[], distinctKey?: string): any[]; /** * 将数组转换为 Markdown 表格 * @param data 数据源 * @param keyName 列标题 * @param columnReCount 重复列数(比如本身有2列,重复2次,就是6列,总行数就会只有三分之一) * @param isVertical 重复列的时候,数据是横向还是竖向(默认为 `true` 竖向) */ static toMarkdownTable(data: Record<string, any>[], keyName: Record<string, string>, columnReCount?: number, isVertical?: boolean): string; /** * 安静的Promise包裹器 * @param promise Promise * @param onError 异常回调 * @returns */ static executeSilence<T, E = Error>(promise: Promise<T>, onError?: (err: Error) => void): Promise<[E | undefined, T | undefined]>; }