npm.io
0.2.3 • Published 4 months ago

scat-util

Licence
MIT
Version
0.2.3
Deps
0
Size
328 kB
Vulns
0
Weekly
0

scat-util

实用工具集 | utility tool function

v0.2.3

  • Optimize function math

安装 | 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 sourcetarget 互为不重合项组成的新集合

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'
首字母大写化

函数:capitalize | initialToUpperCase

参数:

参数名 类型 描述 必需 默认值
str string 目标字符串 -

返回:

返回类型 描述
string 结果

Example:

str.capitalize('asd')
// to be: 'Asd'
首字母小写化

函数:smallize | initialToLowerCase

参数:

参数名 类型 描述 必需 默认值
str string 目标字符串 -

返回:

返回类型 描述
string 结果

Example:

str.smallize('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 hhss'

返回:

返回类型 描述
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

此函数重载包含 regexpsMatchAnyregexpsMatchAllvaluesMatchAnyvaluesMatchAll 所有功能

参数:

参数名 类型 描述 必需 默认值
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 已包含 hexColorAhexColorA 只用于匹配带 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')

Keywords