Skip to content

Module: utils/src/modules/__indexObjects

Functions

allKeys

allKeys(object): string[]

返回一个数组,数组由给定对象自身的、继承的、可枚举的字符串键属性名组成。

Example

ts
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

NameTypeDescription
objectDictionary<any>给定对象

Returns

string[]


clone

clone<V>(value): V

返回给定值的浅拷贝。任何嵌套的对象或数组都通过引用拷贝。

Example

ts
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

NameTypeDescription
valueV给定值

Returns

V


cloneDeep

cloneDeep<V>(value): V

推荐原生:structuredClone。 返回给定值的深拷贝。任何嵌套的对象或数组都通过值拷贝,对于不可拷贝的对象,例如function则返回{}

Example

ts
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

NameTypeDescription
valueV给定值

Returns

V


create

create(prototype, props?): any

推荐原生:Object.create。 返回一个对象,对象以给定对象作为原型,可附加props作为对象的属性。基本上,和Object.create一样,但是没有所有的属性描述符。

Example

ts
create(Stooge.prototype, { name: 'Moe' })
=> { name: 'Moe', __proto__: Stooge.protoType }
create(Stooge.prototype, { name: 'Moe' })
=> { name: 'Moe', __proto__: Stooge.protoType }

Parameters

NameTypeDescription
prototypeunknown给定对象
props?object附加属性对象

Returns

any


defaults

defaults(object, ...handles): Dictionary<any>

返回给定对象,使用handles对象填充给定对象中相应键值为undefined的属性。一旦undefined属性被填充,再使用defaults方法将不会有任何效果。

Example

ts
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

NameTypeDescription
objectDictionary<any>给定对象
...handlesDictionary<any>[]填充对象

Returns

Dictionary<any>


entries

entries<V>(object): [string, TypeOfDictionary<V>][]

推荐原生:Object.entries。 返回一个数组,数组由给定对象自身可枚举属性的键-值对组成,其排列与使用for...in循环遍历该对象时返回的顺序一致(区别在于for-in循环还会枚举原型链中的属性)。object的反函数。

Example

ts
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

NameType
Vextends Dictionary<any, V>

Parameters

NameTypeDescription
objectV给定对象

Returns

[string, TypeOfDictionary<V>][]


extend

extend(object, ...handles): Dictionary<any>

返回给定对象,使用handles对象中的所有可枚举的、可继承的属性覆盖到给定对象上。复制是按顺序的,所以后面的对象属性会把前面的对象属性覆盖掉(如果有重复)。

Example

ts
extend({ name: 'moe' }, { age: 50 })
=> { name: 'moe', age: 50 }
extend({ name: 'moe' }, { age: 50 })
=> { name: 'moe', age: 50 }

Parameters

NameTypeDescription
objectDictionary<any>给定对象
...handlesDictionary<any>[]覆盖对象

Returns

Dictionary<any>


extendOwn

extendOwn(object, ...handles): any

推荐原生:Object.assign。 返回给定对象,使用handles对象中所有可枚举的自有属性覆盖到给定对象上。

Example

ts
extendOwn({}, { a: 1 })
=> { a: 1 }
extendOwn({}, { a: 1 })
=> { a: 1 }

Parameters

NameTypeDescription
objectDictionary<any>给定对象
...handlesDictionary<any>[]覆盖对象

Returns

any


findKey

findKey<V, I>(object, predicate?, context?): undefined | string

返回给定对象中第一个通过predicate真值检测的键,如果没有元素通过检测则返回undefined

Example

ts
findKey({a: 1}, (value) => value === 1)
=> a
findKey({a: 1}, (value) => value === 1)
=> a

Type parameters

NameType
Vextends Dictionary<any, V>
Iextends Iteratee<V, boolean>

Parameters

NameTypeDescription
objectV给定对象
predicate?I谓语迭代器函数,通过 iteratee 进行转换,以简化速记语法
context?unknown上下文对象,若传递,则作为谓语迭代器函数的执行上下文 this

Returns

undefined | string


functions

functions(object): string[]

返回一个数组,数组由给定对象中类型为function的键组成。

Example

ts
functions(_)
=> ['all', 'unknown', 'bind', 'bindAll', 'clone', 'compact', 'compose' ...]
functions(_)
=> ['all', 'unknown', 'bind', 'bindAll', 'clone', 'compact', 'compose' ...]

Parameters

NameTypeDescription
objectDictionary<any>给定对象

Returns

string[]


get

get<V, U>(object, path, defaultValue?): U | TypeOfDictionary<V>

返回给定对象指定path(使用toPath转换)键的值,如果指定键不存在则返回defaultValue

Example

ts
const stooge = { name: 'moe' }
get(stooge, 'name')
=> 'moe'
const stooge = { name: 'moe' }
get(stooge, 'name')
=> 'moe'

