The iiris/set module includes functions for working with Objects. It is
designed to be imported with a wildcard, e.g.
import * as O from 'iiris/object'<T extends object>(object: T) => Array<[keyof T & string, T[keyof T & string]]>Return an array of the own enumerable property key-value pairs of object
O.entries({ a: 1, b: 2, c: 3 })
// => [['a', 1], ['b', 2], ['c', 3]]See also: fromEntries, keys, values
<K extends string, V>(entries: Array<[K, V]>) => Record<K, V>Create an object from an array of [key, value] pairs.
O.fromEntries([
['a', 1],
['b', 2],
['c', 3],
])
// => { a: 1, b: 2, c: 3 }See also: entries
<K extends string, V>(map: Map<K, V>) => Record<K, V>Convert a map with string keys to an object.
O.fromMap(
new Map([
['a', 1],
['b', 2],
['c', 3],
])
)
// => { a: 1, b: 2, c: 3 }<K extends string>(key: K) => <T extends HasKey<K>>(object: T) => T[K]Retrieves the property key from object or undefined.
O.get('a', { a: 1, b: 2, c: 3 })
// => 1
O.get('a', {})
// => undefinedSee also: getOr
<V>(defaultValue: V) => <K extends string>(key: K) => <T extends HasKey<K, V>>(object: T) => Defined<T[K]> | VLike get, but if the resolved value is undefined, defaultValue is
returned instead.
O.getOr(999, 'a', { a: 1, b: 2, c: 3 })
// => 1
O.getOr(999, 'a', {})
// => 999
O.getOr(999, 'a', { a: undefined })
// => 999See also: get
<K extends string>(key: K) => <T extends HasKey<K>>(object: T) => booleanCheck if key is an own property of object.
O.has('a', { a: 1 })
// => true
O.has('toString', { a: 1 })
// => false<T extends object>(object: T) => Array<keyof T & string>Return an array of the own enumerable property keys of object.
O.keys({ a: 1, b: 2, c: 3 })
// => ['a', 'b', 'c']<K1 extends string, K2 extends string>(fn: (value: K1) => K2) => <V>(object: Record<K1, V>) => Record<K2, V>Return an object containing the results of applying fn to each key of the
original object.
If multiple keys map to the same new key, the latest value is selected.
O.mapKeys((k) => k.toUpperCase(), { a: 1, b: 2, c: 3 })
// => { A: 1, B: 2, C: 3 }<V1, V2>(fn: (value: V1) => V2) => <K extends string>(object: Record<K, V1>) => Record<K, V2>Return an object containing the results of applying fn to each value of the
original object.
O.mapValues((n) => n + 1, { a: 1, b: 2, c: 3 })
// => { a: 2, b: 3, c: 4 }<T extends object>(first: T) => <U extends object>(second: U) => T & UCopy the own enumerable properties of two objects, preferring the values from
second in case of duplicate keys.
O.merge({ a: 1, b: 1 }, { b: 2, c: 2 })
// => { a: 1, b: 2, c: 2 }<K extends string>(key: K) => <V>(fn: (value: V) => V) => <T extends HasKey<K, V>>(object: T) => TReturn a copy of object where the property key has replaced by applying
fn to its current value.
- If
keyis not an own property ofobject, theobjectis returned unchanged. - If
fnreturnsundefined, the property is removed.
O.modifyProp('a', (n) => n + 1, { a: 1, b: 2, c: 3 })
// => { a: 2, b: 2, c: 3 }
O.modifyProp('a', () => undefined, { a: 1, b: 2, c: 3 })
// => { b: 2, c: 3 }
O.modifyProp('d', () => 4, { a: 1, b: 2, c: 3 })
// => { a: 1, b: 2, c: 3, d: 4 }<K extends string>(keys: K[]) => <T extends HasKey<K>>(object: T) => Omit<T, Extract<keyof T, K>>Return a copy of object without the specified keys.
O.omit(['a', 'b'], { a: 1, b: 2, c: 3 })
// => { c: 3 }See also: pick
<K extends string>(keys: K[]) => <T extends HasKey<K>>(object: T) => Pick<T, Extract<keyof T, K>>Return a copy of object with only the specified keys.
O.pick(['a', 'b'], { a: 1, b: 2, c: 3 })
// => { a: 1, b: 2 }See also: omit
<K extends string>(key: K) => <T extends HasKey<K>>(object: T) => Omit<T, K>Return a copy of object without the property key.
- If
keyis not an own property ofobject, theobjectis returned unchanged.
O.remove('a', { a: 1, b: 2, c: 3 })
// => { b: 2, c: 3 }<K extends string>(key: K) => <V>(value: V) => <T extends HasKey<K, V>>(object: T) => TReturn a copy of object with property key set to value.
- If
valueisundefined, the property is removed.
O.setProp('a', 999, { a: 1, b: 2, c: 3 })
// => { a: 999, b: 2, c: 3 }
O.setProp('a', undefined, { a: 1, b: 2, c: 3 })
// => { b: 2, c: 3 }<T extends object>(object: T) => Array<T[keyof T & string]>Return an array of the own enumerable property values of object
> O.values({ a: 1, b: 2, c: 3 })
[1, 2, 3]