Skip to content

josscode/safetyfirst

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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'

About

Safe nested lookups with simple tagged template syntax

Resources

Stars

Watchers

Forks

Packages

No packages published