├── .github └── workflows │ └── node.js.yml ├── .travis.yml ├── LICENSE.md ├── README.md ├── index.js ├── package-lock.json ├── package.json └── test.js /.github/workflows/node.js.yml: -------------------------------------------------------------------------------- 1 | # This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node 2 | # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs 3 | 4 | name: test 5 | 6 | on: 7 | push: 8 | branches: [ "master" ] 9 | pull_request: 10 | branches: [ "master" ] 11 | 12 | jobs: 13 | build: 14 | 15 | runs-on: ubuntu-latest 16 | 17 | strategy: 18 | matrix: 19 | node-version: [14.x, 16.x, 18.x] 20 | # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ 21 | 22 | steps: 23 | - uses: actions/checkout@v4 24 | - name: Use Node.js ${{ matrix.node-version }} 25 | uses: actions/setup-node@v3 26 | with: 27 | node-version: ${{ matrix.node-version }} 28 | cache: 'npm' 29 | - run: npm i 30 | - run: npm test 31 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.10" -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright 2019 Dmitry Ivanov 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # dotprop [](https://github.com/dy/dotprop/actions/workflows/node.js.yml) 2 | 3 | Get property by dot notation. 4 | A short and dependable implementation. 5 | 6 | ```js 7 | import getProp from 'dotprop'; 8 | 9 | getProp({a: { b: { c: 1}} }, 'a.b.c') // 1 10 | getProp([1,2,3], 2) // 3 11 | getProp({}, 'a.b') // undefined 12 | 13 | // recognizes arrays too 14 | getProp({a: { b: { c: 1}} }, ['a', 'b', 'c']) // 1 15 | getProp({a: { 'b.c': 1 }}, ['a', 'b.c']) // 1 16 | ``` 17 | 18 | ## Similar 19 | 20 | * [developit/dlv](https://github.com/developit/dlv) – the most compact alternative. 21 | * [sindresohrus/dot-prop](https://github.com/sindresorhus/dot-prop) - enables escaping `\.` at price of larger codebase. 22 | * [facebookindicator/idx](https://github.com/facebookincubator/idx) 23 | 24 | [](https://nodei.co/npm/dotprop/) 25 | 26 |
27 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module dotprop 3 | * 4 | * Get property defined by dot notation in string. 5 | * 6 | * @param {Object} holder Target object where to look property up 7 | * @param {string} propName Dot notation, like 'this.a.b.c' 8 | * @return {*} A property value 9 | */ 10 | export default (holder, propName) => { 11 | if (propName == null || !holder) return holder; 12 | var propParts = Array.isArray(propName) ? propName : (propName + '').split('.'); 13 | var result = holder, lastPropName; 14 | 15 | while ((lastPropName = propParts.shift()) != null) { 16 | if (!result[lastPropName]) return !propParts.length ? result[lastPropName] : undefined; 17 | result = result[lastPropName]; 18 | } 19 | 20 | return result; 21 | }; 22 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dotprop", 3 | "version": "2.0.0", 4 | "lockfileVersion": 3, 5 | "requires": true, 6 | "packages": { 7 | "": { 8 | "name": "dotprop", 9 | "version": "2.0.0", 10 | "license": "MIT" 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dotprop", 3 | "version": "2.0.0", 4 | "description": "Get property value by dot notation", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "node test.js" 8 | }, 9 | "type": "module", 10 | "files": [ 11 | "index.js" 12 | ], 13 | "repository": { 14 | "type": "git", 15 | "url": "git@github.com:dy/dotprop.git" 16 | }, 17 | "keywords": [ 18 | "dot-prop", 19 | "dot", 20 | "json", 21 | "property", 22 | "value", 23 | "notation", 24 | "object", 25 | "get-prop", 26 | "obj", 27 | "prop", 28 | "path", 29 | "get", 30 | "access", 31 | "getprop", 32 | "ramda.path", 33 | "dot path" 34 | ], 35 | "author": { 36 | "name": "Dima Yv", 37 | "email": "df.creative@gmail.com", 38 | "url": "http://github.com/dy" 39 | }, 40 | "license": "MIT", 41 | "bugs": { 42 | "url": "https://github.com/dy/dotprop/issues" 43 | }, 44 | "homepage": "https://github.com/dy/dotprop" 45 | } 46 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import assert from 'assert'; 2 | import getProp from './index.js'; 3 | 4 | assert.equal(getProp({ a: { b: { c: 1 } } }, 'a.b.c'), 1); 5 | assert.equal(getProp([1, 2, 3], 2), 3); 6 | assert.equal(getProp({}, 'a.b'), undefined); 7 | 8 | var f0 = { foo: 1 }; 9 | assert(getProp(f0) === f0); 10 | assert(getProp({ foo: 1 }, 'foo') === 1); 11 | assert(getProp({ foo: null }, 'foo') === null); 12 | assert(getProp({ foo: undefined }, 'foo') === undefined); 13 | assert(getProp({ foo: { bar: true } }, 'foo.bar') === true); 14 | assert(getProp({ foo: { bar: { baz: true } } }, 'foo.bar.baz') === true); 15 | assert(getProp({ foo: { bar: { baz: null } } }, 'foo.bar.baz') === null); 16 | assert(getProp({ foo: { bar: null } }, 'foo.bar.baz') === undefined); 17 | assert(getProp({ foo: { bar: 'a' } }, 'foo.fake.fake2') === undefined); 18 | 19 | assert(getProp({ a: { 'b.c': 1 } }, ['a', 'b.c']) === 1) 20 | assert(getProp({ a: { b: { c: 1 } } }, ['a', 'b', 'c']) === 1) 21 | 22 | // dot-prop tests 23 | const f1 = { foo: { bar: 1 } }; 24 | assert(getProp(f1) === f1); 25 | f1[''] = 'foo'; 26 | assert(getProp(f1, '') === 'foo'); 27 | assert(getProp(f1, 'foo') === f1.foo); 28 | assert(getProp({ foo: 1 }, 'foo') === 1); 29 | assert(getProp({ foo: null }, 'foo') === null); 30 | assert(getProp({ foo: undefined }, 'foo') === undefined); 31 | assert(getProp({ foo: { bar: true } }, 'foo.bar') === true); 32 | assert(getProp({ foo: { bar: { baz: true } } }, 'foo.bar.baz') === true); 33 | assert(getProp({ foo: { bar: { baz: null } } }, 'foo.bar.baz') === null); 34 | assert(getProp({ foo: { bar: 'a' } }, 'foo.fake') === undefined); 35 | assert(getProp({ foo: { bar: 'a' } }, 'foo.fake.fake2') === undefined); 36 | assert(getProp({ foo: { bar: 'a' } }, 'foo.fake.fake2') || 'some value' === 'some value'); 37 | assert(getProp({ '\\': true }, '\\') === true); 38 | assert(getProp({ '\\foo': true }, '\\foo') === true); 39 | assert(getProp({ 'bar\\': true }, 'bar\\') === true); 40 | assert(getProp({ 'foo\\bar': true }, 'foo\\bar') === true); 41 | assert(getProp({ '\\.foo': true }, ['\\.foo']) === true); 42 | assert(getProp({ 'bar\\.': true }, ['bar\\.']) === true); 43 | assert(getProp({ 'foo\\.bar': true }, ['foo\\.bar']) === true); 44 | assert(getProp({ foo: 1 }, 'foo.bar') === undefined); 45 | 46 | const f2 = {}; 47 | Object.defineProperty(f2, 'foo', { 48 | value: 'bar', 49 | enumerable: false 50 | }); 51 | assert(getProp(f2, 'foo') === 'bar'); 52 | // assert(getProp({}, 'hasOwnProperty') === undefined); 53 | 54 | function fn() { } 55 | fn.foo = { bar: 1 }; 56 | assert(getProp(fn) === fn); 57 | assert(getProp(fn, 'foo') === fn.foo); 58 | assert(getProp(fn, 'foo.bar') === 1); 59 | 60 | const f3 = { foo: null }; 61 | 62 | assert(getProp(f3, 'foo.bar') === undefined); 63 | assert(getProp(f3, 'foo.bar') || 'some value' === 'some value'); 64 | 65 | assert(getProp({ 'foo.baz': { bar: true } }, ['foo.baz', 'bar']) === true); 66 | assert(getProp({ 'fo.ob.az': { bar: true } }, ['fo.ob.az', 'bar']) === true); 67 | 68 | assert(getProp(null, 'foo.bar') || false === false); 69 | assert(getProp('foo', 'foo.bar') || false === false); 70 | assert(getProp([], 'foo.bar') || false === false); 71 | assert(getProp(undefined, 'foo.bar') || false === false); 72 | --------------------------------------------------------------------------------