app-lib-utils
v0.0.33
Published
通用基础类库-【app-lib-utils】
Downloads
5
Readme
二、功能描述
三、注意事项
Functions
type(any) ⇒ string
数据类型判断
- 通过对象原型的toString方法进行判断具体的数据类型(包含引用类型)
Kind: global function
Returns: string - 数据类型的字符串
- example
|入参|返回|描述| |-|-|-| |type('') |'string' | |type(null) |'null' | |type(undefined) |'undefined' | |type(NaN) |'number' | NaN也是number类型 | |type(true) |'boolean' | |type({}) |'object' | |type(Symbol()) |'symbol' | |type(new Date()) |'date' | |type([]) |'array' | |type(new Map()) |'map' | |type(new Set()) |'set' | |type(new Error()) |'error' | |type(()=>{}) |'function' | |type(Promise.resolve()) |'promise' | |type(new Proxy({}, {})) |'object' |代理是对象/自定义类实例都是object | |type(new ArrayBuffer(32)) |'arraybuffer' |二进制数据 | |type(window) |'window' |node 不支持window | |type(document) |'htmldocument' | |type(new XMLHttpRequest()) |'xmlhttprequest' |
| Param | Type | Description | | --- | --- | --- | | any | any | 任意数据类型 |
stringToRegxp(regString) ⇒ regexp | undefined
正则字符串转换为正则
- 不是正确正则字符串 返回undefined
Kind: global function
Returns: regexp | undefined - 转换的正则或者undefined
- example
|入参|返回|描述| |-|-|-| |stringToRegxp('/./') |/./ | |stringToRegxp('/.//') |undefined |
| Param | Type | Description | | --- | --- | --- | | regString | string | 正则字符串 |
isPlainObject(obj) ⇒ boolean
是否为纯粹对象
- 通过顶层的Object创建的对象则为纯粹对象
Kind: global function
Returns: boolean - true-纯粹对象 false-非纯粹对象
- example
|入参|返回|描述| |-|-|-| |isPlainObject(Object.create( null )) | true | |isPlainObject({}) | true | |isPlainObject(new Date()) | false|
| Param | Type | Description | | --- | --- | --- | | obj | object | 判断的对象 |
arrayToObject([arr], [key]) ⇒ object
数组转换为对象键值对的方式
- 快速将数组转换为指定key值的对象
Kind: global function
Returns: object - 返回的对象
- example
|入参|返回|描述| |-|-|-| |arrayToObject() | {} | |arrayToObject([1,2,3]) | {1:1,2:2,3:3} | |arrayToObject([{id:1,value:'v1'},{id:2,value:'v2'}]) | {1:{id:1,value:'v1'},2:{id:2,value:'v2'} | |arrayToObject([{id:1,value:'v1'},{id:2,value:'v2'}],'value') | {v1:{id:1,value:'v1'},v2:{id:2,value:'v2'} |
| Param | Type | Default | Description | | --- | --- | --- | --- | | [arr] | array | | 判断的对象 | | [key] | string | "'id'" | 指定数组里对象的属性作为生成对象key值 |
createObj(key, value, [split]) ⇒ object
创建深层次对象
- 嵌套属性key默认用'.'来间隔对象, [] 标识数组
- 不存在 自动创建对象或者数组
Kind: global function
Returns: object - 创建新对象
- example
|入参|返回|描述| |-|-|-| |createObj('a.b.[2].c',123)| {a:{b:[empty × 2, {c:123}]}}|| |createObj(['a','b','[2]','c'],123)|{a:{b:[empty × 2, {c:123}]}}|
| Param | Type | Default | Description | | --- | --- | --- | --- | | key | string | array | | 对象的属性名 | | value | any | | 对应属性值 | | [split] | array | [/./g , /^[(.*)]$/g] | .分割和[]数组的标识 |
get(obj, key, split) ⇒ any
深度获取对象或数组属性值
Kind: global function
Returns: any - [key对应的value值]
- example
|入参|返回|描述| |-|-|-| |get({a:{b:[1,{c:123}]}},'a.b.1.c')|123|| |get({a:{b:[1,{c:123}]}},'a.b.[1].c')|123|等价|
| Param | Type | Description | | --- | --- | --- | | obj | array | [对象] | | key | string | [属性key值] 默认用.来分割对象 []类标识数组类型 | | split | array | [分割和数组的标识] 嵌套属性key默认用'.'来间隔对象,[]标识数组 |
set(object, key, val, [split]) ⇒ object | array
设置深层对象属性和值
Kind: global function
Returns: object | array - 设置后的对象
- example
|入参|返回|描述| |-|-|-| | set(undefined,'a.[0].name','appBir')|{a:[{name:'appBir'}]}|
| Param | Type | Default | Description | | --- | --- | --- | --- | | object | object | array | | 原始对象 | | key | string | | 属性名-默认用.来分割对象 []类标识数组类型 | | val | any | | 对应值 | | [split] | array | [/./g , /^[(.*)]$/g] | 分割和数组的标识-嵌套属性key默认用'.'来间隔对象,[]标识数组 |
push(object, key, val, [split]) ⇒ array
数组深度追加数据
- 内存引用 不会新创建
Kind: global function
Returns: array - 设置后的对象
| Param | Type | Default | Description | | --- | --- | --- | --- | | object | array | | 原始对象 | | key | string | | 属性名-默认用.来分割对象 []类标识数组类型 | | val | any | | 对应值 | | [split] | array | [/./g , /^[(.*)]$/g] | 分割和数组的标识-嵌套属性key默认用'.'来间隔对象,[]标识数组 |
del(object, key, val, [split]) ⇒ object | array
删除指定属性的值
- 内存引用 不会新创建
Kind: global function
Returns: object | array - 设置后的对象
| Param | Type | Default | Description | | --- | --- | --- | --- | | object | object | array | | 原始对象 | | key | string | | 属性名-默认用.来分割对象 []类标识数组类型 | | val | any | | 对应值 | | [split] | array | [/./g , /^[(.*)]$/g] | 分割和数组的标识-嵌套属性key默认用'.'来间隔对象,[]标识数组 |
camelToUnderline(camelStr) ⇒ string
驼峰转下划线
Kind: global function
Returns: string - 下划线字符串
| Param | Type | Description | | --- | --- | --- | | camelStr | string | 驼峰字符串 |
underlineToCamel(underlineStr) ⇒ string
下划线转驼峰
Kind: global function
Returns: string - 驼峰字符串
| Param | Type | Description | | --- | --- | --- | | underlineStr | string | 下划线字符串 |
camelToConst(camelStr) ⇒ string
驼峰转常量格式
Kind: global function
Returns: string - 常量字符串
| Param | Type | Description | | --- | --- | --- | | camelStr | string | 驼峰字符串 |
firstCharToUpperCase(camelStr) ⇒ string
首字母大写
Kind: global function
Returns: string - 常量字符串
| Param | Type | Description | | --- | --- | --- | | camelStr | string | 驼峰字符串 |
dictToObj(dicts, fn) ⇒ object
字典表格式化
- 根据后端存储的字典表,快速适配到前端的使用
Kind: global function
Returns: object - 对象数据
- example
const dict = [
{ id: '1', code: 'pageState', key: 'add', value: '添加' },
{ id: '2', code: 'pageState', key: 'edit', value: '编辑' },
...
];- 转换后的数据
{
PAGE_STATE:{ // 主要用于前端页面的常量处理
ADD:'add', // 作为前端常量使用
add:{ id: '1', code: 'pageState', key: 'add', value: '添加' } // 根key值快速找该字典
...
}
pageState:[ // 主要用于下拉显示-根据不同的组件库采用不同的字段转换,
{ id: '1', code: 'pageState', key: 'add', value: '添加' },
...
]
}
| Param | Type | Description | | --- | --- | --- | | dicts | array | 字典数据 | | fn | function | 格式化字典的对象方法 |
urlToObject(url, [config]) ⇒ object
解析查询参数
- 采用 encodeURIComponent 和 decodeURIComponent 进行编码和解码处理
Kind: global function
Returns: object - 解析对象
- example
|入参|返回|描述| |-|-|-| |urlToObject("http://localhost:9000/api/sysResources?_emb=true&name=中国") | {_emb: "true", name: "中国"} |纯URL解析| |urlToObject("http://localhost:9000/api/sysResources?_emb=true&name=中国",{code:'encode'}) |{_emb: "true", name: "%E4%B8%AD%E5%9B%BD"}|编码-解析| |urlToObject("http://localhost:9000/api/sysResources?_emb=true&name=%E4%B8%AD%E5%9B%BD",{code:'decode'}) |{_emb: "true", name: "中国"}| 解码-解析|
| Param | Type | Default | Description | | --- | --- | --- | --- | | url | string | | url地址 | | [config] | object | {} | 编码解码方式配置 | | config.code | string | | 编码解码配置-encode/decode 编码/ 解码 |
objectToUrl(urlObj, [config]) ⇒ string
对象转换url查询参数
- 目前支持一层 后续有需要进行扩展
Kind: global function
Returns: string - 生成URL地址
- exmaple
|入参|返回|描述| |-|-|-| |objectToUrl( {_emb: "true", name: "中国"})|"_emb=true&name=中国"|纯编码| |stringify( {_emb: "true", name: "中国"},{code:'encode'})|"_emb=true&name=%E4%B8%AD%E5%9B%BD"|编码| |stringify( {_emb: "true", name: "%E4%B8%AD%E5%9B%BD"},{code:'decode'})| "_emb=true&name=中国"|解码|
| Param | Type | Default | Description | | --- | --- | --- | --- | | urlObj | string | | url地址对象 | | [config] | object | {} | 编码解码方式配置 | | config.code | string | | 编码解码配置-encode/decode 编码/ 解码 |
isEmptyObj(obj)
判断对象是否空对象
Kind: global function
Retun: boolean 是否为空对象 true-空对象 false-纯在键/值
| Param | Type | | --- | --- | | obj | object |
arrayToTree(array, option) ⇒ array | object
数组转换为树
Kind: global function
Returns: array | object - 转换后的树形数据
| Param | Type | Default | Description | | --- | --- | --- | --- | | array | array | | 数组数据 | | option | object | | 参数配置 | | [option.keyFiled] | object | "id" | 数据的唯一标识 | | [option.parentFiled] | object | "parent" | 父节点属性 | | [option.childFiled] | object | "children" | 组装树的子节点属性 | | [option.returnType] | object | "tree" | 树状对象返回 或者数组返回 |
toString(str, resourceRadix, targetRadix, isUpperCase) ⇒ string
指定进制转换为指定进制
- 主要用到js的两个方法
- parseInt(str,radix) 将字符串str按照radix进制编码方式转换为10进制返回
- Number.toString(radix) 将该数字的指定进制形式的字符串
Kind: global function
Returns: string - 转换后的字符串
| Param | Type | Description | | --- | --- | --- | | str | string | 进制转换字符串 | | resourceRadix | number | 当前的字符进制 默认10进制 进制支持2 到36之间 | | targetRadix | number | 需要转换的进制 默认10进制 进制支持2 到 36 | | isUpperCase | boolean | 是否需要大写 默认是大写 |
fill(str, length, fillFlag, isFront) ⇒ string
指定长度字符串自动填充指定标识
- 多余则截取
Kind: global function
Returns: string - 返回的长度
| Param | Type | Description | | --- | --- | --- | | str | string | 字符串 | | length | number | 长度 | | fillFlag | string | 填充标识 默认是'0' 超出长度 自动截取 | | isFront | boolean | 是否字符从前填充 默认true 否则后端填充 |
randomChineseWord() ⇒
随机生成汉子
- Unicode编码是16进制数,其中汉字对应范围为4E00-9FA5,转换为10进制数就是19968-40869
- 最新uncode 到9FC6了-- 还有第二个板块(plaint)的汉字体 当前方式不是完全的准确
Kind: global function
Returns: string
subStringByte(str, length) ⇒
按照字节长度截取字符串
Kind: global function
Returns: string 截取后的字符串
| Param | Type | Description | | --- | --- | --- | | str | string | 字符串 | | length | number | 截取长度 |
isAsyncFunction(func) ⇒ boolean
判断一个方法是否为异步的方法
Kind: global function
Returns: boolean - 是否未异步方法
- example
|入参|返回|描述| |-|-|-| |const fn = async ()=>{} |true|| |const fn2 = ()=>{}|false| |promise|//TODO 带验证|
| Param | Type | | --- | --- | | func | function |
randomColor() ⇒ string
随机生成16进制的颜色值
- TODO 后续支持透明度和更优雅的代码
Kind: global function
Returns: string - 随机生成16进制颜色
opacityToHex(opacity, isAutoFill) ⇒ string
JS透明度转换16进制
- 透明度是0-1 颜色值是0到255 - 故将透明度* 255再转换为16进制即为颜色16进制表示的最后两位标识
- 当前与chrome的转换一致 即 四舍五入的方式 将透明度转换为16进制
- isAutoFiled 是否自动补全(两位标识) 默认是
Kind: global function
Returns: string - 十六进制字符串
| Param | Type | Description | | --- | --- | --- | | opacity | number | 透明度 0~1 范围 | | isAutoFill | boolean | 是否自动补全两位16制的字符串输出 默认是 |
hexToOpacity(opacityHexStr) ⇒ string
16进制的透明度转换为颜色
Kind: global function
Returns: string - 透明颜色
| Param | Type | | --- | --- | | opacityHexStr | string |
colorHexToGBA(colorStr, [isAutoFill]) ⇒ object
16进制的颜色转换为gba格式的对象
Kind: global function
Returns: object - {r:1~255,g:1~255,b:1~255,a:0~1};
| Param | Type | Default | Description | | --- | --- | --- | --- | | colorStr | string | | | | [isAutoFill] | boolean | true | 默认值 true |
colorGBAToHex(colorObj) ⇒ string
rgba颜色对象转换为16进制的颜色字符串
Kind: global function
Returns: string - 颜色转换后的值
- example
|入参|返回|描述| |-|-|-| |colorGBAToHex({r: 34, g: 25, b: 77, a: 0.59})| '#22194D96'|| |colorGBAToHex({})|#000000| |colorGBAToHex() | ''|
| Param | Type | | --- | --- | | colorObj | object |
clone(data) ⇒
clone 深度克隆数据
Kind: global function
Returns: any 任意数据类型
| Param | Type | Description | | --- | --- | --- | | data | any | 任意数据类型 |
copyText(text)
复制文本到剪切版本
Kind: global function
| Param | Type | | --- | --- | | text | string |
download(url, fileName)
批量下载文件文件
Kind: global function
| Param | Type | Description | | --- | --- | --- | | url | * | 文件URL地址 | | fileName | * | 文件名称 无效地址--根据运行环境自身而定 手机端:则下载html页面 PC端则显示无效的页面 |
completeString(string, totalSum, fillString) ⇒ string
填充字符串
Kind: global function
Returns: string - 补全后的字符创
| Param | Type | Description | | --- | --- | --- | | string | string | 字符串 | | totalSum | number | 填充长度 | | fillString | string | 填充内容 |
completeNumber(totalSum, number, hex) ⇒ string
[ompleteNumber 自动补全
Kind: global function
Returns: string - [补充后字符串]
example: completeNumber(11,6,16) // result is "00000b" 将11转换16进制自动补全6位。
| Param | Type | Description | | --- | --- | --- | | totalSum | number | [补全位数] | | number | number | [显示数字] | | hex | number | [显示进制进制] |
encodingStr(string, totalSum, hex) ⇒ string
编码字符串
Kind: global function
Returns: string - 编码字符串
| Param | Type | Description | | --- | --- | --- | | string | string | 待编码字符串 | | totalSum | number | 编码长度 | | hex | number | 进制 |
splitStr(str, number, replace) ⇒ type
字符串分割
Kind: global function
Returns: type - 返回值
| Param | Type | Description | | --- | --- | --- | | str | string | 字符串 | | number | number | 分割间隔字符数 | | replace | string | 分割替换符 |
getUUID(id, unSplit, spStr) ⇒ string
创建UUID
Kind: global function
Returns: string - 生成UUID
example:
|入参|返回|描述| |-|-|-| |getUUID() |015686f7eeea01804bdf77e40233ebfb |随机识别码| |getUUID('zhangshan')|015686f872cd7a68616e7ec153f20628 |英文识别码| |getUUID('张三') |015686f84be45f205c7177b5f486d515 |中文识别码|
- description:
1:UUID结构
|位数| 8 | 4 | 4 | 4 | 16 |
|意义| 日期时间时钟 | 识别码 | 随机数 |
2:识别码 支持中文、英文,只有前8位生效,中文前两个汉字,英文前4个字母;默认8位随机数。| Param | Type | Description | | --- | --- | --- | | id | string | Identification code识别码 | | unSplit | string | 是否显示分割线 | | spStr | string | 分割先 |
syncPromises(promisesFns, promiseFnParams, breakCallback) ⇒ promise
同步执行批量promise方法-可进行控制
- 支持普通方法和异步方法一起执行
- 支持执行满足某种条件时,中断执行
- 支持每个promise都支持默认静态参数传递
- 支持每个pormise都能获取上一个或n个执行结果
Kind: global function
Returns: promise - promise对象 某个promise方法异常 在使用await时 需要捕获异常按照正常promise使用 按照标准promise使用即可
- 参数详细说明
|参数|入参|返回| |-|-|-| |promisesFns |每个方法包含三个入参。arg是promiseFnParams对应下标值;preResult是上一个函数结果,第一个函数为null;all是所有执行过函数的结果值 |promise | |promiseFnParams|每个参数都会按照数组下表对应到每个promisesFn arg |any | |function |中断回调函数。每个异步函数执行完毕都会调用。{ isError, result, allResult, errorResult },isError:执行是否异常,result执行结果,allResult全部结果,errorResult:异常结果|执行返回true中断,false执行下一个,默认是true |
- 同步执行的常用场景
|场景|返回|demo| |-|-|-| |每个promise无论中途是否有异常,都返回结果 |全部结果,异常和正常,存在异常catch接受 |参考example1 | |每个promise只要有一个错误则结束继续执行,返回执行过的结果 |执行过的结果,异常和正常,存在异常catch接受 |参考example2 | |每个promise,满足特定条件时(上一个结果满足条件等)结束执行 |执行过的结果,异常和正常,存在异常catch接受 |参考example3 |
- example-fns
|函数|描述|
|-|-|
|const fn1 = (arg, preResult, all) =>new Promise(resolve => log(arg, preResult, all) && setTimeout(() => resolve(Fn1-promise-success), 1000));|成功promise|
|const fn2 = (arg, preResult, all) =>new Promise((s,reject) => log(arg, preResult, all) && setTimeout(() => reject(Fn2-promise-error), 1000));|异常promise|
|const fn3 = (arg, preResult, all) => log(arg, preResult, all) && Fn3-function-success; |成功function|
|const fn4 = (arg, preResult, all) =>new Promise(resolve => log(arg, preResult, all) && setTimeout(() => resolve(Fn4-promise-success), 1000));|成功promise|
- example
|入参|返回|描述| |-|-|-| |syncPromises([fn1, fn2, fn3, fn4], ["arg1", "arg2", "arg3", "arg4"])| ['Fn1-promise-success','Fn2-promise-error','Fn3-function-success','Fn4-promise-success']|异常(catch)返回全部函数结果| |syncPromises([fn1, fn2, fn3, fn4], ["arg1", "arg2", "arg3", "arg4"], ({ isError }) => isError)| [ 'Fn1-promise-success', 'Fn2-promise-error' ]|异常(catch)返回存在异常的结果| |syncPromises([fn1, fn2, fn3, fn4], ["arg1", "arg2", "arg3", "arg4"], ({ result }) => result.startsWith('Fn3'))|[ 'Fn1-promise-success', 'Fn2-promise-error', 'Fn3-function-success' ]|异常(catch)返回存在知道Fn3| |syncPromises([fn1, fn3, fn4], ["arg1", "arg2", "arg3", "arg4"])|['Fn1-promise-success','Fn3-function-success','Fn4-promise-success']|正常(then)返回所有结果| |syncPromises([fn1, fn3, fn4], ["arg1", "arg2", "arg3", "arg4"])|['Fn1-promise-success','Fn3-function-success','Fn4-promise-success']|正常(then)返回全部结果| |syncPromises([fn1, fn3, fn4], ["arg1", "arg2", "arg3", "arg4"], ({ result }) => result.startsWith('Fn3'))| [ 'Fn1-promise-success', 'Fn3-function-success' ]|正常(then)返回执行到Fn3|
| Param | Type | Description | | --- | --- | --- | | promisesFns | array.<function()> | 所有promise方法 不是promise example:[(arg, preResult, all)=>Promise.resolve("xxx")] | | promiseFnParams | array.<any> | 所有promise方法的参数数组 下标与promisesFns下标对应 | | breakCallback | function | 满足指定条件后中断方法 |
