├── mb.js ├── test.html ├── LICENSE ├── README.md └── test.js /mb.js: -------------------------------------------------------------------------------- 1 | var mb=p=>o=>p.map(c=>o=(o||{})[c])&&o 2 | -------------------------------------------------------------------------------- /test.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | mb tests 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 |

mb

3 | goat 4 | 5 | Exception-free nested nullable attribute accessor. 6 | An alternative to [facebookincubator/idx](https://github.com/facebookincubator/idx) in 38 bytes. 7 | 8 |
9 | 10 |
11 | 12 | ## Install 13 | 14 | Just copy/paste this function into your project: 15 | ``` javascript 16 | var mb=p=>o=>p.map(c=>o=(o||{})[c])&&o 17 | ``` 18 | 19 | Alternatively, you can download [mb.js](https://raw.githubusercontent.com/burakcan/mb/master/mb.js). 20 | 21 | ## Use 22 | 23 | ```javascript 24 | var getHello = mb(["a", "b", 0, "hello"]); 25 | var getHelloLength = mb(["a", "b", 0, "hello", "length"]); 26 | 27 | var obj1 = { 28 | a: { 29 | b: [{ hello: "world" }] 30 | } 31 | }; 32 | 33 | var obj2 = { 34 | c: { 35 | d: "e" 36 | } 37 | }; 38 | 39 | getHello(obj1); // world 40 | getHelloLength(obj1); // 5 41 | 42 | getHello(obj2); // undefined 43 | getHelloLength(obj2); // undefined 44 | ``` 45 | 46 | ## Contribution and Code-Golfing 47 | 48 | 1. Clone and shorten current code. 49 | 2. Please open `test.html` in your browser and open console to see if all the tests pass. 50 | 51 | ## Contributors 52 | 53 | - [Fatih Kadir Akin](https://github.com/f) 54 | - [Burak Can](https://github.com/burakcan) 55 | - [Eser Ozvataf](https://github.com/eserozvataf) 56 | - [Cahit Gürgüc](https://github.com/aborjinik) 57 | - [Alper Tekinalp](https://github.com/alpert) 58 | - [Max Gerber](https://github.com/maxwellgerber) 59 | - [Cem Ekici](https://github.com/cekici) 60 | - [Atanas Minev](https://github.com/atmin) 61 | - [Peter Levi](https://github.com/peterlevi) 62 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | const data = { 2 | a: 1, 3 | b: null, 4 | c: undefined, 5 | d: { 6 | _: 'object', 7 | e: {}, 8 | f: null, 9 | g: function () {}, 10 | get h () { 11 | return 3 12 | }, 13 | j: 1, 14 | k: "string" 15 | }, 16 | l: { 17 | m: [ 18 | {n: 1}, 19 | {n: 2}, 20 | {n: 3} 21 | ], 22 | o: { 23 | p: { 24 | q: true, 25 | r: false, 26 | s: { 27 | t: 1, 28 | "spaced keys": "hey" 29 | }, 30 | u: [ 31 | [0, 0, 1], 32 | [0, 2, 0], 33 | [1, 0, 0], 34 | ] 35 | } 36 | } 37 | } 38 | } 39 | const write = w => document.body.innerHTML += `${w}
` 40 | const results = { total: 0, passed: 0 }; 41 | const test = (condition, data) => { 42 | results.total++; 43 | 44 | if (condition) { 45 | results.passed++; 46 | write(` ✅ pass: ${data}`) 47 | return; 48 | } 49 | write(` 🔴 fail: ${data}`) 50 | console.assert(condition, data); 51 | } 52 | 53 | console.group('mb tests'); 54 | test(mb(['a'])(data) === 1, 'a'); 55 | test(mb(['a', 'bad key', 'bad key', 'bad key'])(data) === undefined, 'bad key'); 56 | test(mb(['b'])(data) === null, 'b'); 57 | test(mb(['c'])(data) === undefined, 'c'); 58 | test(mb(['d'])(data)._ === 'object', 'd'); 59 | test(JSON.stringify(mb(['d', 'e'])(data)) === '{}', 'e'); 60 | test(mb(['d', 'f'])(data) === null, 'f'); 61 | test(typeof mb(['d', 'g'])(data) === 'function', 'g'); 62 | test(mb(['d', 'h'])(data) === 3, 'h'); 63 | test(mb(['d', 'j'])(data) === 1, 'j'); 64 | test(mb(['d', 'k'])(data) === 'string', 'k'); 65 | test(mb(['d', 'k', 'length'])(data) === 6, 'length'); 66 | test(mb(['l', 'm'])(data).length === 3, 'm'); 67 | test(mb(['l', 'm', 0, 'n'])(data) === 1, 'n'); 68 | test(mb(['l', 'm', 1, 'n'])(data) === 2, 'n'); 69 | test(mb(['l', 'm', 2, 'n'])(data) === 3, 'n'); 70 | test(mb(['l', 'o', 'p', 'q'])(data) === true, 'q'); 71 | test(mb(['l', 'o', 'p', 'r'])(data) === false, 'r'); 72 | test(mb(['l', 'o', 'p', 's', 't'])(data) === 1, 't'); 73 | test(mb(['l', 'o', 'p', 'u', 1, 1])(data) === 2, 'u'); 74 | test(mb(['l', 'o', 'p', 's', 'spaced keys'])(data) === 'hey', 'spaced keys'); 75 | 76 | test(mb([0, 1])([[1,2]]) === 2, 'array'); 77 | test(mb([0, 1])([0]) === undefined, 'missing intermediate'); 78 | 79 | console.info(`Passed tests ${results.passed} out of ${results.total}.`); 80 | 81 | if (results.passed == results.total) { 82 | console.info('Everything seems fine.'); 83 | } 84 | 85 | console.groupEnd(); 86 | --------------------------------------------------------------------------------