Members
(constant) clipboardObj
- Source:
cookie
- Source:
(constant) cursortPosition
- Source:
(constant) debounce
函数防抖
- Source:
(constant) getDeviceModel
- Source:
(constant) isDiff
- Source:
(constant) objTools
- Source:
(constant) sleepAction
- Source:
(constant) throttle
函数节流
- Source:
Methods
alias(obj, name, aliasName)
给原型方法或属性添加别名 eg:alias(Array,"forEach","each");
Parameters:
Name | Type | Description |
---|---|---|
obj |
object | 对象 |
name |
string | object | 属性名称或对象 eg: 'forEach' | {forEach:'each'} |
aliasName |
string | 别名 |
- Source:
arrayMove(array, from, to) → {*}
数组位移
Parameters:
Name | Type | Description |
---|---|---|
array |
array | |
from |
number | |
to |
number |
- Source:
Returns:
- Type
- *
Example
```
const { arrayMove } = require('@alrale/common-lib');
const input = ['a', 'b', 'c'];
const array1 = arrayMove(input, 1, 2);
console.log(array1);
//=> ['a', 'c', 'b']
const array2 = arrayMove(input, -1, 0);
console.log(array2);
//=> ['c', 'a', 'b']
const array3 = arrayMove(input, -2, -3);
console.log(array3);
//=> ['b', 'a', 'c']
```
arrayMoveMutate(array, from, to)
引用数组位移,性能优化
Parameters:
Name | Type | Description |
---|---|---|
array |
* | |
from |
number | |
to |
number |
- Source:
arrayToObject(key, arr) → {object}
Parameters:
Name | Type | Description |
---|---|---|
key |
string | |
arr |
any |
- Source:
Returns:
- Type
- object
arrayToTree()
转换方法
- Source:
arrFibonacci(length, value, step)
按条件产生数组 arr(5,2,2) => [2,4,6,8,10]
eg:按1-10项产生斐波那契数列 =>arr(10, function (value, i, list) { return i > 1 ? list[i - 1] + list[i - 2] : 1; })
Parameters:
Name | Type | Description |
---|---|---|
length |
number | 要产生的数组长度 |
value |
number | 数组项的初始值 |
step |
number | 递增值或处理函数(当前值,索引,当前产生的数组) |
- Source:
arrVals(list, prop, skipUndefined)
根据指定的键或索引抽取数组项的值
eg:vals([{id:1},{id:2}], "id") => [1,2]
eg:vals([[1,"a"],[2,"b"]], 1) => ["a","b"]
Parameters:
Name | Type | Description |
---|---|---|
list |
array | 对象数组 |
prop |
string | 要抽取的属性 |
skipUndefined |
boolean | 是否跳过值不存在的项,默认为true |
- Source:
callHandler(callHandlerData, name, data) → {*}
Parameters:
Name | Type | Description |
---|---|---|
callHandlerData |
* | |
name |
* | n请求的方法 |
data |
* | 请求时传的参数 |
- Source:
Returns:
function
- Type
- *
camelize(str) → {string}
Parameters:
Name | Type | Description |
---|---|---|
str |
string |
- Source:
Returns:
- Type
- string
checkInt(str, min, max)
判断是否是符合条件的整数
Parameters:
Name | Type | Description |
---|---|---|
str |
string | number | 要检测的字符串或数字 |
min |
number | undefined | 允许的最小值 |
max |
number | undefined | 允许的最大值 |
- Source:
checkNum(str, min, max, max_decimal_len)
判断是否是符合条件的数字
Parameters:
Name | Type | Description |
---|---|---|
str |
string | number | 要检测的字符串或数字 |
min |
number | undefined | 允许的最小值 |
max |
number | undefined | 允许的最大值 |
max_decimal_len |
number | undefined | 最大小数位数 |
- Source:
compareSize()
比较大小
- Source:
Returns:
-1 0 1
dasherize(str) → {string}
Parameters:
Name | Type | Description |
---|---|---|
str |
string |
- Source:
Returns:
- Type
- string
dateFormat1(value) → {*}
日期格式化
时间规则:
今天显示:xx:xx
昨天显示:昨天 xx:xx
一周内显示:星期x xx:xx
一年内显示:xx月xx日 xx:xx
更早日期显示:xx年xx月xx日 xx:xx
Parameters:
Name | Type | Description |
---|---|---|
value |
* | 时间戳,单位s |
- Source:
Returns:
格式化后的日期
- Type
- *
dateFormat2(dateTimeStamp) → {*}
日期格式化
时间规则:
小于一分钟显示:刚刚
1分钟-60分钟内显示:xx分钟前
大于60分钟显示:xx小时前
昨天显示:昨天 xx:xx
更早日期显示:xx-xx-xx xx:xx
Parameters:
Name | Type | Description |
---|---|---|
dateTimeStamp |
* | 时间戳,单位s |
- Source:
Returns:
格式化后的日期
- Type
- *
decode_url_param(param)
解码url参数值 eg:%E6%B5%8B%E8%AF%95 => 测试
Parameters:
Name | Type | Description |
---|---|---|
param |
string | 要解码的字符串 eg:%E6%B5%8B%E8%AF%95 |
- Source:
def(value, defValue) → {*}
若value不为undefine,则返回value;否则返回defValue
Parameters:
Name | Type | Description |
---|---|---|
value |
object | |
defValue |
object | value不存在时返回的值 |
- Source:
Returns:
- Type
- *
elDateFormat(time, type) → {number|string|null}
获取饿了么框架时间选择器时间戳
Parameters:
Name | Type | Description |
---|---|---|
time |
* | 时间 |
type |
* | 类型 默认值 0 0 开始时间 1 结束时间 |
- Source:
Returns:
时间戳
- Type
- number | string | null
escapeHTML(str) → {string}
Parameters:
Name | Type | Description |
---|---|---|
str |
string |
- Source:
Returns:
- Type
- string
exportXls(data, name, callBack)
删除对象里面value值为null的键值对
Parameters:
Name | Type | Description |
---|---|---|
data |
* | 接口返回的blob数据 |
name |
* | excel名称 |
callBack |
* | 导出成功/失败回调 回调返回{type:fail/success} fail情况下 返回{ type: "fail", code, msg } |
- Source:
extend(target, source, forced)
将源对象(source)的所有可枚举且目标对象(target)不存在的属性, 复制到目标对象
Parameters:
Name | Type | Description |
---|---|---|
target |
object | 目标对象 |
source |
object | 源对象 |
forced |
boolean | 是否强制复制, 为true时将会覆盖目标对象同名属性, 默认为false |
- Source:
getBrowserModel() → {string}
获取浏览器类型和版本
- Source:
Returns:
- Type
- string
Example
getBrowserModel() => "Chrome:70.0.3538.102"
getChangedData(target, source, skipProps, skipPrefix)
将目标对象中和源对象值不同的数据作为键值对返回
Parameters:
Name | Type | Description |
---|---|---|
target |
object | 目标对象 |
source |
object | 源对象 |
skipProps |
Array.<string> | 要忽略比较的属性数组 |
skipPrefix |
string | 要忽略的属性前缀 |
- Source:
getChildren(data, result, pid) → {array}
Parameters:
Name | Type | Description |
---|---|---|
data |
* | 要处理对象 |
result |
* | 返回数组 |
pid |
* | 父id |
- Source:
Returns:
- Type
- array
getCookie(key) → {*}
Parameters:
Name | Type | Description |
---|---|---|
key |
* |
- Source:
Returns:
- Type
- *
getDefaultAvatar(userId)
获取默认头像
Parameters:
Name | Type | Description |
---|---|---|
userId |
* |
- Source:
getHttpBase64(url) → {Promise}
Parameters:
Name | Type | Description |
---|---|---|
url |
string | 图片url |
- Source:
Returns:
图片base64信息
- Type
- Promise
getImgBase64(url) → {Promise}
获取图片的base64 url
Parameters:
Name | Type | Description |
---|---|---|
url |
string | 图片url |
- Source:
Returns:
图片base64信息
- Type
- Promise
getThumbnails(opts) → {string}
图像处理获取缩略图
Parameters:
Name | Type | Description | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
opts |
Object | 参数对象
Properties
|
- Source:
Returns:
处理后的图片路径
- Type
- string
getType(obj) → {string}
获取数据类型(小写) undefined|null|string|number|array|function|date|regexp|window|node|list
Parameters:
Name | Type | Description |
---|---|---|
obj |
object | 要检测的数据 |
- Source:
Returns:
- Type
- string
handleEmoji(str, type) → {string}
处理emoji,用于把用utf16编码的字符转换成实体字符
Parameters:
Name | Type | Description |
---|---|---|
str |
string | 需要编译/解析的字符串 |
type |
string | encode 编译 decode 转义 |
- Source:
Returns:
编译/解析后的字符串
- Type
- string
Example
handleEmoji("😃", "encode") => "😃"
handleEmoji("😊", "decode") => "😊"
handleParam(obj) → {object}
删除对象里面value值为null的键值对
Parameters:
Name | Type | Description |
---|---|---|
obj |
* | 需要处理的参数 |
- Source:
Returns:
返回结果
- Type
- object
handleText(str, type) → {string}
处理文本,客户端无法识别h5的br标签和空格符,因此需要处理br标签为\n和空格符为 ''
Parameters:
Name | Type | Description |
---|---|---|
str |
string | 需要编译/转义的字符串 |
type |
string | encode 编译 decode 转义 |
- Source:
Returns:
编译/转义后的字符串
- Type
- string
Example
handleText("<br> <>", "encode") => "\n <>"
handleText("\n <>", "decode") => "<br> <>"
isArray(obj) → {boolean}
检测是否为数组
Parameters:
Name | Type | Description |
---|---|---|
obj |
object | 要检测的数据 |
- Source:
Returns:
- Type
- boolean
isArray(obj)
检测是否为数组
Parameters:
Name | Type | Description |
---|---|---|
obj |
object | 要检测的数据 |
- Source:
isArrayLike(obj) → {boolean}
检测是否为数组或类数组
Parameters:
Name | Type | Description |
---|---|---|
obj |
object | 要检测的数据 |
- Source:
Returns:
- Type
- boolean
isFunc(fn) → {boolean}
检测是否为函数
Parameters:
Name | Type | Description |
---|---|---|
fn |
object | 要检测的数据 |
- Source:
Returns:
- Type
- boolean
isInt(n, min, max)
检测是否为整数(n必须为数字类型)
Parameters:
Name | Type | Description |
---|---|---|
n |
number | 数字 |
min |
number | undefined | 允许的最小值 |
max |
number | undefined | 允许的最大值 |
- Source:
isNum(n, min, max, max_decimal_len)
检测是否是符合条件的数字(n必须为数字类型)
Parameters:
Name | Type | Description |
---|---|---|
n |
number | 数字 |
min |
number | undefined | 允许的最小值 |
max |
number | undefined | 允许的最大值 |
max_decimal_len |
number | undefined | 最大小数位数 |
- Source:
isObject(obj) → {boolean}
检测是否为对象
Parameters:
Name | Type | Description |
---|---|---|
obj |
object | 要检测的数据 |
- Source:
Returns:
- Type
- boolean
isObject(obj)
检测是否为对象
Parameters:
Name | Type | Description |
---|---|---|
obj |
object | 要检测的数据 |
- Source:
isObjEmpty(target)
判断Object array是否是空对象
Parameters:
Name | Type | Description |
---|---|---|
target |
any |
- Source:
isUInt(n)
检测是否为大于0的整数
Parameters:
Name | Type | Description |
---|---|---|
n |
number | 数字 |
- Source:
isUNum(n)
检测是否为大于0的数字(n必须为数字类型)
Parameters:
Name | Type | Description |
---|---|---|
n |
number | 数字 |
- Source:
makeArray(obj, from)
将类数组对象转为数组,若对象不存在,则返回空数组
Parameters:
Name | Type | Description |
---|---|---|
obj |
Array | arguments | NodeList | 数组或类数组 |
from |
number | 开始索引,默认为0 |
- Source:
objectConversionToList(target, formattedObject, options)
目标和对象组合转换成数组 target{name:'1',age:'58'} formattedObject{name:'你好',age:'年龄'} => [{label:'你好':,value:'1'},{label:'年龄':,value:'58'}]
Parameters:
Name | Type | Description |
---|---|---|
target |
目标对象 | |
formattedObject |
格式化对象 | |
options |
K/V |
- Source:
objectToArray(target)
对象to 对象数组
Parameters:
Name | Type | Description |
---|---|---|
target |
object |
- Source:
parseTime(time, cFormat) → {*}
Parameters:
Name | Type | Description |
---|---|---|
time |
* | 時間戳 |
cFormat |
* | 轉換後的格式 |
- Source:
Returns:
格式化后的時間
- Type
- *
setVideoPlay(id) → {*}
Parameters:
Name | Type | Description |
---|---|---|
id |
* | video的dom的id |
- Source:
Returns:
- Type
- *
sleepSync(ms) → {Promise.<void>}
睡眠
Parameters:
Name | Type | Description |
---|---|---|
ms |
number | string |
- Source:
Returns:
- Type
- Promise.<void>
sortMapByKey(map, isKeyUpSort)
根据对象key排序
Parameters:
Name | Type | Description |
---|---|---|
map |
object | 排序对象 |
isKeyUpSort |
boolean | 排序方式 默认顺序 |
- Source:
storage(type, useCookie) → {object}
Parameters:
Name | Type | Description |
---|---|---|
type |
* | type:localStorage | sessionStorage |
useCookie |
* | 是否用cookie 模拟store |
- Source:
Returns:
- Type
- object
titleize(str) → {string}
Parameters:
Name | Type | Description |
---|---|---|
str |
string |
- Source:
Returns:
- Type
- string
toArray(obj, from)
转为数组
Parameters:
Name | Type | Description |
---|---|---|
obj |
Array | NodeList | 数组或类数组 |
from |
number | 开始索引,默认为0 |
- Source:
toLower(str, defValue)
将字符串转为小写,若str不是字符串,则返回defValue
Parameters:
Name | Type | Description |
---|---|---|
str |
string | 字符串 |
defValue |
string | str不是字符串时返回的值 |
- Source:
toMap(list, fv, ignoreCase)
将数组或类数组转换为键值对 eg: ['a','b'] => {a:0,b:1}
Parameters:
Name | Type | Description |
---|---|---|
list |
Array | 要转换的数组 |
fv |
object | function | 默认值或处理函数(value,i) => [key,value] |
ignoreCase |
boolean | 是否忽略大小写, 为true时将统一使用小写, 默认为false |
- Source:
toObjectMap(list, propKey, propValue)
将对象数组转换为键值对 eg: [{name:'a',value:1},{name:'b',value:2}] => {a:1,b:2}
Parameters:
Name | Type | Description |
---|---|---|
list |
array | 要转换的对象数组 |
propKey |
string | 对象中作为键的属性 eg: name |
propValue |
string | boolean | 对象中作为值的属性, 为true时将给对象增加index属性, 为空时将整个对象作为值 |
- Source:
toSimplifiedChinese(num) → {string}
阿拉伯数字转换为简写汉字
Parameters:
Name | Type | Description |
---|---|---|
num |
number |
- Source:
Returns:
- Type
- string
toUpper(str, defValue)
将字符串转为大写,若str不是字符串,则返回defValue
Parameters:
Name | Type | Description |
---|---|---|
str |
string | 字符串 |
defValue |
string | str不是字符串时返回的值 |
- Source:
unescapeHTML(str) → {string}
Parameters:
Name | Type | Description |
---|---|---|
str |
string |
- Source:
Returns:
- Type
- string