@chronocide/dot-obj

Utility functions for nested JavaScript Objects

Usage no npm install needed!

<script type="module">
  import chronocideDotObj from 'https://cdn.skypack.dev/@chronocide/dot-obj';
</script>

README

@chronocide/dot-obj

Strong-typed utility functions for nested JavaScript Objects

Install

$ npm i @chronocide/dot-obj

Note: This package requires Node >=10.12.0

Features

  • get() - Get property at any depth
  • set() - Set property at any depth
  • some() - Test every property and return true on first property that returns true, otherwise false
  • every() - Test every property and return false on first property that returns false, otherwise true

Usage

get()

get<T>(object: Record<string, any>, path: string): T

Get property at any depth

Example

import dot from 'dot-obj';

// Get
dot.get({ foo: { bar: true } }, 'foo.bar') // => true
dot.get({ foo: { bar: true } }, 'foo.bar.baz') // => undefined
dot.get({ foo: { bar: true } }, 'bar') // => undefined
dot.get({ foo: [{ bar: true }, { baz: false }] }, 'foo.0') // => { bar: true }

// TS
dot.get<boolean>({ foo: { bar: true } }, 'foo.bar') // => true (boolean)

set()

set<T extends Record<string, any>>(object: Record<string, any>, path: string, value: any): T

Set property at any depth

Example

import dot from 'dot-obj';

// Set
dot.set({ foo: { bar: true } }, 'foo', { baz: false }) // => { foo: { baz: false } }
dot.set({ foo: { bar: true } }, 'foo.bar', { baz: false }) // => { foo: { bar: { baz: false } } }
dot.set({ foo: { bar: true } }, 'baz', false) // => { foo: { bar: true }, baz: false }

// TS
dot.set<{ foo: { baz: false } }>({ foo: { bar: true } }, 'foo', { baz: false }) // => { foo: { baz: false } } ({ foo: { baz: false } })

some()

some(object: Record<string, any>, match: (entries: [string, any]) => boolean): boolean

Test every property and return true on first property that returns true, otherwise false

import dot from 'dot-obj';

dot.some({ foo: { bar: true } }, ([key]) => key === 'foo') // true
dot.some({ foo: { bar: true } }, ([key]) => key === 'baz') // false
dot.some({ foo: { bar: true } }, ([, value]) => value === 'foo') // false
dot.some({ foo: { bar: true } }, ([, value]) => value === true) // true

every()

every(object: Record<string, any>, match: (entries: [string, any]) => boolean): boolean

Test every property and return false on first property that returns false, otherwise true

import dot from 'dot-obj';

dot.every(
  { foo: { bar: true } },
  ([key]) => typeof key === 'string'
) // true
dot.every(
  { foo: { bar: true } },
  ([key]) => key === 'bar'
) // false
dot.every(
  { foo: { bar: true }, baz: true },
  ([key, value]) => key[0] === 'b' && value === true
) // true
dot.every(
  { foo: { bar: true } },
  ([, value]) => typeof value === 'boolean'
) // false

Donating

ko-fi