Module: utils/src/modules/__indexObjects
Functions
allKeys
▸ allKeys(object
): string
[]
返回一个数组,数组由给定对象自身的、继承的、可枚举的字符串键属性名组成。
Example
function Stooge(name) {
this.name = name
}
Stooge.prototype.silly = true
allKeys(new Stooge('Moe'))
=> ['name', 'silly']
function Stooge(name) {
this.name = name
}
Stooge.prototype.silly = true
allKeys(new Stooge('Moe'))
=> ['name', 'silly']
Parameters
Name | Type | Description |
---|---|---|
object | Dictionary <any > | 给定对象 |
Returns
string
[]
clone
▸ clone<V
>(value
): V
返回给定值的浅拷贝。任何嵌套的对象或数组都通过引用拷贝。
Example
const storage = { name: { deep: { ... } } }
storage.deep === cloneDeep(storage).deep
=> true
const storage = { name: { deep: { ... } } }
storage.deep === cloneDeep(storage).deep
=> true
Type parameters
Name |
---|
V |
Parameters
Name | Type | Description |
---|---|---|
value | V | 给定值 |
Returns
V
cloneDeep
▸ cloneDeep<V
>(value
): V
推荐原生:structuredClone。 返回给定值的深拷贝。任何嵌套的对象或数组都通过值拷贝,对于不可拷贝的对象,例如function
则返回{}
。
Example
const storage = { name: { deep: { ... } } }
storage.deep === cloneDeep(storage).deep
=> false
const storage = { name: { deep: { ... } } }
storage.deep === cloneDeep(storage).deep
=> false
Type parameters
Name |
---|
V |
Parameters
Name | Type | Description |
---|---|---|
value | V | 给定值 |
Returns
V
create
▸ create(prototype
, props?
): any
推荐原生:Object.create。 返回一个对象,对象以给定对象作为原型,可附加props
作为对象的属性。基本上,和Object.create
一样,但是没有所有的属性描述符。
Example
create(Stooge.prototype, { name: 'Moe' })
=> { name: 'Moe', __proto__: Stooge.protoType }
create(Stooge.prototype, { name: 'Moe' })
=> { name: 'Moe', __proto__: Stooge.protoType }
Parameters
Name | Type | Description |
---|---|---|
prototype | unknown | 给定对象 |
props? | object | 附加属性对象 |
Returns
any
defaults
▸ defaults(object
, ...handles
): Dictionary
<any
>
返回给定对象,使用handles
对象填充给定对象中相应键值为undefined
的属性。一旦undefined
属性被填充,再使用defaults
方法将不会有任何效果。
Example
const iceCream = { flavor: 'chocolate' }
defaults(iceCream, { flavor: 'vanilla', sprinkles: 'lots' })
=> { flavor: 'chocolate', sprinkles: 'lots' }
const iceCream = { flavor: 'chocolate' }
defaults(iceCream, { flavor: 'vanilla', sprinkles: 'lots' })
=> { flavor: 'chocolate', sprinkles: 'lots' }
Parameters
Name | Type | Description |
---|---|---|
object | Dictionary <any > | 给定对象 |
...handles | Dictionary <any >[] | 填充对象 |
Returns
Dictionary
<any
>
entries
▸ entries<V
>(object
): [string
, TypeOfDictionary
<V
>][]
推荐原生:Object.entries。 返回一个数组,数组由给定对象自身可枚举属性的键-值对组成,其排列与使用for...in
循环遍历该对象时返回的顺序一致(区别在于for-in
循环还会枚举原型链中的属性)。object
的反函数。
Example
entries({ one: 1, two: 2, three: 3 })
=> [['one', 1], ['two', 2], ['three', 3]]
entries({ one: 1, two: 2, three: 3 })
=> [['one', 1], ['two', 2], ['three', 3]]
Type parameters
Name | Type |
---|---|
V | extends Dictionary <any , V > |
Parameters
Name | Type | Description |
---|---|---|
object | V | 给定对象 |
Returns
[string
, TypeOfDictionary
<V
>][]
extend
▸ extend(object
, ...handles
): Dictionary
<any
>
返回给定对象,使用handles
对象中的所有可枚举的、可继承的属性覆盖到给定对象上。复制是按顺序的,所以后面的对象属性会把前面的对象属性覆盖掉(如果有重复)。
Example
extend({ name: 'moe' }, { age: 50 })
=> { name: 'moe', age: 50 }
extend({ name: 'moe' }, { age: 50 })
=> { name: 'moe', age: 50 }
Parameters
Name | Type | Description |
---|---|---|
object | Dictionary <any > | 给定对象 |
...handles | Dictionary <any >[] | 覆盖对象 |
Returns
Dictionary
<any
>
extendOwn
▸ extendOwn(object
, ...handles
): any
推荐原生:Object.assign。 返回给定对象,使用handles
对象中所有可枚举的自有属性覆盖到给定对象上。
Example
extendOwn({}, { a: 1 })
=> { a: 1 }
extendOwn({}, { a: 1 })
=> { a: 1 }
Parameters
Name | Type | Description |
---|---|---|
object | Dictionary <any > | 给定对象 |
...handles | Dictionary <any >[] | 覆盖对象 |
Returns
any
findKey
▸ findKey<V
, I
>(object
, predicate?
, context?
): undefined
| string
返回给定对象中第一个通过predicate
真值检测的键,如果没有元素通过检测则返回undefined
。
Example
findKey({a: 1}, (value) => value === 1)
=> a
findKey({a: 1}, (value) => value === 1)
=> a
Type parameters
Name | Type |
---|---|
V | extends Dictionary <any , V > |
I | extends Iteratee <V , boolean > |
Parameters
Name | Type | Description |
---|---|---|
object | V | 给定对象 |
predicate? | I | 谓语迭代器函数,通过 iteratee 进行转换,以简化速记语法 |
context? | unknown | 上下文对象,若传递,则作为谓语迭代器函数的执行上下文 this |
Returns
undefined
| string
functions
▸ functions(object
): string
[]
返回一个数组,数组由给定对象中类型为function
的键组成。
Example
functions(_)
=> ['all', 'unknown', 'bind', 'bindAll', 'clone', 'compact', 'compose' ...]
functions(_)
=> ['all', 'unknown', 'bind', 'bindAll', 'clone', 'compact', 'compose' ...]
Parameters
Name | Type | Description |
---|---|---|
object | Dictionary <any > | 给定对象 |
Returns
string
[]
get
▸ get<V
, U
>(object
, path
, defaultValue?
): U
| TypeOfDictionary
<V
>
返回给定对象指定path
(使用toPath
转换)键的值,如果指定键不存在则返回defaultValue
。
Example
const stooge = { name: 'moe' }
get(stooge, 'name')
=> 'moe'
const stooge = { name: 'moe' }
get(stooge, 'name')
=> 'moe'
Type parameters
Name | Type |
---|---|
V | extends Collection |
U | U |
Parameters
Name | Type | Description |
---|---|---|
object | V | 给定对象 |
path | unknown | 检索路径 |
defaultValue? | U | 默认值 |
Returns
U
| TypeOfDictionary
<V
>
has
▸ has(object
, path
): boolean
返回一个布尔值,判断给定对象自身属性中是否具有指定的路径属性(暨是否有指定的键)。 等同于object.hasOwnProperty(key)
,这是使用Object.prototype.hasOwnProperty函数的一个安全引用(此函数可能被意外覆盖)。
Example
has({ a: 1, b: 2, c: { d: 3 } }, ['c', 'd'])
=> true
has({ a: 1, b: 2, c: { d: 3 } }, 'c.d')
=> true
has({ a: 1, b: 2, c: { d: 3 } }, ['c', 'd'])
=> true
has({ a: 1, b: 2, c: { d: 3 } }, 'c.d')
=> true
Parameters
Name | Type | Description |
---|---|---|
object | Dictionary <any > | 给定对象 |
path | unknown | 检索路径 |
Returns
boolean
invert
▸ invert(object
): Dictionary
<any
>
返回一个对象,对象由给定对象键(keys)和值(values)互换后组成。对于这个操作,必须确保给定对象里所有的值都是唯一的且可以序列化成字符串。
Example
invert({ Moe: 'Moses', Larry: 'Louis', Curly: 'Jerome' })
=> { Moses: 'Moe', Louis: 'Larry', Jerome: 'Curly' }
invert({ Moe: 'Moses', Larry: 'Louis', Curly: 'Jerome' })
=> { Moses: 'Moe', Louis: 'Larry', Jerome: 'Curly' }
Parameters
Name | Type | Description |
---|---|---|
object | Dictionary <any > | 给定对象 |
Returns
Dictionary
<any
>
keys
▸ keys(object
): string
[]
推荐原生:Object.keys。 返回一个数组,数组由给定对象自身可枚举的字符串键属性名组成。
Example
keys({ one: 1, two: 2, three: 3 })
=> ['one', 'two', 'three']
keys({ one: 1, two: 2, three: 3 })
=> ['one', 'two', 'three']
Parameters
Name | Type | Description |
---|---|---|
object | Dictionary <any > | 给定对象 |
Returns
string
[]
mapObject
▸ mapObject<V
, I
>(object
, iteratee
, context?
): { [K in string | number | symbol]: IterateeResult<I, V[K]> }
返回一个对象,对象由给定对象每个属性值通过Iteratee
转换后组成。类似于map
,但此方法用于对象。
Example
mapObject({start: 5, end: 12}, function(val, key) {
return val + 5;
});
=> {start: 10, end: 17}
mapObject({start: 5, end: 12}, function(val, key) {
return val + 5;
});
=> {start: 10, end: 17}
Type parameters
Name | Type |
---|---|
V | extends Dictionary <any , V > |
I | extends Iteratee <V , unknown > |
Parameters
Name | Type | Description |
---|---|---|
object | V | 给定对象 |
iteratee | I | 迭代器函数,通过 iteratee 进行转换,以简化速记语法 |
context? | unknown | 上下文对象,若传递,则作为迭代器函数的执行上下文 this |
Returns
{ [K in string | number | symbol]: IterateeResult<I, V[K]> }
matcher
▸ matcher<V
>(properties
): (object
: unknown
) => boolean
返回一个函数,函数判断给定对象是否匹配properties
键-值对。
Example
const ready = matcher({ selected: true, visible: true })
filter(list, ready)
=> [ ... ]
const ready = matcher({ selected: true, visible: true })
filter(list, ready)
=> [ ... ]
Type parameters
Name | Type |
---|---|
V | extends Dictionary <any , V > |
Parameters
Name | Type | Description |
---|---|---|
properties | V | 谓语对象 |
Returns
fn
▸ (object
): boolean
Parameters
Name | Type |
---|---|
object | unknown |
Returns
boolean
omit
▸ omit<V
, K
>(object
, predicate
, context?
): Omit
<V
, K
> | Partial
<V
>
返回一个对象,对象由给定对象中没有通过predicate
真值检测的元素组成。pick
的反函数。
Example
omit({ name: 'moe', age: 50, userid: 'moe1' }, 'userid')
=> { name: 'moe', age: 50 }
omit({ name: 'moe', age: 50, userid: 'moe1' }, ['age', 'userid'])
=> { name: 'moe' }
omit({name: 'moe', age: 50, userid: 'moe1' }, function(value, key, object) {
return isNumber(value)
})
=> { name: 'moe', userid: 'moe1' }
omit({ name: 'moe', age: 50, userid: 'moe1' }, 'userid')
=> { name: 'moe', age: 50 }
omit({ name: 'moe', age: 50, userid: 'moe1' }, ['age', 'userid'])
=> { name: 'moe' }
omit({name: 'moe', age: 50, userid: 'moe1' }, function(value, key, object) {
return isNumber(value)
})
=> { name: 'moe', userid: 'moe1' }
Type parameters
Name | Type |
---|---|
V | extends Dictionary <any , V > |
K | extends string |
Parameters
Name | Type | Description |
---|---|---|
object | V | 给定对象 |
predicate | K | ObjectIterator <TypeOfDictionary <V >, boolean > | K [] | 谓语迭代器函数,通过 iteratee 进行转换,以简化速记语法 |
context? | unknown | 上下文对象,若传递,则作为谓语迭代器函数的执行上下文 this |
Returns
Omit
<V
, K
> | Partial
<V
>
pick
▸ pick<V
, K
>(object
, predicate
, context?
): Pick
<V
, K
> | Partial
<V
>
返回一个对象,对象由给定对象中通过predicate
真值检测的元素组成。
Example
pick({ name: 'moe', age: 50, userid: 'moe1' }, 'age')
=> { age: 50 }
pick({ name: 'moe', age: 50, userid: 'moe1' }, ['name', 'age'])
=> { name: 'moe', age: 50 }
pick({ name: 'moe', age: 50, userid: 'moe1' }, function(value, key, object) {
return isNumber(value)
})
=> { age: 50 }
pick({ name: 'moe', age: 50, userid: 'moe1' }, 'age')
=> { age: 50 }
pick({ name: 'moe', age: 50, userid: 'moe1' }, ['name', 'age'])
=> { name: 'moe', age: 50 }
pick({ name: 'moe', age: 50, userid: 'moe1' }, function(value, key, object) {
return isNumber(value)
})
=> { age: 50 }
Type parameters
Name | Type |
---|---|
V | extends Dictionary <any , V > |
K | extends string |
Parameters
Name | Type | Description |
---|---|---|
object | V | 给定对象 |
predicate | K | ObjectIterator <TypeOfDictionary <V >, boolean > | K [] | 谓语迭代器函数,通过 iteratee 进行转换,以简化速记语法 |
context? | unknown | 上下文对象,若传递,则作为谓语迭代器函数的执行上下文 this |
Returns
Pick
<V
, K
> | Partial
<V
>
property
▸ property(path
): (obj
: Dictionary
<any
>) => undefined
| Dictionary
<any
>
返回一个函数,该函数将返回指定对象的给定检索路径属性(先路径后对象)。path
可以指定为简单的key
,或者指定为对象键或索引键组成的数组,用于深度属性萃取。
Example
const stooge = { name: 'moe' }
'moe' === property('name')(stooge)
=> true
const stooges = { moe: { fears: { worst: 'Spiders' } }, curly: { fears: { worst: 'Moe' } } }
const curlsWorstFear = property(['curly', 'fears', 'worst'])
curlsWorstFear(stooges)
=> 'Moe'
const stooge = { name: 'moe' }
'moe' === property('name')(stooge)
=> true
const stooges = { moe: { fears: { worst: 'Spiders' } }, curly: { fears: { worst: 'Moe' } } }
const curlsWorstFear = property(['curly', 'fears', 'worst'])
curlsWorstFear(stooges)
=> 'Moe'
Parameters
Name | Type | Description |
---|---|---|
path | unknown | 给定检索路径 |
Returns
fn
▸ (obj
): undefined
| Dictionary
<any
>
Parameters
Name | Type |
---|---|
obj | Dictionary <any > |
Returns
undefined
| Dictionary
<any
>
propertyOf
▸ propertyOf(object
): (path
: string
| number
| (string
| number
)[]) => any
返回一个函数,该函数返回指定检索路径属性给定对象的值(先对象后路径),property
的反函数。
Example
const stooge = { name: 'moe' }
propertyOf(stooge)('name')
=> 'moe'
const stooge = { name: 'moe' }
propertyOf(stooge)('name')
=> 'moe'
Parameters
Name | Type | Description |
---|---|---|
object | Dictionary <any > | 给定对象 |
Returns
fn
▸ (path
): any
Parameters
Name | Type |
---|---|
path | string | number | (string | number )[] |
Returns
any
tap
▸ tap<V
>(object
, interceptor
): V
使用给定对象作为参数调用给定函数后返回给定对象。 这种方法的主要意图是作为函数链式调用的一环, 为了对此对象执行操作并返回对象本身。
Example
chain([1,2,3,200])
.filter(function(num) { return num % 2 == 0 })
.tap(alert)
.map(function(num) { return num * num })
.value()
=> // [2, 200] (alerted)
=> [4, 40000]
chain([1,2,3,200])
.filter(function(num) { return num % 2 == 0 })
.tap(alert)
.map(function(num) { return num * num })
.value()
=> // [2, 200] (alerted)
=> [4, 40000]
Type parameters
Name |
---|
V |
Parameters
Name | Type | Description |
---|---|---|
object | V | 给定对象 |
interceptor | Func <any > | 给定函数 |
Returns
V
values
▸ values<V
>(object
): TypeOfDictionary
<V
>[]
推荐原生:Object.values。 返回一个数组,数组由给定对象自有可枚举字符串键属性值组成。
Example
values({ one: 1, two: 2, three: 3 })
=> [1, 2, 3]
values({ one: 1, two: 2, three: 3 })
=> [1, 2, 3]
Type parameters
Name | Type |
---|---|
V | extends Dictionary <any , V > |
Parameters
Name | Type | Description |
---|---|---|
object | V | 给定对象 |
Returns
TypeOfDictionary
<V
>[]