dget-lit
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

dget-lit

Safely get a value from a nested object via a dot-notated path. In addition you have the ability to provide a fallback value to be returned if the full key path does not exists or the value is undefined.

Install

# Using npm
$ npm install dget-lit

# Using yarn
$ yarn add dget-lit

Example

import { dget } from 'dget-lit';

let obj = {
  a: {
    two: 2,
    b: {
      three: undefined,
      c: [3, 4, 5],
    },
  },
};

// use string dot notation for keys
dget(obj, 'a.two'); // 2

// or use an array key
dget(obj, ['a', 'two']); // 2

// use a default
dget(obj, 'a.b.three', 'my-default'); // 'my-default'

// to get a value from an array, use `.<index>` or `[<index>]`
dget(obj, 'a.b.c.2'); // 5
dget(obj, 'a.b.c[2]'); // 5

Development

(1) Install dependencies

# Using npm
$ npm install

# Using yarn
$ yarn

(2) Validate setup

$ ./Taskfile.sh validate

(3) Start development by running tests in watch-mode

$ ./Taskfile.sh test -w

Readme

Keywords

none

Package Sidebar

Install

npm i dget-lit

Weekly Downloads

59

Version

2.0.0

License

MIT

Unpacked Size

4.46 kB

Total Files

6

Last publish

Collaborators

  • joelvoss