safetyfirst

0.0.4 • Public • Published

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'

Package Sidebar

Install

npm i safetyfirst

Weekly Downloads

9

Version

0.0.4

License

ISC

Last publish

Collaborators

  • jordrake