Skip to content

Latest commit

 

History

History
41 lines (31 loc) · 1.08 KB

README.md

File metadata and controls

41 lines (31 loc) · 1.08 KB

Safety First

Safe nested lookups with simple tagged template syntax

npm version

This library aims to enable safe nested lookups in manner similar to lodash/get and other such libraries but uses Tagged template literals to provide an alternate syntax.

Usage

// use whatever alias you like
const g = require('safetyfirst');

const target = {
    foo: {
        bar: {
            baz: 'quux'
            corge: ['grault']
        }
    }
};

// mirrors basic lookup functionality
g`${target}.foo.bar.baz` === 'quux'
g`${target}[foo].bar.baz` === 'quux'
g`${target}['foo'].bar.baz` === 'quux'

// normally target.waldo.fred would have thrown an error
g`${target}.waldo.fred` === undefined

// interpolation 
const bar = 'bar';
g`${target}.foo.${bar}.baz` === 'quux'
g`${target}.foo[${bar}].baz` === 'quux'

// array lookups
g`${target}.foo.bar.corge[0]` === 'grault'