Exception-free nested nullable attribute accessor
Switch branches/tags
Nothing to show
Clone or download
Latest commit 573dd5d Oct 15, 2018
Permalink
Failed to load latest commit information.
LICENSE Initial commit Oct 10, 2018
README.md Convert to .map Oct 15, 2018
mb.js Convert to .map Oct 15, 2018
test.html visual tests Oct 10, 2018
test.js Add test for deep key access on bad paths Oct 14, 2018

README.md

mb

goat

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


Install

Just copy/paste this function into your project:

var mb=(...p)=>o=>p.map(c=>o=(o||{})[c])&&o

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(obj1); // 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