Module: utils
一些常用辅助函数
Functions
alphaId
▸ alphaId(len?
): string
生成一个指定长度的alphaId并返回。id内容由随机字母表字符组成
Example
// urN-k0mpetBwboeQconsole.log(_.alphaId())// Ii6cPyfw-Ql5YC8OIhVwH1lpGY9xconsole.log(_.alphaId(28))
Since
1.4.0
Parameters
Name | Type | Description |
---|---|---|
len? | number | id长度 |
Returns
string
alphaId
defaultTo
▸ defaultTo<T
, V
>(v
, defaultValue
): T
| V
如果v是null/undefined/NaN中的一个,返回defaultValue
Example
//"x"console.log(_.defaultTo(null,'x'))//0console.log(_.defaultTo(0,'y'))
Since
0.16.0
Type parameters
Name |
---|
T |
V |
Parameters
Name | Type | Description |
---|---|---|
v | T | 任何值 |
defaultValue | V | 任何值 |
Returns
T
| V
v或defaultValue
identity
▸ identity(v
): any
返回参数列表中的第一个值,即f(x) = x
。该函数可以用来为高阶函数提供数据如过滤列表或map,也用作默认迭代器
Example
//[1,2,4,'a','1']console.log(_.filter([0,1,false,2,4,undefined,'a','1','',null],_.identity))const list = [ {name:'a',value:1}, {name:'b',value:2}, {name:'c',value:3}]//listconsole.log(_.map(list,_.identity))
Since
0.17.0
Parameters
Name | Type |
---|---|
v | any |
Returns
any
第一个参数
info
▸ info(): void
如果忘了文档地址可以执行这个函数
Since
2.0.0
Returns
void
iteratee
▸ iteratee(value
): Function
创建一个函数,函数类型根据参数值类型而定。创建的函数常用于迭代回调,在Func.js内部被大量使用
Example
const libs = [ {name:'func.js',platform:['web','nodejs'],tags:{utils:true},js:true}, {name:'juth2',platform:['web','java'],tags:{utils:false,middleware:true},js:false}, {name:'soya2d',platform:['web'],tags:{utils:true},js:false}];//[{func.js...}] 如果参数是object,返回_.matcherconsole.log(_.filter(libs,_.iteratee({tags:{utils:true},js:true})))//[func.js,juth2,soya2d] 如果参数是字符串,返回_.propconsole.log(_.map(libs,_.iteratee('name')))//[true,false,true] 如果参数是数组,内容会转为path,并返回_.propconsole.log(_.map(libs,_.iteratee(['tags','utils'])))//[1,3,5] 如果参数是函数,返回这个函数console.log(_.filter([1,2,3,4,5],_.iteratee(n=>n%2)))//[1,2,4,'a','1'] 无参返回_.identityconsole.log(_.filter([0,1,false,2,4,undefined,'a','1','',null],_.iteratee()))
Since
0.17.0
Parameters
Name | Type | Description |
---|---|---|
value | Function | NonFuncItee | 迭代模式 当value是字符串类型时,返回_.prop 当value是对象类型时,返回_.matcher 当value是数组类型时,内容会转为path,并返回_.prop 当value是函数时,返回这个函数 当value未定义时,返回_.identity 其他类型返回f() = false |
Returns
Function
不同类型的返回函数
matcher
▸ matcher<T
>(props
): (obj
: T
) => boolean
创建一个函数,该函数接收一个对象为参数并返回对该对象使用props进行验证的的断言结果。
Example
const libs = [ {name:'func.js',platform:['web','nodejs'],tags:{utils:true},js:true}, {name:'juth2',platform:['web','java'],tags:{utils:false,middleware:true},js:false}, {name:'soya2d',platform:['web'],tags:{utils:true},js:false}];//[{func.js...}]console.log(_.filter(libs,_.matcher({tags:{utils:true},js:true})))
Since
0.17.0
Type parameters
Name | Type |
---|---|
T | extends Object |
Parameters
Name | Type | Description |
---|---|---|
props | T | 断言条件对象 |
Returns
fn
matcher(v)函数
▸ (obj
): boolean
Parameters
Name | Type |
---|---|
obj | T |
Returns
boolean
noConflict
▸ noConflict(): Record
<string
, any
>
当通过非esm方式引用函数库时,函数库会默认挂载全局变量_
。
如果项目中存在其它以该变量为命名空间的函数库(如lodash、underscore等)则会发生命名冲突。
该函数可恢复全局变量为挂载前的引用,并返回func.js命名空间
仅在UMD模式中可用
Example
// 返回func.js并重置全局命名空间 _console.log(_.noConflict())
Since
2.0.0
Returns
Record
<string
, any
>
返回func.js命名空间
noop
▸ noop(): undefined
永远返回undefined
Example
//undefinedconsole.log(_.noop('func'))//undefinedconsole.log(_.noop())
Since
0.16.0
Returns
undefined
undefined
snowflakeId
▸ snowflakeId(nodeId
, epoch?
): string
生成一个64bit整数的雪花id并返回,具体格式如下:
0 - timestamp - nodeId - sequence
可用于客户端生成可跟踪统计的id,如定制终端
0 - 0000000000 0000000000 0000000000 0000000000 0 - 0000000000 - 000000000000
Example
// 343155438738309188console.log(_.snowflakeId(123))// 78249955004317758console.log(_.snowflakeId(456,new Date(2022,1,1).getTime()))
Since
1.4.0
Parameters
Name | Type | Description |
---|---|---|
nodeId | number | 节点id,10bit整数 |
epoch? | number | 时间起点,用于计算相对时间戳 |
Returns
string
snowflakeId 由于js精度问题,直接返回字符串而不是number,如果nodeId为空返回 '0000000000000000000'
times
▸ times(n
, iteratee
): any
调用iteratee函数n次,并将历次调用的返回值数组作为结果返回
Example
//['0',...,'4']console.log(_.times(5,String))//[[0],[1]]console.log(_.times(2,_.toArray))
Since
0.17.0
Parameters
Name | Type | Description |
---|---|---|
n | number | 迭代次数 |
iteratee | (n : number ) => any | 每次迭代调用函数 |
Returns
any
返回值数组
toPath
▸ toPath(path
): string
解析path并返回数组
Example
//['a', 'b', '2', 'c']console.log(_.toPath('a.b[2].c'))//['a', 'b', 'c', '1']console.log(_.toPath(['a','b','c[1]']))//['1']console.log(_.toPath(1))
Since
0.16.0
Parameters
Name | Type | Description |
---|---|---|
path | string | number | (string | number ) | 属性路径,可以是数字索引,字符串key,或者多级属性数组 |
Returns
string
path数组
uniqueId
▸ uniqueId(prefix?
): string
返回一个全局的整数id,序号从0开始。可以用于前端列表编号等用途
Example
//func_0console.log(_.uniqueId('func'))//1console.log(_.uniqueId())
Since
0.16.0
Parameters
Name | Type | Description |
---|---|---|
prefix? | string | id前缀 |
Returns
string
唯一id
uuid
▸ uuid(delimiter?
): string
生成一个32/36个字符组件的随机uuid(v4)并返回
Example
// ddfd73a5-62ac-4412-ad2b-fd495f766cafconsole.log(_.uuid(true))// ddfd73a562ac4412ad2bfd495f766cafconsole.log(_.uuid())
Since
1.4.0
Parameters
Name | Type | Description |
---|---|---|
delimiter? | boolean | 是否生成分隔符 |
Returns
string
uuid