Skip to content

Module: utils/src/modules/__indexUtility

Functions

constant

constant<V>(value): () => V

返回一个函数,函数返回给定值自身。

Example

ts
let stooge = { name: 'moe' }
stooge === constant(stooge)()
=> true
let stooge = { name: 'moe' }
stooge === constant(stooge)()
=> true

Type parameters

Name
V

Parameters

NameTypeDescription
valueV给定值

Returns

fn

▸ (): V

Returns

V


identity

identity<V>(value): V

返回给定值,相当于数学里的:f(x) = x。可以作为默认的迭代器iterator

Example

ts
let stooge = { name: 'moe' }
stooge === identity(stooge)
=> true
let stooge = { name: 'moe' }
stooge === identity(stooge)
=> true

Type parameters

Name
V

Parameters

NameTypeDescription
valueV给定值

Returns

V


iteratee

iteratee<V, C>(value, context?): TypeOfIteratee<V, C, number>

返回给定值通过_baseIteratee转换后的迭代器,支持许多常见回调用例的简写语法。根据值的类型,Iteratee将返回:

Example

ts
// 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

NameTypeDescription
valueV给定值
context?C上下文对象,若传递,则作为迭代器函数的执行上下文 this

Returns

TypeOfIteratee<V, C, number>


noop

noop(): undefined

返回undefined,无论传递给它的是什么参数。 可以用作默认可选的回调参数。

Example

ts
noop()
=> undefined
noop()
=> undefined

Returns

undefined


now

now(): number

返回当前时间的时间戳。

Example

ts
now()
=> 1392066795351
now()
=> 1392066795351

Returns

number


random

random(min, max?): number

返回一个minmax之间的随机整数。如果只传递一个参数,那么将返回0和这个参数之间的整数。

Example

ts
random(0, 100);
=> 4
random(0, 100);
=> 4

Parameters

NameTypeDescription
minnumber最小值
max?number最大值

Returns

number


result

result(object, path?, callBackOrDefault?): any

返回给定对象检索路径属性值,如果属性值是函数,则在给定对象上下文调用并返回值,其余类型直接返回。 未检索到属性值情况下,如果传入了callBackOrDefault参数且为回调函数时,在给定对象上下文调用并返回值,其余类型作为默认值返回,

Example

ts
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

NameTypeDescription
objectDictionary<any>给定对象
path?string | string[]检索路径
callBackOrDefault?unknown回调函数 or 默认值

Returns

any


swap

swap<V>(list, leftKey, rightKey): void

将给定集合中两个给定键的值进行互换。

Example

ts
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

NameType
Vextends Collection

Parameters

NameTypeDescription
listV给定集合
leftKeystring | number需交换值的键
rightKeystring | number需交换值的键

Returns

void


times

times<TResult>(n, iteratee?, context?): TResult[]

返回一个数组,数组由给定函数调用n次的返回值组成,每一次调用给定函数时传递index参数。

Example

ts
times(3, function(n){ return n })
=> [0, 1, 2]
times(3, function(n){ return n })
=> [0, 1, 2]

Type parameters

Name
TResult

Parameters

NameTypeDescription
nnumber迭代次数
iteratee?(n: number) => TResult迭代器函数:(index) => unknown
context?unknown上下文对象

Returns

TResult[]


uniqueId

uniqueId(prefix?): string

返回一个字符串,字符串由prefix加上全局唯一的id组成,主要用在需要为客户端模型或DOM元素生成一个全局唯一的id时。

Example

ts
uniqueId('contact_')
=> 'contact_104
uniqueId('contact_')
=> 'contact_104

Parameters

NameTypeDescription
prefix?stringid 前缀

Returns

string


wait

wait(wait): Promise<unknown>

返回一个promise对象,类似setTimeout,等待wait毫秒,可附加await暂停函数执行等待。

Example

ts
await _.wait(1000)
=> // 一秒钟后执行
await _.wait(1000)
=> // 一秒钟后执行

Parameters

NameTypeDescription
waitnumber等待毫秒数

Returns

Promise<unknown>