scat-util v0.1.1
scat-util
实用工具集 | utility tool function
v0.1.1 Change Log
- optimize comments
- function set
tree
: optimize functionfromArray
安装 | Install
npm install scat-util
更新 | Update
npm install scat-util@latest
# or
npm install scat-util@stable
使用 | Usage
将default整体引入 | Full Import
import ScatUtil from 'scat-util'
ScatUtil.valid(1)
部分函数引入 | Partly Import
与 default 方式引入区别:scat
+ 函数名(函数名首字母大写)
import { scatValid } from 'scat-util'
scatValid(1)
库函数简介 | Library functions Introduction
数值加单位 | Add unit to the tail of the value
函数名:addUnit
默认值取值规则:opt 类型为 JSONObject 时优先取 option.defaultValue,第三参数 defaultValue 优先级次之;opt 类型为 string 时取 第三参数 defaultValue
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | number \| string | 数值 | 否 | - |
unit | string | 后缀单位 | 否 | - |
defaultValue | number \| string | value 缺省时的默认值 | 否 | auto |
Or
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | number \| string | 数值 | 否 | - |
option | OptionObject | 配置 | 否 | - |
defaultValue | number \| string | value 缺省时的默认值(优先级低于 option.defaultValue) | 否 | auto |
OptionObject 配置:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
defaultValue | number \| string | value 缺省时的默认值 | 否 | - |
unit | string | 后缀单位 | 否 | px |
返回:
返回类型 | 描述 |
---|---|
number \| string \| undefined | 结果 |
Example:
addUnit(3.1415, 'px')
// to be: '3.1415px'
数组相关
函数集:arr
| array
按固定数组元素数切片数组
函数名:slice
| arraySlice
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | Array<any> | 源数组 | 是 | - |
sliceLen | number | 一份切片的数组元素数 | 否 | 10 |
返回:
返回类型 | 描述 |
---|---|
Array<any> | 结果数组 |
Example:
array.slice([1, 1, 2, 3, 5], 2)
// to be: [[1, 1], [2, 3], [5]]
是否在数组中
函数名:inside
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
source | Array<any> | 源数组 | 是 | - |
findValue | any \| Array<any> | 查找数据 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
boolean | 结果 |
Example:
array.inside([1, 1, 2, 3, 5], 2)
// to be: true
在数组中出现的次数
函数名:exists
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
source | Array<any> | 源数组 | 是 | - |
findValue | any | 查找数据 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
number | 出现的次数 |
Example:
array.exists([1, 1, 2, 3, 5], 2)
// to be: 1
去重
函数名:dedup
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
source | Array<T> | 数组 | 是 | - |
返回:
返回类型 | 描述 |
---|---|
Array<T> | 结果数组 |
Example:
arr.dedup([1, 1, 2, 3, 5])
// to be: [1, 2, 3, 5]
排序并反转
函数名:sortAndReverse
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
source | Array<T> | 数组 | 是 | - |
sortFn | (arg1: any, arg2?: any) => any | 自定义排序回调 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
Array<T> | 结果数组 |
Example:
arr.sortAndReverse([1, 4, 3, 5], (a, b) => a - b)
// to be: [5, 4, 3, 1]
数组 A 是 B 的子集(A ⊆ B)
函数名:contain
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
source | Array<T> | 数组 | 是 | - |
target | Array<T> | 数组 | 是 | - |
返回:
返回类型 | 描述 |
---|---|
boolean | target 是否 source 的子集 |
Example:
arr.contain([1, 1, 2, 3, 5], [3, 5])
// to be: true
求数组 A,B,C,...的并集(A ∪ B ∪ C ...)
函数名:union
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
args | Array<Array<T>> | 可填多数组 | 是 | - |
返回:
返回类型 | 描述 |
---|---|
Array<T> | - |
Example:
arr.union([1, 2], [3, 4], [5, 6])
// to be: [1, 2, 3, 4, 5, 6]
求数组 A,B,C,...的交集(A ∩ B ∩ C ...)
函数名:overlap
| intersect
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
args | Array<Array<T>> | 可填多数组 | 是 | - |
返回:
返回类型 | 描述 |
---|---|
Array<T> | - |
Example:
arr.overlap([1, 2, 3], [3, 4], [3, 5, 6])
// to be: [3]
根据数组 A ,求数组 B 的补集(CuA)
函数名:addition
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
source | Array<T> | 数组 | 是 | - |
target | Array<T> | 数组 | 是 | - |
返回:
返回类型 | 描述 |
---|---|
boolean | 根据全集 source ,求 target 的补集 |
Example:
arr.addition([1, 1, 2, 3, 5], [1])
// to be: [2, 3, 5]
求数组 A 相对于数组 B 的差集(A - B)
函数名:differ
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
source | Array<T> | 数组 | 是 | - |
target | Array<T> | 数组 | 是 | - |
返回:
返回类型 | 描述 |
---|---|
boolean | 取 source 中与 target 不重合项组成的新集合 |
Example:
arr.differ([1, 1, 2, 3, 5], [1, 1, 2])
// to be: [3, 5]
求数组 A 相对于数组 B 的对称差集((A - B) + (B - A))
函数名:symdiffer
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
source | Array<T> | 数组 | 是 | - |
target | Array<T> | 数组 | 是 | - |
返回:
返回类型 | 描述 |
---|---|
boolean | 取 source 与 target 互为不重合项组成的新集合 |
Example:
arr.symdiffer([1, 1, 2, 3, 5], [1, 2, 4])
// to be: [3, 4, 5]
浏览器相关
函数集: browser
获取浏览器类型和版本
函数名:browserInfo
参数:
无
返回:
返回类型 | 描述 |
---|---|
BrowserInfo | 浏览器类型和版本 |
BrowserInfo:
类型 | 描述 |
---|---|
name | 浏览器类型 |
version | 浏览器版本 |
Example:
browserInfo()
// to be: { name: '', version: '' }
是否微信浏览器
函数名:isWeixin
参数:
无
返回:
返回类型 | 描述 |
---|---|
boolean | 结果 |
Example:
isWeixin()
// to be: false
是否企业微信浏览器
函数名:isWxWork
参数:
无
返回:
返回类型 | 描述 |
---|---|
boolean | 结果 |
Example:
isWxWork()
// to be: false
是否阿里系浏览器
函数名:isAlipay
参数:
无
返回:
返回类型 | 描述 |
---|---|
boolean | 结果 |
Example:
isAlipay()
// to be: false
是否移动端浏览器
函数名:isMobile
参数:
无
返回:
返回类型 | 描述 |
---|---|
boolean | 结果 |
Example:
isMobile()
// to be: false
是否iOS
函数名:isIOS
参数:
无
返回:
返回类型 | 描述 |
---|---|
boolean | 结果 |
Example:
isIOS()
// to be: false
是否PC
函数名:isPC
参数:
无
返回:
返回类型 | 描述 |
---|---|
boolean | 结果 |
Example:
isPC()
// to be: false
是否爬虫
函数名:isSpider
参数:
无
返回:
返回类型 | 描述 |
---|---|
boolean | 结果 |
Example:
isSpider()
// to be: false
检测相关
函数集:check
检测密码强度
函数名:pswdStrength
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | string | 密码 | 是 | - |
返回:
返回类型 | 描述 |
---|---|
number | 强度值0-4,数值越小强度越低 |
Example:
pswdStrength('')
// to be: 0
深拷贝
函数名:clone
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
target | any | 待拷贝值 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
any | 拷贝值 |
Example:
clone({ a: 1 })
// to be: { a: 1 }
数值计算相关
函数集:compute
计算日期天数差
函数名:daysDuration
| daysDiff
已废弃
仅做兼容处理,可使用函数
date.diff
替代
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
start | Date \| string \| number | 时间戳 1 | 是 | - |
end | Date \| string \| number | 时间戳 2 | 是 | - |
返回:
返回类型 | 描述 |
---|---|
number | 日期天数差 |
Example:
compute.daysDuration(new Date(), new Date())
// to be: 0
计算指定月份前、后 N 个月日期
函数名:monthDelta
Example:
compute.monthDelta(2)
compute.monthDelta(2, '2023/01/01')
计算指定日期前、后 N 天日期
函数名:dateDelta
Example:
compute.dateDelta(2)
compute.dateDelta(2, '2023/01/01')
基于指定日期计算偏移量日期
函数名:calDate
Example:
compute.calDate('2023/01/01', 2, 'YYYY/MM/DD 00:00:00')
计算两点(经纬度)之间的距离
函数名:distance
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
longitude1 | number | 坐标点1经度 | 否 | - |
latitude1 | number | 坐标点1纬度 | 否 | - |
longitude2 | number | 坐标点2经度 | 否 | - |
latitude2 | number | 坐标点2纬度 | 否 | - |
accuracy | 'm' \| 'foot' \| 'km' \| 'inch' \| 'mile' \| 'seamile' \| 'cmile' | 距离精度 | 否 | - |
precision | number | 数值精度 | 否 | - |
lang | 'en-US' \| 'zh-CN' | 语言 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
string | 带单位的距离字符串 |
Example:
compute.distance(longitude1, latitude1, longitude2, latitude2, 'm', 1, 'en-US')
根据已知分子、分母比例计算所需值
函数名:scale
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
dividendX | number | 已知比例分子 | 是 | - |
dividerX | number | 已知比例分母 | 是 | - |
dividendY | number | 未知比例分子 | 否 | - |
dividerY | number | 未知比例分母 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
string | 值 |
Example:
compute.scale(2, 6, 3, undefined)
// to be: 9
日期相关
函数集:date
时间差计算
函数名:diff
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
start | string \| number \| Date | 开始日期 | 否 | - |
unit | DateDiffUnit | 差值计算类型 | 否 | - |
👆 计算开始日期与当前事件差值
Or
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
start | string \| number \| Date | 开始日期 | 否 | - |
end | string \| number \| Date | 结束日期 | 否 | - |
unit | DateDiffUnit | 差值计算类型 | 否 | - |
DateDiffUnit:
'd' | 'day' | 'h' | 'hour' | 'm' | 'minute' | 's' | 'second'
返回:
返回类型 | 描述 |
---|---|
number | 时间差 |
Example:
date.diff('2024-01-01', '2024-01-02', 'd')
// to be: 1
是否为闰年
函数名:isLeapYear
①能够被4整除但不被100整除的是闰年;②能够被100整除但不被400整除的是平年;③能够被400整除但不被3200整除的是闰年;④能够被3200整除但不被172800整除的是平年;⑤能被172800整除的一律是闰年。
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
time | string \| number \| Date | 日期 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
boolean | 结果 |
Example:
date.isLeapYear('2024-01-01')
// to be: true
获取当前地区UTC时区
函数名:utcTZ
返回:
返回类型 | 描述 |
---|---|
string | 结果 |
Example:
date.utcTZ()
获取当前地区UTC时区偏移量
函数名:utcTZOffset
返回:
返回类型 | 描述 |
---|---|
number | 负数 东N区;正数 西N区 |
Example:
date.utcTZOffset()
// to be: -8
校验值有效
函数名:validItem
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | any | 待验证值 | 是 | - |
返回:
返回类型 | 描述 |
---|---|
boolean | 值校验通过 |
Example:
validItem('')
// to be: false
validItem([])
// to be: false
validItem({})
// to be: false
validItem(0)
// to be: true
进阶:同时校验多值
函数名:valid
支持同时校验多值
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
a | any | 待验证值 | 否 | - |
b | any | 待验证值 | 否 | - |
... | any | 待验证值 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
boolean | 全部待验证值都校验通过 |
Example:
valid('', [], {})
// to be: false
数组、字符串拆分
函数集:split
| slice
拆分字符串
函数名:str
| string
| sliceStr
| sliceString
已废弃
仅做兼容处理,可使用函数
string.slice
替代
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
str | string | 目标字符串 | 否 | - |
slice | number | 片长 | 否 | - |
option | OptionObject | 配置 | 是 | - |
OptionObject 配置:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
fromHead | boolean | 从 list 第一位开始 | 否 | - |
padStart | string | 分段头部填充字符 | 否 | - |
padEnd | string | 分段尾部填充字符 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
Array<any> | 结果 |
Example:
split.str('MyName', 2)
// to be: ['My', 'Na', 'me']
拆分数组
函数名:arr
| array
| sliceArr
| sliceArray
已废弃
仅做兼容处理,可使用函数
array.slice
替代
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
arr | string | 目标数组 | 否 | - |
slice | number | 片长 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
Array<any> | 结果 |
Example:
split.arr(['My', 'Na', 'me'], 2)
// to be: [['my', 'Na'], ['me']]
字符串相关
函数集:str
| string
头插占位字符串
函数名:padStart
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
target | string | 目标字符串 | 是 | - |
num | number \| string | 最小返回字符串字符长度 | 是 | - |
fillstr | string | 占位字符串 | 是 | - |
返回:
返回类型 | 描述 |
---|---|
string | 结果 |
Example:
str.padStart('MyName', 7, '*')
// to be: '*MyName'
尾插占位字符串
函数名:padEnd
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
target | string | 目标字符串 | 是 | - |
num | number \| string | 最小返回字符串字符长度 | 是 | - |
fillstr | string | 占位字符串 | 是 | - |
返回:
返回类型 | 描述 |
---|---|
string | 结果 |
Example:
str.padEnd('MyName', 7, '*')
// to be: 'MyName*'
字符串插入
函数名:insert
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | string | 源字符串 | 否 | - |
index | number \| string | 插入位置 | 是 | - |
insertString | string | 插入字符串 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
string | 结果 |
Example:
str.insert('MyName', 2, '*')
// to be: 'My*Name'
按固定字符数切片字符串为字符串数组
函数名:slice
| stringSlice
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | string | 源字符串 | 否 | - |
sliceLen | number | 一份切片的字符数 | 否 | - |
option | OptionObject | 配置 | 否 | - |
OptionObject:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
fromHead | boolean | 从list第一位开始 | 否 | - |
padStart | string | 分段头部填充字符 | 否 | - |
padEnd | string | 分段尾部填充字符 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
Array<string> | 字符串数组 |
Example:
str.slice('MyName', 2)
// to be: ['My', 'Na', 'me']
去除空格
函数名:trim
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | string | 源字符串 | 否 | - |
type | 'start' \| 'end' \| 'both' \| 'all' | 去除空格类型 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
string | 字符串 |
Example:
str.trim(' My Na me ', 'all')
// to be: 'MyName'
首字母大写化
函数:initialToUpperCase
| capitalize
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
str | string | 目标字符串 | 是 | - |
返回:
返回类型 | 描述 |
---|---|
string | 结果 |
Example:
str.initialToUpperCase('asd')
// to be: 'Asd'
首字母小写化
函数:str.initialToLowerCase
| smallize
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
str | string | 目标字符串 | 是 | - |
返回:
返回类型 | 描述 |
---|---|
string | 结果 |
Example:
str.initialToLowerCase('ASD')
// to be: 'aSD'
数学计算相关
函数集:math
浮点数相乘
函数名:mul
| multiply
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
a | number | 被乘数 | 否 | - |
b | number | 乘数 | 否 | - |
... | number | 乘数 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
number | 积 |
Example:
math.mul(1.1, 1.2, 1.3)
// to be: 1.716
浮点数相加
函数名:plus
| add
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
a | number | 被加数 | 否 | - |
b | number | 加数 | 否 | - |
... | number | 加数 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
number | 和 |
Example:
math.plus(1.1, 1.2, 1.3)
// to be: 3.6
浮点数相减
函数名:minus
| substract
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
a | number | 被减数 | 否 | - |
b | number | 减数 | 否 | - |
... | number | 减数 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
number | 差 |
Example:
math.minus(5, 1.2, 1.3)
// to be: 2.5
浮点数相除
函数名:div
| division
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
a | number | 被除数 | 否 | - |
b | number | 除数 | 否 | - |
... | number | 除数 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
number | 商 |
Example:
math.div(3.6, 2, 1.3)
// to be: 1
数组计算式
模拟自然数学表达式
函数名:cal
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
values | [MathArgs \| MathCalArgs, MathCalType, MathArgs \| MathCalArgs] | 数组计算式 | 是 | - |
precision | number | 小数点精确度 | 否 | - |
type MathArgs = string | number | undefined | null
type MathCalType = '+' | 'plus' | 'add' | '-' | 'minus' | 'sub' | 'substract' | '*' | 'x' | '×' | 'mul' | 'multiply' | '/' | '÷' | 'div' | 'division'
type MathCalArgs = [MathArgs | MathCalArgs, MathCalType, MathArgs | MathCalArgs]
返回:
返回类型 | 描述 |
---|---|
number | 计算结果 |
Example:
math.cal([7, '-', [5, '*', 2]])
// to be: -3
求一堆数值的最大公约数(公因数)
函数名:gcd
| GCD
| greatestCommonDivisor
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
a | number | 数值 | 否 | - |
b | number | 数值 | 否 | - |
... | number | 数值 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
number | 最大公约数(公因数) |
Example:
math.gcd(1, 1, 2, 3, 5)
求一堆数值的最小公倍数
函数名:lcm
| LCM
| leastCommonMultiple
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
a | number | 数值 | 否 | - |
b | number | 数值 | 否 | - |
... | number | 数值 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
number | 最小公倍数 |
Example:
math.lcm(1, 1, 2, 3, 5)
格式化
函数集:format
数值
函数名:money
| numeric
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | string \| number | 需要格式化的值 | 否 | - |
precision | number | 精度 | 否 | - |
Or
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | string \| number | 需要格式化的值 | 否 | - |
separator | string | 分隔符 | 否 | - |
Or
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | string \| number | 需要格式化的值 | 否 | - |
option | OptionObject | 配置 | 否 | - |
OptionObject 配置:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
precision | number | 精度 | 否 | 2 |
separator | string | 千位分隔符 | 否 | ',' |
showZero | boolean | 强制显示 0 | 否 | false |
floor | boolean | 向下取整 | 否 | false |
返回:
返回类型 | 描述 |
---|---|
string | 数值字符串 |
Example:
format.money('35565')
数字
函数名:number
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | string \| number | 需要格式化的值 | 否 | - |
precision | number | 精度 | 否 | - |
Or
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | string \| number | 需要格式化的值 | 否 | - |
separator | string | 分隔符 | 否 | - |
Or
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | string \| number | 需要格式化的值 | 否 | - |
option | OptionObject | 配置 | 否 | - |
OptionObject 配置:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
precision | number | 精度 | 否 | 2 |
separator | string | 千位分隔符 | 否 | ',' |
showZero | boolean | 强制显示 0 | 否 | true |
floor | boolean | 向下取整 | 否 | false |
返回:
返回类型 | 描述 |
---|---|
string | 数值字符串 |
Example:
format.number('35565.2', { precision: 2, floor: true })
精确到小数点后 1 位的数值
函数名:format.numberP1
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | string \| number | 数值 | 否 | - |
floor | boolean | 向下取整 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
string | 数值字符串 |
Example:
format.numberP1('35565.2', true)
精确到小数点后 3 位的数值
函数名:format.numberP3
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | string \| number | 数值 | 否 | - |
floor | boolean | 向下取整 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
string | 数值字符串 |
Example:
format.numberP3('35565.2', true)
向下取整的数值
函数名:format.floorNum
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | string \| number | 数值 | 否 | - |
precision | number | 精度 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
string | 数值字符串 |
Example:
format.floorNum('35565.2', { precision: 2, floor: true })
向下取整的,精确到小数点后 1 位的数值
函数名:format.floorNumP1
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | string \| number | 数值 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
string | 数值字符串 |
Example:
format.floorNumP1('35565.2', true)
向下取整的,精确到小数点后 3 位的数值
函数名:format.floorNumP3
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | string \| number | 数值 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
string | 数值字符串 |
Example:
format.floorNumP3('35565.2', true)
时间戳转换
函数名:timestamp
| moment
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | string \| number \| Date | 时间戳 | 否 | - |
formatstr | string | 转换格式 | 否 | 'YYYY-MM-DD hh:mm:ss' |
返回:
返回类型 | 描述 |
---|---|
string | 格式化的时间字符串 |
Example:
format.timestamp(new Date(2024, 01, 01), 'YYYY-MM')
// to be: 2024-01
手机号
函数名:format.mobile
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | string \| number | 手机号 | 否 | - |
separator | string | 脱敏符号 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
string | 脱敏后手机号字符串 |
Example:
format.mobile('18888888888', '*')
超出部分省略号
函数名:ellipsis
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | string | 字符串 | 否 | - |
maxlen | number | 最大显示字符数 | 否 | 8 |
返回:
返回类型 | 描述 |
---|---|
string | 处理后的字符串 |
Example:
format.ellipsis('18888888888', 2)
// to be: '18...'
姓名脱敏
函数名:name
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | string | 姓名 | 否 | - |
separator | string | 脱敏符号 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
string | 脱敏后姓名字符串 |
Example:
format.name('MyName', '*')
节流防抖
节流
函数名:throttle
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
callback | function | 要执行的回调函数 | 否 | - |
waitTime | number | 延迟执行时间(毫秒) | 否 | 500 |
immediate | boolean | 立即执行 | 否 | true |
mustcall | function | 必定执行函数 | 否 | - |
Or
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
callback | function | 要执行的回调函数 | 否 | - |
waitTime | number | 延迟执行时间(毫秒) | 否 | 500 |
option | OptionObject | 配置 | 否 | - |
mustcall | function | 必定执行函数 | 否 | - |
OptionObject 配置:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
immediate | number | 立即执行 | 否 | true |
mustDoIt | function | 必定执行函数 | 否 | - |
mustcall | function | 必定执行函数(优先级高于mustDoIt) | 否 | - |
uid | string | uuid | 否 | - |
key | string | uuid(优先级高于key) | 否 | - |
返回:
返回类型 | 描述 |
---|---|
void | - |
Example:
throttle(() => {
// do something
}, 1000)
防抖
函数名:debounce
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
callback | function | 要执行的回调函数 | 否 | - |
waitTime | number | 延迟执行时间(毫秒) | 否 | 500 |
immediate | boolean | 立即执行 | 否 | false |
mustcall | function | 必定执行函数 | 否 | - |
Or
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
callback | function | 要执行的回调函数 | 否 | - |
waitTime | number | 延迟执行时间(毫秒) | 否 | 500 |
option | OptionObject | 配置 | 否 | - |
mustcall | function | 必定执行函数 | 否 | - |
OptionObject 配置:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
immediate | number | 立即执行 | 否 | false |
mustDoIt | function | 必定执行函数 | 否 | - |
mustcall | function | 必定执行函数(优先级高于mustDoIt) | 否 | - |
uid | string | uuid | 否 | - |
key | string | uuid(优先级高于key) | 否 | - |
返回:
返回类型 | 描述 |
---|---|
void | - |
Example:
debounce(() => {
// do something
}, 1000)
生成uuid
函数名:uuid
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
len | number | uuid的长度 | 否 | - |
radix | number | 生成uuid的基数(意味着返回的字符串都是这个基数),2:二进制, 8:八进制, 10:十进制, 16:十六进制, 36:[0-9A-Z] 62 [0-9A-Za-z] | 否 |
返回:
返回类型 | 描述 |
---|---|
string | uuid |
Example:
uuid()
随机生成数值
函数集:rand
随机整数
函数名:int
| integer
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
min | number | 最小值 | 否 | - |
max | number | 最大值 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
number | 随机整数 |
Example:
rand.int(5, 10)
随机浮点数
函数名:dec
| decimal
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
min | number | 最小值 | 否 | - |
max | number | 最大值 | 否 | - |
precision | number | 小数后N位 | 否 | 2 |
返回:
返回类型 | 描述 |
---|---|
number | 随机浮点数 |
Example:
rand.dec(5, 10)
转换 | Trans
函数集:trans
HEX -> RGBA
函数名:hex2rgba
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
hex | string | 十六进制 | 否 | - |
opacity | number | alpha通道值 | 否 | 1 |
返回:
返回类型 | 描述 |
---|---|
string | RGBA字符串 |
Example:
trans.hex2rgba('#000000')
转短横线命名
函数名:hump2dash
| camelCase2kebabCase
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | string | 驼峰字符串 | 否 | - |
replace | string | 替换字符 | 否 | '-' |
返回:
返回类型 | 描述 |
---|---|
string | 短横线命名字符串 |
Example:
trans.hump2dash('HasProp')
// to be: 'has-prop'
转驼峰命名
函数名:dash2hump
| camelCase
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | string | 字符串 | 否 | - |
replace | string | 替换字符 | 否 | '-' |
返回:
返回类型 | 描述 |
---|---|
string | 驼峰命名字符串 |
Example:
trans.dash2hump('has-prop')
// to be: hasProp
转帕斯卡命名
函数名:pascalCase
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | string | 字符串 | 否 | - |
replace | string | 替换字符 | 否 | '-' |
返回:
返回类型 | 描述 |
---|---|
string | 帕斯卡命名字符串 |
Example:
trans.pascalCase('has-prop')
// to be: HasProp
阿拉伯数字 -> 中文数字
函数名:num2chn
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
num | string \| number | 数字 | 否 | - |
option | OptionObject | option 配置 | 否 | - |
OptionObject 配置:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
colloquial | boolean | 口语化 | 否 | - |
capital | boolean | 中文数字大写 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
string | 中文数字 |
Example:
trans.num2chn('365')
压缩图片
函数名:compressImg
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
file | File | 图片文件 | 否 | - |
quality | number | 质量 0-1 | 否 | 0.9 |
返回:
返回类型 | 描述 |
---|---|
Promise<File> | 图片文件 |
Example:
trans.compressImg(File, 0.9)
key-value 键值对为数字-字符串/数字形式的 -> 数组
函数名:object2array
Example:
trans.object2array({ a: 1, b: 2 })
数组 -> key-value 键值对为数字-字符串/数字形式
函数名:array2object
Example:
trans.array2object([
{ name: 'a', value: 1 },
{ name: 'b', value: 2 }
])
blob -> base64
函数名:blob2dataurl
Example:
trans.blob2dataurl(new Blob(...))
blob -> file
函数名:blob2file
Example:
trans.blob2file(new File(...))
file -> base64
函数名:file2dataurl
Example:
trans.file2dataurl(new File(...))
file -> blob
函数名:file2blob
Example:
trans.file2blob(new Blob(...))
base64 -> blob
函数名:dataurl2blob
Example:
trans.dataurl2blob('')
base64 -> file
函数名:dataurl2file
Example:
trans.dataurl2file('')
转为数字(number 类型)
函数名:toNumber
Example:
trans.toNumber('123', { replaceNaN: 0 })
// to be: 123
树形结构操作
对象名:flatTree
对象内属性为函数集 tree
中的各函数
树形结构操作 | Tree
函数集:tree
设置全局自定义属性名
函数名:setFieldNames
若函数参数 fieldNames 未传入或传入的值为 null 或 undefined 时,全局自定义属性名生效,若全局自定义属性名为空,则使用默认自定义属性名
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
fieldNames | FieldNames | 自定义属性名 | 否 | - |
FieldNames:
属性 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
label | string | 节点描述的字段名 | 否 | 'label' |
value | string | 节点唯一标识key的字段名 | 否 | 'value' |
children | string | 子节点的字段名 | 否 | 'children' |
parentValue | string | 父节点唯一标识key的字段名 | 否 | 'parentValue' |
返回:
返回类型 | 描述 |
---|---|
void | - |
tree.setFieldNames({ label: 'name', value: 'id' })
数组转树形结构
函数名:fromArray
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
array | Array<object> | 树 | 是 | - |
fieldNames | FieldNames | 自定义属性名 | 否 | - |
FieldNames:
属性 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
label | string | 节点描述的字段名 | 否 | 'label' |
value | string | 节点唯一标识key的字段名 | 否 | 'value' |
children | string | 子节点的字段名 | 否 | 'children' |
parentValue | string | 父节点唯一标识key的字段名 | 否 | 'parentValue' |
返回:
返回类型 | 描述 |
---|---|
Array<object> | 树形结构 |
Example:
tree.fromArray(
[
{ pid: undefined, id: 1 },
{ pid: 1, id: 11 },
{ pid: 1, id: 12 }
],
{ value: 'id', children: 'children', parentValue: 'pid' }
)
// to be: [{ pid: undefined, id: 1, children: [{ pid: 1, id: 11 }, { pid: 1, id: 12 }] }]
深度优先遍历平面化树形结构(DFS)
函数名:flat
| flatByDepth
| depthFirst
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
tree | Array<object> | 树 | 是 | - |
fieldNames | FieldNames | 自定义属性名 | 否 | - |
FieldNames:
属性 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
label | string | 节点描述的字段名 | 否 | 'label' |
value | string | 节点唯一标识key的字段名 | 否 | 'value' |
children | string | 子节点的字段名 | 否 | 'children' |
parentValue | string | 父节点唯一标识key的字段名 | 否 | 'parentValue' |
返回:
返回类型 | 描述 |
---|---|
Array<object> | 树形平面化后的数组 |
Example:
tree.flatByDepth([{ a: 1, c: [{ a: 2 }] }], { value: 'a', children: 'c', parentValue: 'pid' })
// to be: [{ a: 1, pid: undefined }, { a: 2, pid: 1 }]
广度优先遍历平面化树形结构(BFS)泛洪方式/广播方式
函数名:flatByBreadth
| breadthFirst
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
tree | Array<object> | 树 | 是 | - |
fieldNames | FieldNames | 自定义属性名 | 否 | - |
FieldNames:
属性 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
label | string | 节点描述的字段名 | 否 | 'label' |
value | string | 节点唯一标识key的字段名 | 否 | 'value' |
children | string | 子节点的字段名 | 否 | 'children' |
parentValue | string | 父节点唯一标识key的字段名 | 否 | 'parentValue' |
返回:
返回类型 | 描述 |
---|---|
Array<object> | 树形平面化后的数组 |
Example:
tree.flatByBreadth([{ a: 1, c: [{ a: 2 }] }], { value: 'a', children: 'c', parentValue: 'pid' })
// to be: [{ a: 1, pid: undefined }, { a: 2, pid: 1 }]
查询树节点到根节点的链形路径(返回唯一标识:'value'值)
函数名:findNodeKeyPath
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
tree | Array<object> | 树 | 是 | - |
nodeValue | string \| number | 唯一标识 | 是 | - |
fieldNames | FieldNames | 自定义属性名 | 否 | - |
FieldNames:
属性 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
label | string | 节点描述的字段名 | 否 | 'label' |
value | string | 节点唯一标识key的字段名 | 否 | 'value' |
children | string | 子节点的字段名 | 否 | 'children' |
parentValue | string | 父节点唯一标识key的字段名 | 否 | 'parentValue' |
返回:
返回类型 | 描述 |
---|---|
Array<string \| number> | 链形路径 |
Example:
tree.findNodeKeyPath([{ a: 1, c: [{ a: 2 }] }], 2, { value: 'a', children: 'c', parentValue: 'pid' })
// to be: [1, 2]
查询树节点到根节点的链形路径(返回完整Node节点信息)
函数名:findNodePath
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
tree | Array<object> | 树 | 是 | - |
nodeValue | string \| number | 唯一标识 | 是 | - |
fieldNames | FieldNames | 自定义属性名 | 否 | - |
noChildren | boolean | 不返回 children 字段 | 否 | true |
FieldNames:
属性 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
label | string | 节点描述的字段名 | 否 | 'label' |
value | string | 节点唯一标识key的字段名 | 否 | 'value' |
children | string | 子节点的字段名 | 否 | 'children' |
parentValue | string | 父节点唯一标识key的字段名 | 否 | 'parentValue' |
返回:
返回类型 | 描述 |
---|---|
Array<object> | 树形结构 |
Example:
tree.findNodePath([{ a: 1, c: [{ a: 2 }] }], 2, { value: 'a', children: 'c', parentValue: 'pid' })
// to be: [{ a: 1 }, { a: 2 }]
查询树目标节点的父节点key(返回唯一标识:'value'值)
函数名:findParentNodeKey
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
tree | Array<object> | 树 | 是 | - |
nodeValue | string \| number | 唯一标识 | 是 | - |
fieldNames | FieldNames | 自定义属性名 | 否 | - |
FieldNames:
属性 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
label | string | 节点描述的字段名 | 否 | 'label' |
value | string | 节点唯一标识key的字段名 | 否 | 'value' |
children | string | 子节点的字段名 | 否 | 'children' |
parentValue | string | 父节点唯一标识key的字段名 | 否 | 'parentValue' |
返回:
返回类型 | 描述 |
---|---|
string \| number \| null \| undefined | 节点key |
Example:
tree.findParentNodeKey([{ a: 1, c: [{ a: 2 }] }], 2, { value: 'a', children: 'c', parentValue: 'pid' })
// to be: 1
查询树目标节点的父节点(返回完整Node节点信息)
函数名:findParentNode
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
tree | Array<object> | 树 | 是 | - |
nodeValue | string \| number | 唯一标识 | 是 | - |
fieldNames | FieldNames | 自定义属性名 | 否 | - |
noChildren | boolean | 不返回 children 字段 | 否 | true |
FieldNames:
属性 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
label | string | 节点描述的字段名 | 否 | 'label' |
value | string | 节点唯一标识key的字段名 | 否 | 'value' |
children | string | 子节点的字段名 | 否 | 'children' |
parentValue | string | 父节点唯一标识key的字段名 | 否 | 'parentValue' |
返回:
返回类型 | 描述 |
---|---|
object | 树节点 |
Example:
tree.findParentNode([{ a: 1, c: [{ a: 2 }] }], 2, { value: 'a', children: 'c', parentValue: 'pid' }, true)
// to be: { a: 1 }
树节点模糊查询
函数名:fuzzyQuery
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
tree | Array<object> | 树 | 是 | - |
fuzzyValue | any | 模糊查询值 | 是 | - |
attr | string \| number | 查询属性名 | 是 | 'value' |
fieldNames | FieldNames | 自定义属性名 | 否 | - |
FieldNames:
属性 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
label | string | 节点描述的字段名 | 否 | 'label' |
value | string | 节点唯一标识key的字段名 | 否 | 'value' |
children | string | 子节点的字段名 | 否 | 'children' |
parentValue | string | 父节点唯一标识key的字段名 | 否 | 'parentValue' |
返回:
返回类型 | 描述 |
---|---|
Array<object> | 树形结构 |
Example:
tree.fuzzyQuery([{ a: 1, c: [{ a: 2 }] }], 1, 'a', { value: 'a', children: 'c', parentValue: 'pid' })
// to be: [{ a: 1, c: [{ a: 2 }] }]
树节点模糊查询(仅返回符合条件的节点,不返回父节点)
函数名:fuzzyQueryNode
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
tree | Array<object> | 树 | 是 | - |
fuzzyValue | any | 模糊查询值 | 是 | - |
attr | string \| number | 查询属性名 | 是 | 'value' |
fieldNames | FieldNames | 自定义属性名 | 否 | - |
FieldNames:
属性 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
label | string | 节点描述的字段名 | 否 | 'label' |
value | string | 节点唯一标识key的字段名 | 否 | 'value' |
children | string | 子节点的字段名 | 否 | 'children' |
parentValue | string | 父节点唯一标识key的字段名 | 否 | 'parentValue' |
返回:
返回类型 | 描述 |
---|---|
Array<object> | 扁平化节点列表 |
Example:
tree.fuzzyQueryNode([{ a: '12', c: [{ a: '22' }] }], '2', 'a', { value: 'a', children: 'c', parentValue: 'pid' })
// to be: [{ a: '12' }, { a: '22' }]
找出树所有叶子节点
函数名:findAllLeaf
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
tree | Array<object> | 树 | 是 | - |
fieldNames | FieldNames | 自定义属性名 | 否 | - |
FieldNames:
属性 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
label | string | 节点描述的字段名 | 否 | 'label' |
value | string | 节点唯一标识key的字段名 | 否 | 'value' |
children | string | 子节点的字段名 | 否 | 'children' |
parentValue | string | 父节点唯一标识key的字段名 | 否 | 'parentValue' |
返回:
返回类型 | 描述 |
---|---|
Array<object> | 树形结构 |
Example:
tree.findAllLeaf([{ a: 1, c: [{ a: 2 }] }], { value: 'a', children: 'c', parentValue: 'pid' })
// to be: [{ a: 2 }]
去除树节点空children属性
函数名:removeEmptyChildren
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
tree | Array<object> | 树 | 是 | - |
fieldNames | FieldNames | 自定义属性名 | 否 | - |
FieldNames:
属性 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
label | string | 节点描述的字段名 | 否 | 'label' |
value | string | 节点唯一标识key的字段名 | 否 | 'value' |
children | string | 子节点的字段名 | 否 | 'children' |
parentValue | string | 父节点唯一标识key的字段名 | 否 | 'parentValue' |
返回:
返回类型 | 描述 |
---|---|
Array<object> | 树形结构 |
Example:
tree.findAllLeaf([{ a: 1, c: [] }], { value: 'a', children: 'c', parentValue: 'pid' })
// to be: [{ a: 1 }]
为树每个节点添加属性
函数名:addNodesAttr
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
tree | Array<object> | 树 | 是 | - |
addition | { [propName: string \| number]: (item: any, index: number) => any } | 添加属性 | 否 | - |
fieldNames | FieldNames | 自定义属性名 | 否 | - |
FieldNames:
属性 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
label | string | 节点描述的字段名 | 否 | 'label' |
value | string | 节点唯一标识key的字段名 | 否 | 'value' |
children | string | 子节点的字段名 | 否 | 'children' |
parentValue | string | 父节点唯一标识key的字段名 | 否 | 'parentValue' |
返回:
返回类型 | 描述 |
---|---|
Array<object> | 树形结构 |
Example:
tree.addNodesAttr([{ a: 1, c: [] }], { b: (item, index) => `${item.a}${index}` }, { value: 'a', children: 'c', parentValue: 'pid' })
// to be: [{ a: 1, b: '10', c: [] }]
将树每个节点移除属性
函数名:removeNodesAttr
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
tree | Array<object> | 树 | 是 | - |
attr | string \| Array<string> | 属性 | 否 | - |
fieldNames | FieldNames | 自定义属性名 | 否 | - |
FieldNames:
属性 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
label | string | 节点描述的字段名 | 否 | 'label' |
value | string | 节点唯一标识key的字段名 | 否 | 'value' |
children | string | 子节点的字段名 | 否 | 'children' |
parentValue | string | 父节点唯一标识key的字段名 | 否 | 'parentValue' |
返回:
返回类型 | 描述 |
---|---|
Array<object> | 树形结构 |
Example:
tree.removeNodesAttr([{ a: 1, c: [] }], c, { value: 'a', children: 'c', parentValue: 'pid' })
// to be: [{ a: 1 }]
截取 URL 参数
函数集:url
截取 url 中的键值对
函数名:toParam
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | string | URL | 否 | - |
option | OptionObject | 配置 | 否 | - |
OptionObject 配置:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
fullUrl | boolean | 完整URL,若只想传入以“&”相连的键值对需设置为false | 否 | true |
transNumber | boolean | 将字符串数字转为数值数字 | 否 | false |
返回:
返回类型 | 描述 |
---|---|
object | 键值对对象 |
Example:
url.toParam('https://www.baidu.com?type=what')
// to be: { type: 'what' }
键值对转换为 URL 可用的参数字符串
函数名:paramTo
Example:
url.paramTo({ type: 'what', aha: 'yep' }, { transUndefined: true, transNull: true })
// to be: 'type=what&aha=yep'
深合并
函数名:merge
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | object | 对象 | 否 | - |
... | object | 对象 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
object | 合并后的对象 |
Example:
merge({ a: 1 }, { a: 3, b: 4 }, { a: 2 })
// to be: { a: 2, b: 4 }
提取对象键值对
函数名:pick
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | object | 对象 | 否 | - |
propKey | string \| Array<string> | 属性名或属性名数组 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
object | 对象 |
Example:
pick({ a: 1, b: 2, c: 3 }, ['a', 'b'])
// to be: { a: 1, b: 2 }
排除对象属性并返回排除操作后的新对象
函数名:omit
| reject
reject
已废弃,使用 omit
以替代
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | object | 对象 | 否 | - |
propKey | string \| Array<string> | 属性名或属性名数组 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
object | 对象 |
Example:
omit({ a: 1, b: 2, c: 3 }, ['a', 'b'])
// to be: { c: 3 }
判断对象中有 PropertyKey
函数名:hasProp
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | any | 对象 | 否 | - |
propKey | string \| Array<string> | 属性名或属性名数组 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
boolean | 是否有这个/这些属性 |
Example:
hasProp({ a: 1, b: 2, c: 3 }, 'a')
// to be: true
hasProp({ a: 1, b: 2, c: 3 }, 'd')
// to be: false
hasProp({ a: 1, b: 2, c: 3 }, ['a', 'b'])
// to be: true
hasProp({ a: 1, b: 2, c: 3 }, ['a', 'd'])
// to be: false
判断(可能是嵌套的)参数a 与 参数b 完全相等
函数名:equal
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
a | any | 参数a | 否 | - |
b | any | 参数b | 否 | - |
返回:
返回类型 | 描述 |
---|---|
boolean | 是否相等 |
Example:
equal({ a: 1, b: 2, c: 3 }, { a: 1, c: 3, b: 2 })
// to be: false
有效值萃取
函数名:extract
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | any | 待萃取值 | 否 | - |
defaultValue | any | 萃取默认值 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
any | 返回有效值,或在值无效时默认返回'',当defaultValue传入时返回defaultValue值 |
Example:
extract(NaN, 123)
// to be: 123
extract(NaN)
// to be: ''
判断差异
函数集:diff
是否 JS 基本类型
函数名:isPrimitive
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | any \| undefined | 数据 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
boolean | 判断结果 |
Example:
diff.isPrimitive(1)
是否对象
函数名:isObject
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | any \| undefined | 数据 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
boolean | 判断结果 |
Example:
diff.isObject({ a: 1 })
是否数组
函数名:isArray
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | any \| undefined | 数据 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
boolean | 判断结果 |
Example:
diff.isArray([1])
是否日期
函数名:isDate
可判断日期字符串/数字时间戳,也可判断 Date 类型日期
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | any \| undefined | 数据 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
boolean | 判断结果 |
Example:
diff.isDate(new Date())
是否正则
函数名:isRegExp
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | any \| undefined | 数据 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
boolean | 判断结果 |
Example:
diff.isRegExp(new RegExp())
是否数字
函数名:isNumber
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | any \| undefined | 数据 | 否 | - |
range | [number, number] | 数值所在范围内 | 否 | - |
containEdge | boolean | 是否包含数值上下界 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
boolean | 判断结果 |
Example:
diff.isNumber(123)
是否 Blob
函数名:isBlob
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | any \| undefined | 数据 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
boolean | 判断结果 |
Example:
diff.isBlob(new Blob(..))
是否 File
函数名:isFile
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
value | any \| undefined | 数据 | 否 | - |
返回:
返回类型 | 描述 |
---|---|
boolean | 判断结果 |
Example:
diff.isFile(new File(..))
判断 RGBA 颜色深浅
函数名:rgbaIsLight
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
r | number | 红色数值(0-255) | 是 | - |
g | number | 绿色数值(0-255) | 是 | - |
b | number | 蓝色数值(0-255) | 是 | - |
a | number | alpha 通道值【透明度】(0-1) | 否 | - |
threshold | number | 阈值(r * 0.299 + g * 0.758 + b * 0.114,默认 192) | 否 | - |
返回:
返回类型 | 描述 |
---|---|
boolean | 判断结果 |
Example:
diff.rgbaIsLight(255, 255, 255, 1)
判断颜色(HEX,RGBA)深浅
函数名:colorIsLight
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
color | string | 颜色 | 是 | - |
threshold | number | 阈值(r * 0.299 + g * 0.758 + b * 0.114,默认 192) | 否 | - |
返回:
返回类型 | 描述 |
---|---|
boolean | 判断结果 |
Example:
diff.colorIsLight(255, 255, 255, 1)
正则校验
函数集:test
基础函数
函数名:regexp
此函数重载包含
regexpsMatchAny
、regexpsMatchAll
、valuesMatchAny
、valuesMatchAll
所有功能
参数:
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
expression | string \| RegExp | regexpMap对象属性名/正则表达式 | 是 | - |
value | string \| Array<string> | 要判断的值 | 否 | - |
Or
1个值匹配多个正则公式(全匹配/部分匹配)
或多个值匹配1个正则公式(全匹配/部分匹配)
参数名 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
expressionArray | Array<string \| RegExp> | regexpMap对象属性名/正则表达式 数组 | 是 | - |
value | string \| Array<string> | 要判断的值 | 否 | - |
type | 'some' \| 'all' | 匹配方式 | 否 | - |
Example:
test.regexp(/^[0-9]/, '123')
// to be: true
基础函数进阶用法
1 个值匹配多个正则公式
部分匹配
函数名:regexpsMatchAny
Example:
test.regexpsMatchAny([/^[0-9]/, /^[4-9]/], '123')
全匹配
函数名:regexpsMatchAll
Example:
test.regexpsMatchAll([/^[0-9]/, /^[4-9]/], '123')
多个个值匹配 1 个正则公式
部分匹配
函数名:valuesMatchAny
Example:
test.valuesMatchAny(/^[0-9]/, ['123', '456'])
全匹配
函数名:valuesMatchAll
Example:
test.valuesMatchAll(/^[0-9]/, ['123', '456'])
中国国内手机号
函数名:mobile
Example:
test.mobile('123')
中国国内座机号
函数名:phone
Example:
test.phone('123')
金额
函数名:money
Example:
test.money('123')
base64
函数名:base64
Example:
test.base64('123')
本地文件
函数名:localFile
Example:
test.localFile('file:///123.jpg')
图片文件名
函数名:imageFile
| image
Example:
test.imageFile('123')
HEX color
函数名:hexColor
Example:
test.hexColor('123')
HEXA color
hexColor
已包含 hexColorA
,hexColorA
只用于匹配带 Alpha 值的 hex 颜色值
函数名:hexColorA
Example:
test.hexColorA('123')
RGBA
函数名:rgba
Example:
test.rgba('123')
渐变色
函数名:linearGradient
Example:
test.linearGradient('123')
URL
函数名:url
Example:
test.url('123')
电子邮件
函数名:email
Example:
test.email('123')
中国国内邮政编码
函数名:zipcode
Example:
test.zipcode('123')
字母+数字
函数名:letterNumber
Example:
test.letterNumber('123')
数字
函数名:number
Example:
test.number('123')
MAC 地址
函数名:mac
Example:
test.mac('11:11:11:11:11')
身份证
函数名:idcard
Example:
test.idcard('123')
IPv4
函数名:ipv4
Example:
test.ipv4('123')
带端口号的IPv4
函数名:ipv4port
Example:
test.ipv4port('123')
验证日期
函数名:date
Example:
test.date('123')
包含汉字
函数名:hanzi
Example:
test.hanzi('123')
QQ号
函数名:qqNumber
Example:
test.qqNumber('123')
8 months ago
8 months ago
8 months ago
9 months ago
12 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
1 year ago
12 months ago
12 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago