Skip to content

Latest commit

 

History

History
399 lines (277 loc) · 7.16 KB

object.md

File metadata and controls

399 lines (277 loc) · 7.16 KB

Module iiris/object

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'

Table of contents

Object

entries

<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

Example
O.entries({ a: 1, b: 2, c: 3 })
// => [['a', 1], ['b', 2], ['c', 3]]

See also: fromEntries, keys, values


fromEntries

<K extends string, V>(entries: Array<[K, V]>) => Record<K, V>

Create an object from an array of [key, value] pairs.

Example
O.fromEntries([
  ['a', 1],
  ['b', 2],
  ['c', 3],
])
// => { a: 1, b: 2, c: 3 }

See also: entries


fromMap

<K extends string, V>(map: Map<K, V>) => Record<K, V>

Convert a map with string keys to an object.

Example
O.fromMap(
  new Map([
    ['a', 1],
    ['b', 2],
    ['c', 3],
  ])
)
// => { a: 1, b: 2, c: 3 }

get

<K extends string>(key: K) => <T extends HasKey<K>>(object: T) => T[K]

Retrieves the property key from object or undefined.

Example
O.get('a', { a: 1, b: 2, c: 3 })
// => 1

O.get('a', {})
// => undefined

See also: getOr


getOr

<V>(defaultValue: V) => <K extends string>(key: K) => <T extends HasKey<K, V>>(object: T) => Defined<T[K]> | V

Like get, but if the resolved value is undefined, defaultValue is returned instead.

Example
O.getOr(999, 'a', { a: 1, b: 2, c: 3 })
// => 1

O.getOr(999, 'a', {})
// => 999

O.getOr(999, 'a', { a: undefined })
// => 999

See also: get


has

<K extends string>(key: K) => <T extends HasKey<K>>(object: T) => boolean

Check if key is an own property of object.

Example
O.has('a', { a: 1 })
// => true

O.has('toString', { a: 1 })
// => false

keys

<T extends object>(object: T) => Array<keyof T & string>

Return an array of the own enumerable property keys of object.

Example
O.keys({ a: 1, b: 2, c: 3 })
// => ['a', 'b', 'c']

See also: entries, values


mapKeys

<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.

Example
O.mapKeys((k) => k.toUpperCase(), { a: 1, b: 2, c: 3 })
// => { A: 1, B: 2, C: 3 }

mapValues

<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.

Example
O.mapValues((n) => n + 1, { a: 1, b: 2, c: 3 })
// => { a: 2, b: 3, c: 4 }

merge

<T extends object>(first: T) => <U extends object>(second: U) => T & U

Copy the own enumerable properties of two objects, preferring the values from second in case of duplicate keys.

Example
O.merge({ a: 1, b: 1 }, { b: 2, c: 2 })
// => { a: 1, b: 2, c: 2 }

modify

<K extends string>(key: K) => <V>(fn: (value: V) => V) => <T extends HasKey<K, V>>(object: T) => T

Return a copy of object where the property key has replaced by applying fn to its current value.

  • If key is not an own property of object, the object is returned unchanged.
  • If fn returns undefined, the property is removed.
Example
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 }

See also: set, remove


omit

<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.

Example
O.omit(['a', 'b'], { a: 1, b: 2, c: 3 })
// => { c: 3 }

See also: pick


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.

Example
O.pick(['a', 'b'], { a: 1, b: 2, c: 3 })
// => { a: 1, b: 2 }

See also: omit


remove

<K extends string>(key: K) => <T extends HasKey<K>>(object: T) => Omit<T, K>

Return a copy of object without the property key.

  • If key is not an own property of object, the object is returned unchanged.
Example
O.remove('a', { a: 1, b: 2, c: 3 })
// => { b: 2, c: 3 }

set

<K extends string>(key: K) => <V>(value: V) => <T extends HasKey<K, V>>(object: T) => T

Return a copy of object with property key set to value.

  • If value is undefined, the property is removed.
Example
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 }

See also: modify, remove


values

<T extends object>(object: T) => Array<T[keyof T & string]>

Return an array of the own enumerable property values of object

Example
> O.values({ a: 1, b: 2, c: 3 })
[1, 2, 3]

See also: keys, entries