Module: utils/src/modules/__indexUtility
Functions
constant
▸ constant<V
>(value
): () => V
返回一个函数,函数返回给定值自身。
Example
let stooge = { name: 'moe' }
stooge === constant(stooge)()
=> true
let stooge = { name: 'moe' }
stooge === constant(stooge)()
=> true
Type parameters
Name |
---|
V |
Parameters
Name | Type | Description |
---|---|---|
value | V | 给定值 |
Returns
fn
▸ (): V
Returns
V
identity
▸ identity<V
>(value
): V
返回给定值,相当于数学里的:f(x) = x
。可以作为默认的迭代器iterator
。
Example
let stooge = { name: 'moe' }
stooge === identity(stooge)
=> true
let stooge = { name: 'moe' }
stooge === identity(stooge)
=> true
Type parameters
Name |
---|
V |
Parameters
Name | Type | Description |
---|---|---|
value | V | 给定值 |
Returns
V
iteratee
▸ iteratee<V
, C
>(value
, context?
): TypeOfIteratee
<V
, C
, number
>
返回给定值通过_baseIteratee
转换后的迭代器,支持许多常见回调用例的简写语法。根据值的类型,Iteratee
将返回:
Example
// No value
Iteratee()
=> identity()
// Function
Iteratee(function(n) { return n * 2 })
=> function(n) { return n * 2 }
// Object
Iteratee({ firstName: 'Chelsea' })
=> matcher({ firstName: 'Chelsea' })
// other
Iteratee('firstName')
=> property('firstName')
// No value
Iteratee()
=> identity()
// Function
Iteratee(function(n) { return n * 2 })
=> function(n) { return n * 2 }
// Object
Iteratee({ firstName: 'Chelsea' })
=> matcher({ firstName: 'Chelsea' })
// other
Iteratee('firstName')
=> property('firstName')
Type parameters
Name |
---|
V |
C |
Parameters
Name | Type | Description |
---|---|---|
value | V | 给定值 |
context? | C | 上下文对象,若传递,则作为迭代器函数的执行上下文 this |
Returns
TypeOfIteratee
<V
, C
, number
>
noop
▸ noop(): undefined
返回undefined
,无论传递给它的是什么参数。 可以用作默认可选的回调参数。
Example
noop()
=> undefined
noop()
=> undefined
Returns
undefined
now
▸ now(): number
返回当前时间的时间戳。
Example
now()
=> 1392066795351
now()
=> 1392066795351
Returns
number
random
▸ random(min
, max?
): number
返回一个min
和max
之间的随机整数。如果只传递一个参数,那么将返回0
和这个参数之间的整数。
Example
random(0, 100);
=> 4
random(0, 100);
=> 4
Parameters
Name | Type | Description |
---|---|---|
min | number | 最小值 |
max? | number | 最大值 |
Returns
number
result
▸ result(object
, path?
, callBackOrDefault?
): any
返回给定对象检索路径属性值,如果属性值是函数,则在给定对象上下文调用并返回值,其余类型直接返回。 未检索到属性值情况下,如果传入了callBackOrDefault
参数且为回调函数时,在给定对象上下文调用并返回值,其余类型作为默认值返回,
Example
const object = { cheese: 'crumpets', stuff: function() { return 'nonsense' } }
result(object, 'cheese')
=> 'crumpets'
result(object, 'stuff')
=> 'nonsense'
result(object, 'meat', 'ham')
=> 'ham'
const object = { cheese: 'crumpets', stuff: function() { return 'nonsense' } }
result(object, 'cheese')
=> 'crumpets'
result(object, 'stuff')
=> 'nonsense'
result(object, 'meat', 'ham')
=> 'ham'
Parameters
Name | Type | Description |
---|---|---|
object | Dictionary <any > | 给定对象 |
path? | string | string [] | 检索路径 |
callBackOrDefault? | unknown | 回调函数 or 默认值 |
Returns
any
swap
▸ swap<V
>(list
, leftKey
, rightKey
): void
将给定集合中两个给定键的值进行互换。
Example
var arr = [1, 2]
swap(arr, 0, 1);
=> [2, 1]
var obj = {pid: 1, uid: 2}
swap(obj, 'pid', 'uid')
=> {pid: 2, uid: 1}
var arr = [1, 2]
swap(arr, 0, 1);
=> [2, 1]
var obj = {pid: 1, uid: 2}
swap(obj, 'pid', 'uid')
=> {pid: 2, uid: 1}
Type parameters
Name | Type |
---|---|
V | extends Collection |
Parameters
Name | Type | Description |
---|---|---|
list | V | 给定集合 |
leftKey | string | number | 需交换值的键 |
rightKey | string | number | 需交换值的键 |
Returns
void
times
▸ times<TResult
>(n
, iteratee?
, context?
): TResult
[]
返回一个数组,数组由给定函数调用n
次的返回值组成,每一次调用给定函数时传递index
参数。
Example
times(3, function(n){ return n })
=> [0, 1, 2]
times(3, function(n){ return n })
=> [0, 1, 2]
Type parameters
Name |
---|
TResult |
Parameters
Name | Type | Description |
---|---|---|
n | number | 迭代次数 |
iteratee? | (n : number ) => TResult | 迭代器函数:(index) => unknown |
context? | unknown | 上下文对象 |
Returns
TResult
[]
uniqueId
▸ uniqueId(prefix?
): string
返回一个字符串,字符串由prefix
加上全局唯一的id
组成,主要用在需要为客户端模型或DOM
元素生成一个全局唯一的id
时。
Example
uniqueId('contact_')
=> 'contact_104
uniqueId('contact_')
=> 'contact_104
Parameters
Name | Type | Description |
---|---|---|
prefix? | string | id 前缀 |
Returns
string
wait
▸ wait(wait
): Promise
<unknown
>
返回一个promise
对象,类似setTimeout
,等待wait
毫秒,可附加await
暂停函数执行等待。
Example
await _.wait(1000)
=> // 一秒钟后执行
await _.wait(1000)
=> // 一秒钟后执行
Parameters
Name | Type | Description |
---|---|---|
wait | number | 等待毫秒数 |
Returns
Promise
<unknown
>