├── .editorconfig ├── .eslintrc.json ├── .gitattributes ├── .gitignore ├── .travis.yml ├── .verb.md ├── LICENSE ├── README.md ├── docs └── usage.md ├── example.js ├── index.js ├── package.json └── test └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org/ 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | end_of_line = lf 7 | indent_size = 2 8 | indent_style = space 9 | insert_final_newline = true 10 | trim_trailing_whitespace = true 11 | 12 | [{**/{actual,fixtures,expected,templates}/**,*.md}] 13 | trim_trailing_whitespace = false 14 | insert_final_newline = false 15 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "eslint:recommended" 4 | ], 5 | 6 | "env": { 7 | "browser": false, 8 | "es6": true, 9 | "node": true, 10 | "mocha": true 11 | }, 12 | 13 | "parserOptions":{ 14 | "ecmaVersion": 9, 15 | "sourceType": "module", 16 | "ecmaFeatures": { 17 | "modules": true, 18 | "experimentalObjectRestSpread": true 19 | } 20 | }, 21 | 22 | "globals": { 23 | "document": false, 24 | "navigator": false, 25 | "window": false 26 | }, 27 | 28 | "rules": { 29 | "accessor-pairs": 2, 30 | "arrow-spacing": [2, { "before": true, "after": true }], 31 | "block-spacing": [2, "always"], 32 | "brace-style": [2, "1tbs", { "allowSingleLine": true }], 33 | "comma-dangle": [2, "never"], 34 | "comma-spacing": [2, { "before": false, "after": true }], 35 | "comma-style": [2, "last"], 36 | "constructor-super": 2, 37 | "curly": [2, "multi-line"], 38 | "dot-location": [2, "property"], 39 | "eol-last": 2, 40 | "eqeqeq": [2, "allow-null"], 41 | "generator-star-spacing": [2, { "before": true, "after": true }], 42 | "handle-callback-err": [2, "^(err|error)$" ], 43 | "indent": [2, 2, { "SwitchCase": 1 }], 44 | "key-spacing": [2, { "beforeColon": false, "afterColon": true }], 45 | "keyword-spacing": [2, { "before": true, "after": true }], 46 | "new-cap": [2, { "newIsCap": true, "capIsNew": false }], 47 | "new-parens": 2, 48 | "no-array-constructor": 2, 49 | "no-caller": 2, 50 | "no-class-assign": 2, 51 | "no-cond-assign": 2, 52 | "no-const-assign": 2, 53 | "no-control-regex": 2, 54 | "no-debugger": 2, 55 | "no-delete-var": 2, 56 | "no-dupe-args": 2, 57 | "no-dupe-class-members": 2, 58 | "no-dupe-keys": 2, 59 | "no-duplicate-case": 2, 60 | "no-empty-character-class": 2, 61 | "no-eval": 2, 62 | "no-ex-assign": 2, 63 | "no-extend-native": 2, 64 | "no-extra-bind": 2, 65 | "no-extra-boolean-cast": 2, 66 | "no-extra-parens": [2, "functions"], 67 | "no-fallthrough": 2, 68 | "no-floating-decimal": 2, 69 | "no-func-assign": 2, 70 | "no-implied-eval": 2, 71 | "no-inner-declarations": [2, "functions"], 72 | "no-invalid-regexp": 2, 73 | "no-irregular-whitespace": 2, 74 | "no-iterator": 2, 75 | "no-label-var": 2, 76 | "no-labels": 2, 77 | "no-lone-blocks": 2, 78 | "no-mixed-spaces-and-tabs": 2, 79 | "no-multi-spaces": 2, 80 | "no-multi-str": 2, 81 | "no-multiple-empty-lines": [2, { "max": 1 }], 82 | "no-native-reassign": 0, 83 | "no-negated-in-lhs": 2, 84 | "no-new": 2, 85 | "no-new-func": 2, 86 | "no-new-object": 2, 87 | "no-new-require": 2, 88 | "no-new-wrappers": 2, 89 | "no-obj-calls": 2, 90 | "no-octal": 2, 91 | "no-octal-escape": 2, 92 | "no-proto": 0, 93 | "no-redeclare": 2, 94 | "no-regex-spaces": 2, 95 | "no-return-assign": 2, 96 | "no-self-compare": 2, 97 | "no-sequences": 2, 98 | "no-shadow-restricted-names": 2, 99 | "no-spaced-func": 2, 100 | "no-sparse-arrays": 2, 101 | "no-this-before-super": 2, 102 | "no-throw-literal": 2, 103 | "no-trailing-spaces": 0, 104 | "no-undef": 2, 105 | "no-undef-init": 2, 106 | "no-unexpected-multiline": 2, 107 | "no-unneeded-ternary": [2, { "defaultAssignment": false }], 108 | "no-unreachable": 2, 109 | "no-unused-vars": [2, { "vars": "all", "args": "none" }], 110 | "no-useless-call": 0, 111 | "no-with": 2, 112 | "one-var": [0, { "initialized": "never" }], 113 | "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }], 114 | "padded-blocks": [0, "never"], 115 | "quotes": [2, "single", "avoid-escape"], 116 | "radix": 2, 117 | "semi": [2, "always"], 118 | "semi-spacing": [2, { "before": false, "after": true }], 119 | "space-before-blocks": [2, "always"], 120 | "space-before-function-paren": [2, "never"], 121 | "space-in-parens": [2, "never"], 122 | "space-infix-ops": 2, 123 | "space-unary-ops": [2, { "words": true, "nonwords": false }], 124 | "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }], 125 | "use-isnan": 2, 126 | "valid-typeof": 2, 127 | "wrap-iife": [2, "any"], 128 | "yoda": [2, "never"] 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Enforce Unix newlines 2 | * text eol=lf 3 | 4 | # binaries 5 | *.ai binary 6 | *.psd binary 7 | *.jpg binary 8 | *.gif binary 9 | *.png binary 10 | *.jpeg binary -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # always ignore files 2 | *.DS_Store 3 | .idea 4 | .vscode 5 | *.sublime-* 6 | 7 | # test related, or directories generated by tests 8 | test/actual 9 | actual 10 | coverage 11 | .nyc* 12 | 13 | # npm 14 | node_modules 15 | npm-debug.log 16 | 17 | # yarn 18 | yarn.lock 19 | yarn-error.log 20 | 21 | # misc 22 | _gh_pages 23 | _draft 24 | _drafts 25 | bower_components 26 | vendor 27 | temp 28 | tmp 29 | TODO.md 30 | package-lock.json -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | os: 3 | - linux 4 | - osx 5 | language: node_js 6 | node_js: 7 | - node 8 | - '10' 9 | - '9' 10 | - '8' 11 | - '7' 12 | - '6' 13 | - '5' 14 | - '4' 15 | -------------------------------------------------------------------------------- /.verb.md: -------------------------------------------------------------------------------- 1 | ## Usage 2 | 3 | ```js 4 | const expand = require('expand-hash'); 5 | const obj = { 6 | 'foo.bar.bar': 'some value', 7 | 'foo.qux': 'another value', 8 | fez: true 9 | }; 10 | 11 | console.log(expand(obj)); 12 | // { 13 | // foo: { bar: { bar: 'some value' }, qux: 'another value' }, 14 | // fez: true 15 | // } 16 | ``` 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014-2018, Brian Woodward. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # expand-hash [![NPM version](https://img.shields.io/npm/v/expand-hash.svg?style=flat)](https://www.npmjs.com/package/expand-hash) [![NPM monthly downloads](https://img.shields.io/npm/dm/expand-hash.svg?style=flat)](https://npmjs.org/package/expand-hash) [![NPM total downloads](https://img.shields.io/npm/dt/expand-hash.svg?style=flat)](https://npmjs.org/package/expand-hash) [![Linux Build Status](https://img.shields.io/travis/doowb/expand-hash.svg?style=flat&label=Travis)](https://travis-ci.org/doowb/expand-hash) 2 | 3 | > Recursively expands property keys with dot-notation into objects. 4 | 5 | Please consider following this project's author, [Brian Woodward](https://github.com/doowb), and consider starring the project to show your :heart: and support. 6 | 7 | ## Install 8 | 9 | Install with [npm](https://www.npmjs.com/): 10 | 11 | ```sh 12 | $ npm install --save expand-hash 13 | ``` 14 | 15 | ## Usage 16 | 17 | ```js 18 | const expand = require('expand-hash'); 19 | const obj = { 20 | 'foo.bar.bar': 'some value', 21 | 'foo.qux': 'another value', 22 | fez: true 23 | }; 24 | 25 | console.log(expand(obj)); 26 | // { 27 | // foo: { bar: { bar: 'some value' }, qux: 'another value' }, 28 | // fez: true 29 | // } 30 | ``` 31 | 32 | ## About 33 | 34 |
35 | Contributing 36 | 37 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). 38 | 39 |
40 | 41 |
42 | Running Tests 43 | 44 | Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command: 45 | 46 | ```sh 47 | $ npm install && npm test 48 | ``` 49 | 50 |
51 | 52 |
53 | Building docs 54 | 55 | _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ 56 | 57 | To generate the readme, run the following command: 58 | 59 | ```sh 60 | $ npm install -g verbose/verb#dev verb-generate-readme && verb 61 | ``` 62 | 63 |
64 | 65 | ### Related projects 66 | 67 | * [expand-object](https://www.npmjs.com/package/expand-object): Expand a string into a JavaScript object using a simple notation. Use the CLI or… [more](https://github.com/jonschlinkert/expand-object) | [homepage](https://github.com/jonschlinkert/expand-object "Expand a string into a JavaScript object using a simple notation. Use the CLI or as a node.js lib.") 68 | * [stringify-keys](https://www.npmjs.com/package/stringify-keys): Build an array of key paths from an object. | [homepage](https://github.com/doowb/stringify-keys "Build an array of key paths from an object.") 69 | 70 | ### Contributors 71 | 72 | | **Commits** | **Contributor** | 73 | | --- | --- | 74 | | 19 | [doowb](https://github.com/doowb) | 75 | | 8 | [jonschlinkert](https://github.com/jonschlinkert) | 76 | | 1 | [cconrad](https://github.com/cconrad) | 77 | 78 | ### Author 79 | 80 | **Brian Woodward** 81 | 82 | * [LinkedIn Profile](https://linkedin.com/in/woodwardbrian) 83 | * [GitHub Profile](https://github.com/doowb) 84 | * [Twitter Profile](https://twitter.com/doowb) 85 | 86 | ### License 87 | 88 | Copyright © 2018, [Brian Woodward](https://github.com/doowb). 89 | Released under the [MIT License](LICENSE). 90 | 91 | *** 92 | 93 | _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on May 15, 2018._ -------------------------------------------------------------------------------- /docs/usage.md: -------------------------------------------------------------------------------- 1 | Use dot notation to recursively expand object keys into nested objects. 2 | 3 | ```js 4 | var expandHash = require('expand-hash'); 5 | var hash = {'foo.bar.baz.bang': 'fez', a: 'b'}; 6 | console.log(expandHash(hash)); 7 | ``` 8 | 9 | expands to: 10 | 11 | ```js 12 | // the original key's value is inherited by the last object 13 | {foo: {bar: {baz: {bang: 'fez'} } }, a: 'b'} 14 | ``` 15 | 16 | Also recursively expands keys in nested objects, so that: 17 | 18 | ```js 19 | { one: { 'two.three.four.five': 'bar'}, bang: 'fez'} 20 | ``` 21 | expands to: 22 | 23 | ```js 24 | { one: {two: {three: {four: {five: 'bar'} } } }, bang: 'fez' } 25 | ``` -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | const expand = require('./'); 2 | const obj = { 3 | 'foo.bar.bar': 'some value', 4 | 'foo.qux': 'another value', 5 | fez: true 6 | }; 7 | console.log(expand(obj)); 8 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function expand(value) { 4 | if (!isObject(value)) return value; 5 | const res = Array.isArray(value) ? [] : {}; 6 | for (const key of Object.keys(value)) { 7 | set(res, key, expand(value[key])); 8 | } 9 | return res; 10 | }; 11 | 12 | function set(obj, prop, val) { 13 | const segs = split(prop); 14 | const last = segs.pop(); 15 | while (segs.length) { 16 | const key = segs.shift(); 17 | obj = obj[key] || (obj[key] = {}); 18 | } 19 | obj[last] = val; 20 | } 21 | 22 | function split(str) { 23 | const segs = str.split('.'); 24 | const keys = []; 25 | for (let i = 0; i < segs.length; i++) { 26 | const seg = segs[i]; 27 | while (seg.slice(-1) === '\\') { 28 | seg = seg.slice(0, -1) + '.' + (segs[++i] || ''); 29 | } 30 | keys.push(seg); 31 | } 32 | return keys; 33 | } 34 | 35 | function isObject(val) { 36 | return val !== null && typeof val === 'object'; 37 | } 38 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "expand-hash", 3 | "description": "Recursively expands property keys with dot-notation into objects.", 4 | "version": "1.0.1", 5 | "homepage": "https://github.com/doowb/expand-hash", 6 | "author": "Brian Woodward (https://github.com/doowb)", 7 | "repository": "doowb/expand-hash", 8 | "bugs": { 9 | "url": "https://github.com/doowb/expand-hash/issues" 10 | }, 11 | "license": "MIT", 12 | "files": [ 13 | "index.js" 14 | ], 15 | "main": "index.js", 16 | "engines": { 17 | "node": ">=4" 18 | }, 19 | "scripts": { 20 | "test": "mocha" 21 | }, 22 | "devDependencies": { 23 | "gulp-format-md": "^1.0.0", 24 | "mocha": "^5.1.1" 25 | }, 26 | "keywords": [ 27 | "config values", 28 | "dot notation", 29 | "expand", 30 | "hash", 31 | "keys", 32 | "object", 33 | "string", 34 | "values" 35 | ], 36 | "verb": { 37 | "toc": false, 38 | "layout": "default", 39 | "tasks": [ 40 | "readme" 41 | ], 42 | "data": { 43 | "author": { 44 | "linkedin": "woodwardbrian", 45 | "twitter": "doowb" 46 | } 47 | }, 48 | "plugins": [ 49 | "gulp-format-md" 50 | ], 51 | "related": { 52 | "list": [ 53 | "stringify-keys", 54 | "expand-object" 55 | ] 56 | }, 57 | "lint": { 58 | "reflinks": true 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | require('mocha'); 4 | const assert = require('assert'); 5 | const expandHash = require('..'); 6 | 7 | describe('expand-hash', () => { 8 | it('expand simple hash', () => { 9 | assert.deepEqual(expandHash({ foo: 'bar', baz: 'bang' }), { foo: 'bar', baz: 'bang' }); 10 | }); 11 | 12 | it('expand multiple props with the same keys', () => { 13 | assert.deepEqual(expandHash({ 'foo.bar': 'bar', 'foo.baz': 'baz', 'beep.boop': 'bop', something: 'else' }), { 14 | foo: { bar: 'bar', baz: 'baz' }, 15 | beep: { boop: 'bop' }, 16 | something: 'else' 17 | }); 18 | }); 19 | 20 | it('expand to deeply nested object', () => { 21 | const complex = { 'one.two.three.four.five': 'bar', bang: 'fez' }; 22 | const expected = { one: { two: { three: { four: { five: 'bar' } } } }, bang: 'fez' }; 23 | const actual = expandHash(complex); 24 | assert.deepEqual(actual, expected); 25 | }); 26 | 27 | it('should expand nested keys into objects', () => { 28 | assert.deepEqual(expandHash({ one: { 'two.three.four.five': 'bar' }, bang: 'fez' }), { 29 | one: { two: { three: { four: { five: 'bar' } } } }, 30 | bang: 'fez' 31 | }); 32 | 33 | assert.deepEqual(expandHash({ a: ['b', 'c'], one: { 'two.three.four.five': 'bar' }, bang: 'fez' }), { 34 | a: ['b', 'c'], 35 | one: { two: { three: { four: { five: 'bar' } } } }, 36 | bang: 'fez' 37 | }); 38 | }); 39 | 40 | it('should expand keys on object values into objects', () => { 41 | assert.deepEqual(expandHash({ a: ['b', 'c'], one: { two: { three: { 'four.five': 'bar' } } }, bang: 'fez' }), { 42 | a: ['b', 'c'], 43 | one: { two: { three: { four: { five: 'bar' } } } }, 44 | bang: 'fez' 45 | }); 46 | }); 47 | 48 | it('should expand keys in arrays into objects', () => { 49 | assert.deepEqual(expandHash({ a: [{'a.b': 'c'}, 'c'], one: { two: { three: { 'four.five': 'bar' } } }, bang: 'fez' }), { 50 | a: [ { a: { b: 'c' } }, 'c' ], 51 | one: { two: { three: { four: { five: 'bar' } } } }, 52 | bang: 'fez' 53 | }); 54 | }); 55 | }); 56 | --------------------------------------------------------------------------------