1.0.1 • Published 6 years ago

safe-property v1.0.1

Weekly downloads
2
License
ISC
Repository
github
Last release
6 years ago

safe-property

Ultra-light way to safely access your nested objects properties.

let property = require('safe-property');

let obj = {
  a: {
    b: {
      d: [
        { x: true },
        {
          y: {
            x: false
          }
        }
      ]
    },
    c: {
      value: 123
    }
  }
}

console.log(property(obj, ['a', 'c', 'value']));
// 123
console.log(property(obj, 'a.c.value'));
// 123
console.log(property(obj, 'a.b.d.1.y'));
// { x: false }
console.log(property(obj, 'a.b.z'));
// undefined
console.log(property(obj, 'a.b.z', 'your princess is in another castle'));
// your princess is in another castle