Object (Dict)
Utility functions for Object
.
deleteKey
Returns a new object with the provided key deleted.
function deleteKey<T, K extends keyof T>(dict: T, key: K): Omit<T, K>
function deleteKey<T, K extends keyof T>(key: K): (dict: T) => Omit<T, K>
D.deleteKey({ name: 'Joe', location: 'Warsaw' }, 'location') // → { name: 'Joe' }
pipe({ name: 'Joe', location: 'Warsaw' }, D.deleteKey('location')) // → { name: 'Joe' }
deleteKeys
Returns a new object with the provided keys deleted.
function deleteKeys<T, K extends keyof T>(dict: T, keys: ReadonlyArray<K>): Omit<T, K>
function deleteKeys<T, K extends keyof T>(keys: ReadonlyArray<K>): (dict: T) => Omit<T, K>
D.deleteKeys({ name: 'Joe', location: 'Warsaw' }, []) // → {
name: 'Joe',
location: 'Warsaw',
}
D.deleteKeys({ name: 'Joe', location: 'Warsaw' }, ['name']) // → { location: 'Warsaw' }
D.deleteKeys({ name: 'Joe', location: 'Warsaw' }, ['name', 'location']) // → {}
pipe({ name: 'Joe', location: 'Warsaw' }, D.deleteKeys([])) // → {
name: 'Joe',
location: 'Warsaw',
}
pipe({ name: 'Joe', location: 'Warsaw' }, D.deleteKeys(['name'])) // → { location: 'Warsaw' }
pipe(
{ name: 'Joe', location: 'Warsaw' },
D.deleteKeys(['name', 'location']),
) // → {}
filter
Removes each property that doesn't satisfy the given predicate function.
function filter<T extends Record<string, any>>(dict: T, predicateFn: (value: T[keyof T]) => boolean): Partial<T>
function filter<T extends Record<string, any>>(predicateFn: (value: T[keyof T]) => boolean): (dict: T) => Partial<T>
D.filter({ name: 'Joe', age: 20 }, value => value === 20) // → { age: 20 }
pipe(
{
name: 'Joe',
age: 20,
},
D.filter(value => value === 'Joe'),
) // → { name: 'Joe' }
filterWithKey
Removes each property that doesn't satisfy the given predicate function (which takes two arguments: a property value and key).
function filterWithKey<T extends Record<string, any>>(dict: T, predicateFn: (key: keyof T, value: T[keyof T]) => boolean): Partial<T>
function filterWithKey<T extends Record<string, any>>(predicateFn: (key: keyof T, value: T[keyof T]) => boolean): (dict: T) => Partial<T>
D.filterWithKey(
{ name: 'Joe', age: 20 },
(key, value) => value === 30 && key === 'age',
) // → {}
pipe(
{
name: 'Joe',
age: 20,
},
D.filterWithKey((key, _) => key === 'name'),
) // → { name: 'Joe' }
fromPairs
Creates a new object from an array of tuples ([key, value]
).
function fromPairs<T, K extends string | number>(xs: Array<readonly [K, T]>): Record<string, T>
D.fromPairs([
['name', 'Joe'],
['location', 'Warsaw'],
] as const) // → { name: 'Joe', location: 'Warsaw' }
pipe(
[
['name', 'Joe'],
['location', 'Warsaw'],
] as const,
D.fromPairs,
) // → { name: 'Joe', location: 'Warsaw' }
get
Returns Some(value)
if the given key exists, otherwise returns None
.
function get<T, K extends keyof T>(dict: T, key: K): Option<ExtractValue<T[K]>>
function get<T, K extends keyof T>(key: K): (dict: T) => Option<ExtractValue<T[K]>>
D.get({ name: 'Joe', location: 'Warsaw' }, 'location') // → Some('Warsaw')
pipe({ name: 'Joe', location: 'Warsaw' }, D.get('location')) // → Some('Warsaw')
pipe([{ name: 'Joe' }, { location: 'Warsaw' }], A.map(D.get('name'))) // → [Some('Joe'), None]
getUnsafe
Returns the value if the given key exists, otherwise returns undefined
.
function getUnsafe<T, K extends keyof T>(dict: T, key: K): T[K]
function getUnsafe<T, K extends keyof T>(key: K): (dict: T) => T[K]
D.getUnsafe({ name: 'Joe', location: 'Warsaw' }, 'location') // → 'Warsaw'
pipe({ name: 'Joe', location: 'Warsaw' }, D.getUnsafe('location')) // → 'Warsaw'
pipe(
[{ name: 'Joe' }, { location: 'Warsaw' }],
A.map(D.getUnsafe('name')),
) // → ['Joe', undefined]
isEmpty
Determines whether the given object is empty.
function isEmpty<T extends Record<string, any>>(dict: T): boolean
D.isEmpty({ name: 'Joe', age: 20 }) // → false
D.isEmpty({}) // → true
pipe(
{
name: 'Joe',
age: 20,
},
D.isEmpty,
) // → false
pipe({}, D.isEmpty) // → true
isNotEmpty
Determines whether the given object is not empty.
function isNotEmpty<T extends Record<string, any>>(dict: T): boolean
D.isNotEmpty({ name: 'Joe', age: 20 }) // → true
D.isNotEmpty({}) // → false
pipe(
{
name: 'Joe',
age: 20,
},
D.isNotEmpty,
) // → true
pipe({}, D.isNotEmpty) // → false
keys
Returns a new array that contains all keys of the provided object.
function keys<T extends Record<string, unknown>>(dict: T): Array<keyof T>
D.keys({
name: 'Joe',
age: 20,
}) // → ['name', 'age']
pipe(
{
name: 'Joe',
age: 20,
},
D.keys,
) // → ['name', 'age']
makeEmpty
Creates an empty object. Alternative for const obj = {} as SomeObjectType
.
function makeEmpty<T>(): T
map
Transforms each value in the object to a new value using the provided function.
function map<T extends Record<string, any>, R>(dict: T, mapFn: (value: T[keyof T]) => R): Record<keyof T, R>
function map<T extends Record<string, any>, R>(mapFn: (value: T[keyof T]) => R): (dict: T) => Record<keyof T, R>
D.map(
{
name: 'Joe',
location: 'Warsaw',
},
value => value.length,
) // → { name: 3, location: 6 }
pipe(
{
name: 'Joe',
location: 'Warsaw',
},
D.map(value => value.length),
) // → { name: 3, location: 6 }
mapWithKey
Transforms each value in the object to a new value using the provided function (which takes two arguments: a property value and key).
function mapWithKey<T extends Record<string, any>, R>(dict: T, mapFn: (key: keyof T, value: T[keyof T]) => R): Record<keyof T, R>
function mapWithKey<T extends Record<string, any>, R>(mapFn: (key: keyof T, value: T[keyof T]) => R): (dict: T) => Record<keyof T, R>
D.mapWithKey(
{
name: 'Joe',
location: 'Warsaw',
},
(key, value) => `${key}-${value.toLowerCase()}`,
) // → { name: 'name-joe', location: 'location-warsaw' }
pipe(
{
name: 'Joe',
location: 'Warsaw',
},
D.mapWithKey((key, value) => `${key}-${value.toLowerCase()}`),
) // → { name: 'name-joe', location: 'location-warsaw' }
merge
Merges two provided objects.
function merge<A, B>(fst: A, snd: B): A & B
function merge<A, B>(snd: B): (fst: A) => A & B
D.merge({ x: 1, y: 2 }, { y: 3, z: 4 }) // → { x: 1, y: 3, z: 4 }
pipe({ x: 1, y: 2 }, D.merge({ y: 3, z: 4 })) // → { x: 1, y: 3, z: 4 }
prop
@deprecated Use either D.get
or D.getUnsafe
instead.
function prop<T, K extends keyof T>(dict: T, key: K): T[K]
function prop<T, K extends keyof T>(key: K): (dict: T) => T[K]
reject
Removes each property that satisfies the given predicate function.
function reject<T extends Record<string, any>>(dict: T, predicateFn: (value: T[keyof T]) => boolean): Partial<T>
function reject<T extends Record<string, any>>(predicateFn: (value: T[keyof T]) => boolean): (dict: T) => Partial<T>
D.reject({ name: 'Joe', age: 20 }, value => value === 20) // → { name: 'Joe' }
pipe(
{
name: 'Joe',
age: 20,
},
D.reject(value => value === 'Joe'),
) // → { age: 20 }
rejectWithKey
Removes each property that satisfies the given predicate function (which takes two arguments: a property value and key).
function rejectWithKey<T extends Record<string, any>>(dict: T, predicateFn: (key: keyof T, value: T[keyof T]) => boolean): Partial<T>
function rejectWithKey<T extends Record<string, any>>(predicateFn: (key: keyof T, value: T[keyof T]) => boolean): (dict: T) => Partial<T>
D.rejectWithKey(
{ name: 'Joe', age: 20 },
(key, value) => value === 20 && key === 'age',
) // → { name: 'Joe' }
pipe(
{
name: 'Joe',
age: 20,
},
D.rejectWithKey((key, _) => key === 'name'),
) // → { age: 20 }
selectKeys
Returns a new object with the provided keys selected.
function selectKeys<T, K extends keyof T>(dict: T, keys: ReadonlyArray<K>): Pick<T, K>
function selectKeys<T, K extends keyof T>(keys: ReadonlyArray<K>): (dict: T) => Pick<T, K>
D.selectKeys({ name: 'Joe', location: 'Warsaw', age: 42 }, [
'name',
'age',
]) // → { name: 'Joe', age: 42 }
// ⬇️ const obj: Record<string, string> = { name: 'Joe', location: 'Warsaw' }
D.selectKeys(obj, ['name', 'age']) // → { name: 'Joe' }
pipe(
{ name: 'Joe', location: 'Warsaw', age: 42 },
D.selectKeys(['name', 'age']),
) // → { name: 'Joe', age: 42 }
// ⬇️ const obj: Record<string, string> = { name: 'Joe', location: 'Warsaw' }
pipe(obj, D.selectKeys(['name', 'age'])) // → { name: 'Joe' }
set
Adds a property. Equivalent to merging with {key: value}
function set<T, K extends string | number, V>(dict: T, key: K, value: V): T & Record<K, V>
function set<T, K extends string | number, V>(key: K, value: V): (dict: T) => T & Record<K, V>
D.set({ name: 'Joe', location: 'Warsaw' }, 'location', 'Gdańsk') // → { name: 'Joe', location: 'Gdańsk' }
D.set({ 0: false, 1: true }, 1, 'abc') // → { 0: false, 1: 'abc' }
pipe({ name: 'Joe', location: 'Warsaw' }, D.set('location', 'Gdańsk')) // → { name: 'Joe', location: 'Gdańsk' }
pipe({ 0: false, 1: true }, D.set(1, 'abc')) // → { 0: false, 1: 'abc' }
toPairs
Converts an object into an array of [key, value]
tuples.
function toPairs<T, K extends string | number>(dict: Record<K, T>): Array<readonly [string, T]>
D.toPairs({
name: 'Joe',
age: 20,
}) // → [['name', 'Joe'], ['age', 20]]
pipe(
{
name: 'Joe',
age: 20,
},
D.toPairs,
) // → [['name', 'Joe'], ['age', 20]]
update
Updates a property by applying the provided function to the corresponding optional value.
function update<T, K extends keyof T, R>(dict: T, key: K, fn: (value: Option<T[K]>) => R): T & Record<K, R>
function update<T, K extends keyof T, R>(key: K, fn: (value: Option<T[K]>) => R): (dict: T) => T & Record<K, R>
D.update({ name: 'Joe', location: 'Warsaw' }, 'name', option =>
O.mapWithDefault(option, 'unknown', S.toUpperCase),
) // → { name: 'JOE', location: 'Warsaw' }
// ⬇️ const obj: Record<string, string> = { name: 'Joe', location: 'Warsaw' }
D.update(obj, 'key', option =>
O.mapWithDefault(option, 'unknown', S.toUpperCase),
) // → { name: 'Joe', key: 'unknown', location: 'Warsaw' }
pipe(
{ name: 'Joe', location: 'Warsaw' },
D.update('name', option =>
O.mapWithDefault(option, 'unknown', S.toUpperCase),
),
) // → { name: 'JOE', location: 'Warsaw' }
// ⬇️ const obj: Record<string, string> = { name: 'Joe', location: 'Warsaw' }
pipe(
obj,
D.update('key', option =>
O.mapWithDefault(option, 'unknown', S.toUpperCase),
),
) // → { name: 'Joe', key: 'unknown', location: 'Warsaw' }
updateUnsafe
Updates a property by applying the provided function to the corresponding value.
function updateUnsafe<T, K extends keyof T, R>(dict: T, key: K, fn: (value: T[K]) => R): T & Record<K, R>
function updateUnsafe<T, K extends keyof T, R>(key: K, fn: (value: T[K]) => R): (dict: T) => T & Record<K, R>
D.updateUnsafe(
{ name: 'Joe', location: 'Warsaw' },
'name',
S.toUpperCase,
) // → { name: 'JOE', location: 'Warsaw' }
D.updateUnsafe({ 0: false, 1: true }, 1, B.inverse) // → { 0: false, 1: false }
pipe(
{ name: 'Joe', location: 'Warsaw' },
D.updateUnsafe('name', S.toUpperCase),
) // → { name: 'JOE', location: 'Warsaw' }
pipe({ 0: false, 1: true }, D.updateUnsafe(1, B.inverse)) // → { 0: false, 1: false }
values
Returns a new array that contains all values of the provided object.
function values<T extends string | number, R>(dict: Record<T, R>): Array<R>
D.values({
name: 'Joe',
age: 20,
}) // → ['Joe', 20]
pipe(
{
name: 'Joe',
age: 20,
},
D.values,
) // → ['Joe', 20]