preludejs/Obj API
Complete API reference for preludejs/Obj
preludejs/Obj API Reference
Core
clone ƒ
Returns a deep clone of the value via JSON round-tripping.
function clone<T>(value: T): TParameters
| Parameter | Type | Description |
|---|---|---|
value | T |
Example
clone({ a: 1, b: 2 }) //=> { a: 1, b: 2 }get ƒ
Reads a member from an object by key; key-first and curried.
function get<K extends PropertyKey>(key: K): <T extends Record<K, unknown>>(obj: T) => T[K]
function get<T, K extends keyof T>(key: K, obj: T): T[K]Parameters
| Parameter | Type | Description |
|---|---|---|
key | K |
Example
get('foo', { foo: 'bar' }) //=> 'bar'keys ƒ
Returns the own enumerable keys of an object.
function keys<T extends object>(obj: T): (keyof T)[]Parameters
| Parameter | Type | Description |
|---|---|---|
obj | T |
Example
keys({ a: 2, b: 3 }) //=> ['a', 'b']map ƒ
Maps each key/value pair of an object to an array; curried.
function map<V, B>(fn: (key: string, value: V, index: number) => B): (obj: Record<string, V>) => B[]
function map<V, B>(fn: (key: string, value: V, index: number) => B, obj: Record<string, V>): B[]Parameters
| Parameter | Type | Description |
|---|---|---|
fn | (key: string, value: V, index: number) => B |
Example
map((k: string, v: number) => `${k}=${v}`, { a: 1, b: 2 }) //=> ['a=1', 'b=2']merge ƒ
Merges any number of source objects into the first; later sources win.
function merge<A extends object>(target: A): A
function merge<A extends object, B extends object>(target: A, b: B): A & B
function merge<A extends object, B extends object, C extends object>(target: A, b: B, c: C): A & B & C
function merge<A extends object>(target: A, ...sources: object[]): A & Record<string, unknown>Parameters
| Parameter | Type | Description |
|---|---|---|
target | A |
Example
merge({ a: 1, b: 2 }, { b: 3 }) //=> { a: 1, b: 3 }objToPairs ƒ
Turns an object into a list of [key, value] pairs.
function objToPairs<T extends Record<PropertyKey, unknown>>(obj: T): (readonly [keyof T, T[keyof T]])[]Parameters
| Parameter | Type | Description |
|---|---|---|
obj | T |
Example
objToPairs({ a: 'b', c: 'd' }) //=> [['a', 'b'], ['c', 'd']]pairsToObj ƒ
Turns a list of [key, value] pairs into an object.
function pairsToObj<K extends PropertyKey, V>(pairs: readonly (readonly [K, V])[]): Record<K, V>Parameters
| Parameter | Type | Description |
|---|---|---|
pairs | readonly (readonly [K, V])[] |
Example
pairsToObj([['a', 1], ['b', 2]]) //=> { a: 1, b: 2 }reduce ƒ
Reduces an object's key/value pairs to a single value; curried.
The callback receives (acc, key, value, index, obj).
function reduce<A, V>(fn: (acc: A, key: string, value: V, index: number, obj: Record<string, V>) => A): (initial: A) => (obj: Record<string, V>) => A
function reduce<A, V>(fn: (acc: A, key: string, value: V, index: number, obj: Record<string, V>) => A, initial: A): (obj: Record<string, V>) => A
function reduce<A, V>(fn: (acc: A, key: string, value: V, index: number, obj: Record<string, V>) => A, initial: A, obj: Record<string, V>): AParameters
| Parameter | Type | Description |
|---|---|---|
fn | (acc: A, key: string, value: V, index: number, obj: Record<string, V>) => A |
Example
reduce((acc: number, _k: string, v: number) => acc + v, 0, { a: 1, b: 2 }) //=> 3values ƒ
Returns the own enumerable values of an object.
function values<T extends object>(obj: T): T[keyof T][]Parameters
| Parameter | Type | Description |
|---|---|---|
obj | T |
Example
values({ a: 2, b: 3 }) //=> [2, 3]