0.0.4 • Published 10 years ago

safetyfirst v0.0.4

Weekly downloads
3
License
ISC
Repository
github
Last release
10 years ago

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'