metecik / mb

Exception-free nested nullable attribute accessor

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

mb

goat

Exception-free nested nullable attribute accessor. An alternative to facebookincubator/idx in 52 bytes.


Install

Just copy/paste this function into your project:

var mb=(...p)=>p.reduce.bind(p,(a,c)=>Object(a)[c])

Alternatively, you can download mb.js.

Use

var getHello = mb('a', 'b', 0, 'hello');
var getHelloLength = mb('a', 'b', 0, 'hello', 'length');

var obj1 = {
  a: {
    b: [{ hello: 'world' }]
  }
};

var obj2 = {
  c: {
    d: 'e'
  }
};

getHello(obj1); // world
getHelloLength(obj2); // 5

getHello(obj2); // undefined
getHelloLength(obj2); // undefined

Contribution and Code-Golfing

  1. Clone and shorten current code.
  2. Please open test.html in your browser and open console to see if all the tests pass.

Contributors

About

Exception-free nested nullable attribute accessor

License:The Unlicense


Languages

Language:JavaScript 91.0%Language:HTML 9.0%