Namespace: util

util

NIM util 工具方法, 通过 NIM.util 来获取此工具的引用

Methods


<static> asyncPool(poolLimit, array, iteratorFn)

搬自 https://github.com/rxaviers/async-pool

Parameters:
Name Type Description
poolLimit number

并发限制数

array array

迭代数据参数

iteratorFn function

迭代器

Returns:

使用示例

const timeout = i => new Promise(resolve => setTimeout(() => resolve(i), i));
await asyncPool(2, [1000, 5000, 3000, 2000], timeout);


<static> cutObjArray(base, arr1, arr2, options)

从数组里面去除某些项

Parameters:
Name Type Description
base Array

基数组

arr1 Array.<Object>

待去除的数组

arr2 Array.<Object>

待去除的数组

options Object

配置参数

Properties
Name Type Argument Default Description
keyPath String <optional>
'id'

keyPath, 去重的字段, 默认为 id

Returns:

去除后的数组

Type
Array
Example
var olds = [
    { account: 'a' },
    { account: 'b' },
    { account: 'c' }
];
var invalids = [
    { account: 'b' }
];
var options = {
    keyPath: 'account'
};
var array = NIM.util.cutObjArray(olds, invalids, options);
// array 为
// [
//     { account: 'a' },
//     { account: 'c' }
// ]

<static> findObjInArray(array, options)

在数组里面找 keyPath 对应的属性值为 value 的元素

  • 数组的每一项均为对象, 并且必须有由 keyPath 指定的属性
Parameters:
Name Type Description
array Array.<Object>

待查找的数组

options Object

查找的条件

Properties
Name Type Argument Description
keyPath String <optional>

keyPath, 匹配的字段, 默认为 'id'

value Anything <optional>

匹配的值

Returns:

找到的元素, 或者 null

Type
Object
Example
var array = [
    {name: 'tom'},
    {name: 'jack'},
    {name: 'dan'}
];
var obj = NIM.util.findObjInArray(array, {
    keyPath: 'name',
    value: 'jack'
});
// obj 为 {name: 'jack'}

<static> guid()

生成一个 32 位的 GUID/UUID

Returns:

guid/uuid

Type
String

<static> mergeObjArray(arr1, arr2 [, options])

合并数组

  • 此方法接收不定量参数
    • 最后一个参数如果是对象, 那么就是配置参数
    • 除了配置参数之外, 所有其它的参数都必须是数组, 它们都会被合并
  • 如果两个对象keyPath字段对应的属性值相同, 后面的对象会被合并到前面的对象
Parameters:
Name Type Argument Description
arr1 Array.<Object>

待合并的数组

arr2 Array.<Object>

待合并的数组

options Object <optional>

配置参数

Properties
Name Type Argument Default Description
keyPath String <optional>
'id'

keyPath, 去重的字段, 默认为 id

notSort Boolean <optional>

是否要排序, 默认false要排序, 传true则不排序

compare function <optional>

决定排序的方法, 如果不提供, 那么使用 NIM.naturalSort 进行排序

sortPath String <optional>

sortPath, 排序用的字段, 默认为 keyPath

insensitive Boolean <optional>

排序时是否不区分大小写, 默认区分

desc Boolean <optional>

是否逆序, 默认正序

Returns:

合并并排序后的数组

Type
Array.<Object>
Example
var arr1 = [
    {
        account: 'tom',
        name: 'T'
    }
];
var arr2 = [
    {
        account: 'adam'
    },
    {
        account: 'tom',
        name: 'T-new'
    }
];
var options = {
    keyPath: 'account'
};
var resultArray = NIM.util.mergeObjArray(arr1, arr2, options);
// resultArray为
// [
//     {account: 'adam'},
//     {account: 'tom', name: 'T-new'},
// ]

<static> sortObjArray(array [, options])

返回排序后的数组

  • 数组的每一项都为 Object, 并且必须有由 sortPath 指定的属性
Parameters:
Name Type Argument Description
array Array.<Object>

待排序的数组

options Object <optional>

配置参数

Properties
Name Type Argument Description
compare function <optional>

决定排序的方法, 如果不提供, 那么使用 NIM.naturalSort 进行排序

sortPath String <optional>

sortPath, 排序用的字段, 默认为 id

insensitive Boolean <optional>

排序时是否不区分大小写, 默认区分

desc Boolean <optional>

是否逆序, 默认正序

Returns:

排序后的数组

Type
Array.<Object>
Example
var array = [
    { account: 'b' },
    { account: 'a' }
];
var options = {
    sortPath: 'account'
};
NIM.util.sortObjArray(array, options);
// array 为
//[
//    { account: 'a' },
//    { account: 'b' }
//]

<static> throttle(func, wait, options, InWaitFunc)

节流、频率控制 返回函数连续调用时,func 执行频率限定为 次 / wait

Parameters:
Name Type Description
func function

传入函数

wait number

表示时间窗口的间隔

options object

如果想忽略开始边界上的调用,传入{leading: false}。
如果想忽略结尾边界上的调用,传入{trailing: false}

InWaitFunc function

如果当前在wait时间内调用,则调用该函数

Returns:

返回客户调用函数

Type
function

<static> validate(rules, params, callFunc)

校验函数,参照 parameter 库的调用方法

Parameters:
Name Type Description
rules object

规则

params params

待校验的对象

callFunc string

调用函数名

rules 的格式如下
{
keyword: { type: 'string' },
sessionLimit: { type: 'number', min: 0, required: false },
msgLimit: { type: 'number', min: 0, required: false },
order: { type: 'enum', values: ['ASC', 'DESC'], required: false },
p2pList: { type: 'string', allowEmpty: false, required: false },
teamList: { type: 'string', allowEmpty: false, required: false },
senderList: { type: 'string', allowEmpty: false, required: false },
}

Returns:

返回过滤后的参数

Type
object