├── .npmignore ├── .gitignore ├── .babelrc ├── .travis.yml ├── src └── index.js ├── CHANGELOG.md ├── package.json ├── LICENSE.txt ├── README.md └── test └── dig_test.js /.npmignore: -------------------------------------------------------------------------------- 1 | src/ 2 | test/ 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | dist 3 | -------------------------------------------------------------------------------- /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | presets: [ 'es2015' ] 3 | } 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '5' 4 | - '6' 5 | - '7' 6 | sudo: false 7 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | module.exports = (target, ...keys) => { 2 | let digged = target; 3 | for (const key of keys) { 4 | if (typeof digged === 'undefined' || digged === null) { 5 | return undefined; 6 | } 7 | if (typeof key === 'function') { 8 | digged = key(digged); 9 | } else { 10 | digged = digged[key]; 11 | } 12 | }; 13 | return digged; 14 | }; 15 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 0.1.3 (2018-05-18) 2 | 3 | ### Fix 4 | 5 | - Add support for null intermediate values ([#11](https://github.com/joe-re/object-dig/pull/11)) [@EricRoos](https://github.com/EricRoos) 6 | 7 | ## 0.1.2 (2018-05-17) 8 | 9 | ### Fix 10 | 11 | - Return undefined if target is null ([#9](https://github.com/joe-re/object-dig/pull/9)) [@EricRoos](https://github.com/EricRoos) 12 | 13 | ## 0.1.1 (2017-04-17) 14 | 15 | ### Fix 16 | 17 | - Fix issue with undefined target case([#5](https://github.com/joe-re/object-dig/pull/5)) [@sirwolfgang](https://github.com/sirwolfgang) 18 | 19 | ## 0.1.0 (2016-02-09) 20 | 21 | ### Features 22 | 23 | - be enable dig resolving function object([Resolve callback](https://github.com/joe-re/object-dig/pull/3)) 24 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "object-dig", 3 | "version": "0.1.3", 4 | "description": "This allow you to use method like Ruby's hash#dig in JavaScript.", 5 | "homepage": "https://github.com/joe-re/object-dig", 6 | "main": "dist/index.js", 7 | "repository": { 8 | "type": "git", 9 | "url": "git+https://github.com/joe-re/object-dig.git" 10 | }, 11 | "bugs": { 12 | "url": "https://github.com/joe-re/object-dig/issues" 13 | }, 14 | "scripts": { 15 | "prepublish": "mkdir -p dist && babel src/index.js -o dist/index.js", 16 | "build": "mkdir -p dist && babel src/index.js -o dist/index.js", 17 | "test": "mocha --require babel-core/register --recursive test" 18 | }, 19 | "keywords": [ 20 | "dig", 21 | "ruby", 22 | "javascript" 23 | ], 24 | "author": "joe-re", 25 | "license": "MIT", 26 | "engines": { 27 | "node": ">=4.0.0", 28 | "npm": ">=2.0.0" 29 | }, 30 | "devDependencies": { 31 | "babel": "^6.3.26", 32 | "babel-cli": "^6.4.5", 33 | "babel-core": "^6.4.5", 34 | "babel-preset-es2015": "^6.3.13", 35 | "mocha": "^2.4.5", 36 | "power-assert": "^1.2.0" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016 Masato Noguchi 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 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 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # object-dig 2 | 3 | [![Build Status](https://travis-ci.org/joe-re/object-dig.svg?branch=master)](https://travis-ci.org/joe-re/object-dig) 4 | 5 | This allow you to use method like Ruby's hash#dig in JavaScript. 6 | 7 | > http://ruby-doc.org/core-2.3.0_preview1/Hash.html#method-i-dig 8 | >> Retrieves the value object corresponding to the each key objects repeatedly. 9 | 10 | # Install 11 | 12 | ``` 13 | $ npm install --save object-dig 14 | ``` 15 | 16 | # Usage 17 | 18 | ```js 19 | var dig = require('object-dig'); 20 | 21 | var object = { a: { b: { c: 'c' } } }; 22 | 23 | dig(object, 'a', 'b'); 24 | // => { c: 'c' } 25 | 26 | dig(object, 'a', 'b', 'c'); 27 | // => 'c' 28 | 29 | dig(object, 'a', 'unknownProp', 'c'); 30 | // =>undefined 31 | ``` 32 | 33 | and you can give function object to dig. 34 | Function object's argument is result of just before evaluating. 35 | 36 | ```js 37 | dig(object, 'a', 'b', 'c', (val) => `${val} was changed`); 38 | // => 'c was changed' 39 | 40 | dig(object, 'a', 'b', 'c', (val) => `${val} was changed`, (val) => `${val} more`); 41 | // => 'c was changed more' 42 | ``` 43 | 44 | # License 45 | 46 | MIT © [joe-re](https://github.com/joe-re) 47 | -------------------------------------------------------------------------------- /test/dig_test.js: -------------------------------------------------------------------------------- 1 | import dig from '../src/index'; 2 | import assert from 'power-assert'; 3 | 4 | describe('dig', () => { 5 | let target = {}; 6 | 7 | context('target is null.', () => { 8 | it('get undefined', () => { 9 | assert.equal(dig(null, 'unknownProp'), undefined); 10 | }); 11 | }); 12 | context('target is undefined.', () => { 13 | it('get undefined', () => { 14 | assert.equal(dig(undefined, 'unknownProp'), undefined); 15 | }); 16 | }); 17 | 18 | context('target is empty object.', () => { 19 | it('get undefined', () => { 20 | assert.equal(dig(target, 'unknownProp'), undefined); 21 | }); 22 | }); 23 | 24 | context('intermediate value is null', () => { 25 | before(() => { 26 | target = { a: { b: null}} 27 | }); 28 | it('get undefined', () => { 29 | assert.equal(dig(target, 'a', 'b', 'c'), undefined); 30 | }); 31 | }); 32 | context('find unknown key on the way of digging.', () => { 33 | before(() => { 34 | target = { a: { b: { c: 'c' } } }; 35 | }); 36 | it('get undefined', () => { 37 | assert.equal(dig(target, 'a', 'unknownProp', 'b'), undefined); 38 | }); 39 | }); 40 | 41 | context('find value matching keys.', () => { 42 | before(() => { 43 | target = { a: { b: { c: 'c' } } }; 44 | }); 45 | context('one argument.', () => { 46 | it('get value', () => { 47 | assert.equal(dig(target, 'a'), target.a); 48 | }); 49 | }); 50 | 51 | context('two arguments.', () => { 52 | it('get value', () => { 53 | assert.equal(dig(target, 'a', 'b'), target.a.b); 54 | }); 55 | }); 56 | 57 | context('three arguments.', () => { 58 | it('get value', () => { 59 | assert.equal(dig(target, 'a', 'b', 'c'), target.a.b.c); 60 | }); 61 | }); 62 | }); 63 | 64 | context('give function object.', () => { 65 | before(() => { 66 | target = { a: { b: { c: 'c' } } }; 67 | }); 68 | 69 | it('executes function and receives result.', () => { 70 | assert.equal(dig(target, 'a', 'b', 'c', (val) => `${val} was changed!`), `${target.a.b.c} was changed!`); 71 | }); 72 | }); 73 | 74 | }); 75 | --------------------------------------------------------------------------------