jslk
v1.1.3
Published
前端工具库
Downloads
25
Readme
jslk
常用 js 工具库, TypeScript + ESlint + Jest + Rollup
npm包版本号管理
使用: concurrently npm-version-bump 调用命令行更新版本号
npm install concurrently npm-version-bump -g
npm打包发布快捷命令
- npm run build1: 用例测试 + 更新主版本号 + 打包 + 标记 + git推送
- npm run build2: 用例测试 + 更新次版本号 + 打包 + 标记 + git推送
- npm run build3: 用例测试 + 更新补丁号 + 打包 + 标记 + git推送
- npm run login: 登陆npm
- npm run send: 发布
使用文档
npm install jslk -S
cnpm install jslk -S
yarn add jslk -S
pnpm add jslk -S
0. General funcs
0.1 typeis 检测值类型
0.2 re 正则校验
re(regxp: RegExp, val: any): boolean
1. Array
1.1 arrayable 数组有效性检测
arrayable(array: any, length = true): boolean
1.2 arrayUnique 从数组中检索唯一元素
arrayUnique<T>(array: any, nested = false): Array<T>
1.3 arrValsCount 从数组中检索存在的元素计数
arrValsCount<T>(array: any): NumberDict
1.4 arrValCount 计数一个元素在数组中出现的次数
arrValCount<T>(array: any, key: T): number
1.5 flat 拉平数组
flat<T>(array: any, depth = Infinity): OptionalPropertyArray<T>
2. Buffer
2.1 url2Blob 字符串url转换为blob
url2Blob(url: string): Blob | never
2.2 str2Buffer 转换字符串到缓冲区
str2Buffer(str: string): Uint8Array | undefined
2.3 buffer2Str 将缓冲区转换为字符串
buffer2Str(buff: Uint8Array): string | undefined
2.4 encode 编码: 对标window.btoa
2.5 decode 解码: 对标window.atob
3. Date
3.1 formatDate 日期格式转换
formatDate(date?: Date | string, format = 'yyyy-MM-dd hh:mm:ss'): string
3.2 timezoneDate 检索指定时区的日期
timezoneDate(year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0, timezone?: string): Date | undefined
3.3 dateable 确定一个变量是Date类型
dateable(date?: any, validDate = true): boolean
3.4 quarterable
Retrieve a quarter string with another quarter and delta quarters which are used to calculate
quarterable(quarter: any, delta = 0, separator = 'Q'): string | undefined
4. Enum
4.1 inEnum 检测枚举是否存在
inEnum<T>(enums?: Dict<T>, i?: string | T): boolean
4.2 Types 前端数据类型枚举
4.3 TypesDesc 前端数据类型枚举描述
5. eventloop
5.1 sleep 在eventloop中休眠几毫秒
sleep(ms = 16): Promise<number>
6. Map
6.1 mappable 是否为非空map
mappable(map?: any, length = true): boolean
7. Math
7.1 rangeRandom 根据范围返回一个随机数
rangeRandom(min?: any, max?: any): number
7.2 numberic 数字格式化
numberic(val?: any, digits = 2, places?: number): number | undefined
7.3 guid 生成一个随机id
guid(length = 8, salt = 'abcdefghijklmnopqrstuvwxyz0123456789'): string
7.4 hash 随机生成一个哈希字符串
hash(): string
7.5 numberFormat 数字格式化
numberFormat(val?: any, digits = 2, places?: number): string | undefined
7.6 moneyFormat 金额格式化,例如'1,000,000'
moneyFormat(val?: any, digits = 2, places?: number, defaultVal = ''): string
7.7 signFormat 使用正负号将数字值格式化为字符串,如'+1'或'-1','0'
signFormat(val?: any, defaultVal = ''): string
8. Object
8.1 objectable Object有效性检测
objectable(obj: any, length = true): boolean
8.2 optional 从对象中的可选链中检索值
optional<T>(path: any, obj?: any, separator = '.'): OptionalPropertyItem<T>
8.3 extractable 提取键值
extractable(...args: Array<ExtractableArgs>): Dict<any>
8.4 assign
Assign a value to an object with key path
assign<T>(path: any, obj: OptionalDict<T>, val?: any, separator = '.'): OptionalDict<T>
8.5 objReverse 反转对象的键和值
objReverse(obj?: OptionalDict<any> | null): OptionalDict<any> | null
9. Regexp
9.1 isNil 是否为空/未定义/空字符串
isNil(val?: any): boolean
9.2 isNull 是否为null
isNul(val?: any): boolean
9.3 isUndefined 是否为undefined
isUndefined(val?: any): boolean
9.4 isNumberic 是否为有数字
isNumberic(val: any, signed?: boolean, arabic = true): boolean | number
10. Set
10.1 settable 是否为非空set集合
settable(set?: any, length = true): boolean
11. String
11.1 stringable 是否为非空字符串
stringable(str: any, length = true): boolean
11.2 strLen 计算字符串中的字符数
strLen(str: any): number
12. Function
12.1 copy 深拷贝
copy(obj: any, deep = true): any
12.2 debounce 防抖函数
debounce<F extends Func>(fn: F, time: number | string, immediate = false): Func | never
12.3 throuttle 节流函数
throuttle<F extends Func>(fn: F, time: number | string, options?: ThrottleOptions): Func | never
12.4 sort 排序函数
sort(a: any, b: any, ignoreCase = false, locale = 'zh-Hans-CN')
12.5 listToTree 列表转树结构
listToTree(list: Array<any>, treeConfig?: treeConfigField): tree
12.6 treeToList 树结构转列表
treeToList(tree: tree, result?: tree, treeConfig?: treeConfigField, level?: number): tree
12.7 treeFind 查找树的子节点
treeFind<F extends Func>(tree: tree, func: F, treeConfig?: treeConfigField):treeItem | null
12.8 treeFindPath 查找树节点路径
treeFindPath<F extends Func>(tree: tree, func: F, treeConfig?: treeConfigField, path=[]):Array<any>
12.9 treeFindPathAll 查找所有节点路径
treeFindPathAll<F extends Func>(tree: tree, func: F, treeConfig: treeConfigField = defaultTreeConfig, path:Array<any> = [], result:Array<any> = []):Array<any>
12.9 createColors 根据颜色值生成由深到浅的色系值
createColors<F extends Func>(color: string, num: number = 6): string[]