Type parameters

NameType
Vextends Collection
UU

Parameters

NameTypeDescription
objectV给定对象
pathunknown检索路径
defaultValue?U默认值

Returns

U | TypeOfDictionary<V>


has

has(object, path): boolean

返回一个布尔值,判断给定对象自身属性中是否具有指定的路径属性(暨是否有指定的键)。 等同于object.hasOwnProperty(key),这是使用Object.prototype.hasOwnProperty函数的一个安全引用(此函数可能被意外覆盖)。

Example

ts
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

NameTypeDescription
objectDictionary<any>给定对象
pathunknown检索路径

Returns

boolean


invert

invert(object): Dictionary<any>

返回一个对象,对象由给定对象键(keys)和值(values)互换后组成。对于这个操作,必须确保给定对象里所有的值都是唯一的且可以序列化成字符串。

Example

ts
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

NameTypeDescription
objectDictionary<any>给定对象

Returns

Dictionary<any>


keys

keys(object): string[]

推荐原生:Object.keys。 返回一个数组,数组由给定对象自身可枚举的字符串键属性名组成。

Example

ts
keys({ one: 1, two: 2, three: 3 })
=> ['one', 'two', 'three']
keys({ one: 1, two: 2, three: 3 })
=> ['one', 'two', 'three']

Parameters

NameTypeDescription
objectDictionary<any>给定对象

Returns

string[]


mapObject

mapObject<V, I>(object, iteratee, context?): { [K in string | number | symbol]: IterateeResult<I, V[K]> }

返回一个对象,对象由给定对象每个属性值通过Iteratee转换后组成。类似于map,但此方法用于对象。

Example

ts
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

NameType
Vextends Dictionary<any, V>
Iextends Iteratee<V, unknown>

Parameters

NameTypeDescription
objectV给定对象
iterateeI迭代器函数,通过 iteratee 进行转换,以简化速记语法
context?unknown上下文对象,若传递,则作为迭代器函数的执行上下文 this

Returns

{ [K in string | number | symbol]: IterateeResult<I, V[K]> }


matcher

matcher<V>(properties): (object: unknown) => boolean

返回一个函数,函数判断给定对象是否匹配properties键-值对。

Example

ts
const ready = matcher({ selected: true, visible: true })
filter(list, ready)
=> [ ... ]
const ready = matcher({ selected: true, visible: true })
filter(list, ready)
=> [ ... ]

Type parameters

NameType
Vextends Dictionary<any, V>

Parameters

NameTypeDescription
propertiesV谓语对象

Returns

fn

▸ (object): boolean

Parameters
NameType
objectunknown
Returns

boolean


omit

omit<V, K>(object, predicate, context?): Omit<V, K> | Partial<V>

返回一个对象,对象由给定对象中没有通过predicate真值检测的元素组成。pick的反函数。

Example

ts
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

NameType
Vextends Dictionary<any, V>
Kextends string

Parameters

NameTypeDescription
objectV给定对象
predicateK | 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

ts
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

NameType
Vextends Dictionary<any, V>
Kextends string

Parameters

NameTypeDescription
objectV给定对象
predicateK | 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

ts
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

NameTypeDescription
pathunknown给定检索路径

Returns

fn

▸ (obj): undefined | Dictionary<any>

Parameters
NameType
objDictionary<any>
Returns

undefined | Dictionary<any>


propertyOf

propertyOf(object): (path: string | number | (string | number)[]) => any

返回一个函数,该函数返回指定检索路径属性给定对象的值(先对象后路径),property的反函数。

Example

ts
const stooge = { name: 'moe' }
propertyOf(stooge)('name')
=> 'moe'
const stooge = { name: 'moe' }
propertyOf(stooge)('name')
=> 'moe'

Parameters

NameTypeDescription
objectDictionary<any>给定对象

Returns

fn

▸ (path): any

Parameters
NameType
pathstring | number | (string | number)[]
Returns

any


tap

tap<V>(object, interceptor): V

使用给定对象作为参数调用给定函数后返回给定对象。 这种方法的主要意图是作为函数链式调用的一环, 为了对此对象执行操作并返回对象本身。

Example

ts
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

NameTypeDescription
objectV给定对象
interceptorFunc<any>给定函数

Returns

V


values

values<V>(object): TypeOfDictionary<V>[]

推荐原生:Object.values。 返回一个数组,数组由给定对象自有可枚举字符串键属性值组成。

Example

ts
values({ one: 1, two: 2, three: 3 })
=> [1, 2, 3]
values({ one: 1, two: 2, three: 3 })
=> [1, 2, 3]

Type parameters

NameType
Vextends Dictionary<any, V>

Parameters

NameTypeDescription
objectV给定对象

Returns

TypeOfDictionary<V>[]