├── .editorconfig ├── .eslintrc.json ├── .gitattributes ├── .gitignore ├── .travis.yml ├── .verb.md ├── LICENSE ├── README.md ├── example.js ├── index.js ├── package.json └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | end_of_line = lf 7 | charset = utf-8 8 | indent_size = 2 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | insert_final_newline = false 15 | 16 | [test/**] 17 | trim_trailing_whitespace = false 18 | insert_final_newline = false 19 | 20 | [templates/**] 21 | trim_trailing_whitespace = false 22 | insert_final_newline = false 23 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "ecmaFeatures": { 3 | "modules": true, 4 | "experimentalObjectRestSpread": true 5 | }, 6 | "env": { 7 | "browser": false, 8 | "es6": true, 9 | "node": true, 10 | "mocha": true 11 | }, 12 | "globals": { 13 | "document": false, 14 | "navigator": false, 15 | "window": false 16 | }, 17 | "rules": { 18 | "accessor-pairs": 2, 19 | "arrow-spacing": [ 20 | 2, 21 | { 22 | "before": true, 23 | "after": true 24 | } 25 | ], 26 | "block-spacing": [ 27 | 2, 28 | "always" 29 | ], 30 | "brace-style": [ 31 | 2, 32 | "1tbs", 33 | { 34 | "allowSingleLine": true 35 | } 36 | ], 37 | "comma-dangle": [ 38 | 2, 39 | "never" 40 | ], 41 | "comma-spacing": [ 42 | 2, 43 | { 44 | "before": false, 45 | "after": true 46 | } 47 | ], 48 | "comma-style": [ 49 | 2, 50 | "last" 51 | ], 52 | "constructor-super": 2, 53 | "curly": [ 54 | 2, 55 | "multi-line" 56 | ], 57 | "dot-location": [ 58 | 2, 59 | "property" 60 | ], 61 | "eol-last": 2, 62 | "eqeqeq": [ 63 | 2, 64 | "allow-null" 65 | ], 66 | "generator-star-spacing": [ 67 | 2, 68 | { 69 | "before": true, 70 | "after": true 71 | } 72 | ], 73 | "handle-callback-err": [ 74 | 2, 75 | "^(err|error)$" 76 | ], 77 | "indent": [ 78 | 2, 79 | 2, 80 | { 81 | "SwitchCase": 1 82 | } 83 | ], 84 | "key-spacing": [ 85 | 2, 86 | { 87 | "beforeColon": false, 88 | "afterColon": true 89 | } 90 | ], 91 | "new-cap": [ 92 | 2, 93 | { 94 | "newIsCap": true, 95 | "capIsNew": false 96 | } 97 | ], 98 | "new-parens": 2, 99 | "no-array-constructor": 2, 100 | "no-caller": 2, 101 | "no-class-assign": 2, 102 | "no-cond-assign": 2, 103 | "no-const-assign": 2, 104 | "no-control-regex": 2, 105 | "no-debugger": 2, 106 | "no-delete-var": 2, 107 | "no-dupe-args": 2, 108 | "no-dupe-class-members": 2, 109 | "no-dupe-keys": 2, 110 | "no-duplicate-case": 2, 111 | "no-empty-character-class": 2, 112 | "no-empty-label": 2, 113 | "no-eval": 2, 114 | "no-ex-assign": 2, 115 | "no-extend-native": 2, 116 | "no-extra-bind": 2, 117 | "no-extra-boolean-cast": 2, 118 | "no-extra-parens": [ 119 | 2, 120 | "functions" 121 | ], 122 | "no-fallthrough": 2, 123 | "no-floating-decimal": 2, 124 | "no-func-assign": 2, 125 | "no-implied-eval": 2, 126 | "no-inner-declarations": [ 127 | 2, 128 | "functions" 129 | ], 130 | "no-invalid-regexp": 2, 131 | "no-irregular-whitespace": 2, 132 | "no-iterator": 2, 133 | "no-label-var": 2, 134 | "no-labels": 2, 135 | "no-lone-blocks": 2, 136 | "no-mixed-spaces-and-tabs": 2, 137 | "no-multi-spaces": 2, 138 | "no-multi-str": 2, 139 | "no-multiple-empty-lines": [ 140 | 2, 141 | { 142 | "max": 1 143 | } 144 | ], 145 | "no-native-reassign": 2, 146 | "no-negated-in-lhs": 2, 147 | "no-new": 2, 148 | "no-new-func": 2, 149 | "no-new-object": 2, 150 | "no-new-require": 2, 151 | "no-new-wrappers": 2, 152 | "no-obj-calls": 2, 153 | "no-octal": 2, 154 | "no-octal-escape": 2, 155 | "no-proto": 0, 156 | "no-redeclare": 2, 157 | "no-regex-spaces": 2, 158 | "no-return-assign": 2, 159 | "no-self-compare": 2, 160 | "no-sequences": 2, 161 | "no-shadow-restricted-names": 2, 162 | "no-spaced-func": 2, 163 | "no-sparse-arrays": 2, 164 | "no-this-before-super": 2, 165 | "no-throw-literal": 2, 166 | "no-trailing-spaces": 0, 167 | "no-undef": 2, 168 | "no-undef-init": 2, 169 | "no-unexpected-multiline": 2, 170 | "no-unneeded-ternary": [ 171 | 2, 172 | { 173 | "defaultAssignment": false 174 | } 175 | ], 176 | "no-unreachable": 2, 177 | "no-unused-vars": [ 178 | 2, 179 | { 180 | "vars": "all", 181 | "args": "none" 182 | } 183 | ], 184 | "no-useless-call": 0, 185 | "no-with": 2, 186 | "one-var": [ 187 | 0, 188 | { 189 | "initialized": "never" 190 | } 191 | ], 192 | "operator-linebreak": [ 193 | 0, 194 | "after", 195 | { 196 | "overrides": { 197 | "?": "before", 198 | ":": "before" 199 | } 200 | } 201 | ], 202 | "padded-blocks": [ 203 | 0, 204 | "never" 205 | ], 206 | "quotes": [ 207 | 2, 208 | "single", 209 | "avoid-escape" 210 | ], 211 | "radix": 2, 212 | "semi": [ 213 | 2, 214 | "always" 215 | ], 216 | "semi-spacing": [ 217 | 2, 218 | { 219 | "before": false, 220 | "after": true 221 | } 222 | ], 223 | "space-after-keywords": [ 224 | 2, 225 | "always" 226 | ], 227 | "space-before-blocks": [ 228 | 2, 229 | "always" 230 | ], 231 | "space-before-function-paren": [ 232 | 2, 233 | "never" 234 | ], 235 | "space-before-keywords": [ 236 | 2, 237 | "always" 238 | ], 239 | "space-in-parens": [ 240 | 2, 241 | "never" 242 | ], 243 | "space-infix-ops": 2, 244 | "space-return-throw-case": 2, 245 | "space-unary-ops": [ 246 | 2, 247 | { 248 | "words": true, 249 | "nonwords": false 250 | } 251 | ], 252 | "spaced-comment": [ 253 | 0, 254 | "always", 255 | { 256 | "markers": [ 257 | "global", 258 | "globals", 259 | "eslint", 260 | "eslint-disable", 261 | "*package", 262 | "!", 263 | "," 264 | ] 265 | } 266 | ], 267 | "use-isnan": 2, 268 | "valid-typeof": 2, 269 | "wrap-iife": [ 270 | 2, 271 | "any" 272 | ], 273 | "yoda": [ 274 | 2, 275 | "never" 276 | ] 277 | } 278 | } 279 | -------------------------------------------------------------------------------- /.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 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.DS_Store 2 | *.sublime-* 3 | _gh_pages 4 | bower_components 5 | node_modules 6 | npm-debug.log 7 | actual 8 | test/actual 9 | temp 10 | tmp 11 | TODO.md 12 | vendor 13 | .idea 14 | benchmark 15 | coverage 16 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - "stable" 5 | - "5" 6 | - "4" 7 | - "0.12" 8 | - "0.10" 9 | matrix: 10 | fast_finish: true 11 | allow_failures: 12 | - node_js: "0.10" 13 | -------------------------------------------------------------------------------- /.verb.md: -------------------------------------------------------------------------------- 1 | # {%= name %} {%= badge("fury") %} {%= badge("travis") %} 2 | 3 | > {%= description %} 4 | 5 | ## Install 6 | {%= include("install-npm", {save: true}) %} 7 | 8 | ## Usage 9 | 10 | ```js 11 | var toPath = require('{%= name %}'); 12 | 13 | toPath('foo', 'bar', 'baz'); 14 | toPath('foo', ['bar', 'baz']); 15 | //=> 'foo.bar.baz' 16 | ``` 17 | 18 | Also supports passing an arguments object (without having to slice args): 19 | 20 | ```js 21 | function foo() 22 | return toPath(arguments); 23 | } 24 | 25 | foo('foo', 'bar', 'baz'); 26 | foo('foo', ['bar', 'baz']); 27 | //=> 'foo.bar.baz' 28 | ``` 29 | 30 | Visit the [example](./example.js) to see how this could be used in an application. 31 | 32 | ## Related projects 33 | {%= related(verb.related.list) %} 34 | 35 | ## Running tests 36 | {%= include("tests") %} 37 | 38 | ## Contributing 39 | {%= include("contributing") %} 40 | 41 | ## Author 42 | {%= include("author") %} 43 | 44 | ## License 45 | {%= copyright() %} 46 | {%= license %} 47 | 48 | *** 49 | 50 | {%= include("footer") %} 51 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015-2016, Jon Schlinkert. 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 | # to-object-path [![NPM version](https://badge.fury.io/js/to-object-path.svg)](http://badge.fury.io/js/to-object-path) 2 | 3 | > Create an object path from a list or array of strings. 4 | 5 | ## Install 6 | 7 | Install with [npm](https://www.npmjs.com/) 8 | 9 | ```sh 10 | $ npm i to-object-path --save 11 | ``` 12 | 13 | ## Usage 14 | 15 | ```js 16 | var toPath = require('to-object-path'); 17 | 18 | toPath('foo', 'bar', 'baz'); 19 | toPath('foo', ['bar', 'baz']); 20 | //=> 'foo.bar.baz' 21 | ``` 22 | 23 | Also supports passing an arguments object (without having to slice args): 24 | 25 | ```js 26 | function foo() 27 | return toPath(arguments); 28 | } 29 | 30 | foo('foo', 'bar', 'baz'); 31 | foo('foo', ['bar', 'baz']); 32 | //=> 'foo.bar.baz' 33 | ``` 34 | 35 | Visit the [example](./example.js) to see how this could be used in an application. 36 | 37 | ## Related projects 38 | 39 | * [get-value](https://www.npmjs.com/package/get-value): Use property paths (` a.b.c`) to get a nested value from an object. | [homepage](https://github.com/jonschlinkert/get-value) 40 | * [has-value](https://www.npmjs.com/package/has-value): Returns true if a value exists, false if empty. Works with deeply nested values using… [more](https://www.npmjs.com/package/has-value) | [homepage](https://github.com/jonschlinkert/has-value) 41 | * [omit-value](https://www.npmjs.com/package/omit-value): Omit properties from an object or deeply nested property of an object using object path… [more](https://www.npmjs.com/package/omit-value) | [homepage](https://github.com/jonschlinkert/omit-value) 42 | * [set-value](https://www.npmjs.com/package/set-value): Create nested values and any intermediaries using dot notation (`'a.b.c'`) paths. | [homepage](https://github.com/jonschlinkert/set-value) 43 | * [unset-value](https://www.npmjs.com/package/unset-value): Delete nested properties from an object using dot notation. | [homepage](https://github.com/jonschlinkert/unset-value) 44 | 45 | ## Running tests 46 | 47 | Install dev dependencies: 48 | 49 | ```sh 50 | $ npm i -d && npm test 51 | ``` 52 | 53 | ## Contributing 54 | 55 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/to-object-path/issues/new). 56 | 57 | ## Author 58 | 59 | **Jon Schlinkert** 60 | 61 | + [github/jonschlinkert](https://github.com/jonschlinkert) 62 | + [twitter/jonschlinkert](http://twitter.com/jonschlinkert) 63 | 64 | ## License 65 | 66 | Copyright © 2015 Jon Schlinkert 67 | Released under the MIT license. 68 | 69 | *** 70 | 71 | _This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on October 28, 2015._ -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var Base = require('base'); 4 | var toPath = require('./'); 5 | 6 | function App(options) { 7 | Base.call(this); 8 | this.options = options || {}; 9 | } 10 | 11 | Base.extend(App); 12 | 13 | App.prototype.option = function(key, value) { 14 | var path = toPath('options', key); 15 | if (arguments.length === 1) { 16 | return this.get(path, value); 17 | } 18 | this.set(path, value); 19 | return this; 20 | }; 21 | 22 | var app = new App(); 23 | app.option('foo.bar', 'baz'); 24 | 25 | console.log(app); 26 | //=> {options: { foo: { bar: 'baz' }}} 27 | 28 | console.log(app.option('foo')); 29 | //=> { bar: 'baz' } 30 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * to-object-path 3 | * 4 | * Copyright (c) 2015, Jon Schlinkert. 5 | * Licensed under the MIT License. 6 | */ 7 | 8 | 'use strict'; 9 | 10 | var typeOf = require('kind-of'); 11 | 12 | module.exports = function toPath(args) { 13 | if (typeOf(args) !== 'arguments') { 14 | args = arguments; 15 | } 16 | return filter(args).join('.'); 17 | }; 18 | 19 | function filter(arr) { 20 | var len = arr.length; 21 | var idx = -1; 22 | var res = []; 23 | 24 | while (++idx < len) { 25 | var ele = arr[idx]; 26 | if (typeOf(ele) === 'arguments' || Array.isArray(ele)) { 27 | res.push.apply(res, filter(ele)); 28 | } else if (typeof ele === 'string') { 29 | res.push(ele); 30 | } 31 | } 32 | return res; 33 | } 34 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "to-object-path", 3 | "description": "Create an object path from a list or array of strings.", 4 | "version": "0.3.0", 5 | "homepage": "https://github.com/jonschlinkert/to-object-path", 6 | "author": "Jon Schlinkert (https://github.com/jonschlinkert)", 7 | "repository": "jonschlinkert/to-object-path", 8 | "bugs": { 9 | "url": "https://github.com/jonschlinkert/to-object-path/issues" 10 | }, 11 | "license": "MIT", 12 | "files": [ 13 | "index.js" 14 | ], 15 | "main": "index.js", 16 | "engines": { 17 | "node": ">=0.10.0" 18 | }, 19 | "scripts": { 20 | "test": "mocha" 21 | }, 22 | "dependencies": { 23 | "kind-of": "^3.0.2" 24 | }, 25 | "devDependencies": { 26 | "base": "^0.6.7", 27 | "mocha": "*" 28 | }, 29 | "keywords": [ 30 | "dot", 31 | "nested", 32 | "notation", 33 | "object", 34 | "path", 35 | "stringify" 36 | ], 37 | "verb": { 38 | "related": { 39 | "list": [ 40 | "get-value", 41 | "set-value", 42 | "has-value", 43 | "omit-value", 44 | "unset-value" 45 | ] 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | require('mocha'); 4 | var assert = require('assert'); 5 | var toPath = require('./'); 6 | 7 | describe('toPath', function() { 8 | it('should create an object path from a list of strings:', function() { 9 | assert.equal(toPath('foo', 'bar', 'baz'), 'foo.bar.baz'); 10 | }); 11 | 12 | it('should create an object path from an array of strings:', function() { 13 | assert.equal(toPath(['foo', 'bar', 'baz']), 'foo.bar.baz'); 14 | }); 15 | 16 | it('should flatten array args', function() { 17 | assert.equal(toPath(['foo', ['bar', ['baz']]]), 'foo.bar.baz'); 18 | }); 19 | 20 | it('should create an object path from a list of arguments:', function() { 21 | function foo() { 22 | return toPath(arguments); 23 | } 24 | assert.equal(foo('foo', 'bar', 'baz'), 'foo.bar.baz'); 25 | }); 26 | 27 | it('should create an object path from an array of arguments:', function() { 28 | function foo() { 29 | return toPath(arguments); 30 | } 31 | assert.equal(foo(['foo', 'bar', 'baz']), 'foo.bar.baz'); 32 | }); 33 | 34 | it('should work when arguments is not the first value:', function() { 35 | function foo() { 36 | return toPath('options', arguments); 37 | } 38 | assert.equal(foo(['foo', 'bar', 'baz']), 'options.foo.bar.baz'); 39 | }); 40 | 41 | it('should not use non-string values in the path', function() { 42 | function foo() { 43 | return toPath('options', arguments); 44 | } 45 | var fn = function() { 46 | return; 47 | }; 48 | assert.equal(foo([fn, 'foo', 'bar', 'baz'], fn), 'options.foo.bar.baz'); 49 | }); 50 | 51 | it('should create an object path from a mixture of arrays and strings:', function() { 52 | assert.equal(toPath('foo', ['bar', 'baz']), 'foo.bar.baz'); 53 | assert.equal(toPath(['foo'], ['bar'], ['baz']), 'foo.bar.baz'); 54 | assert.equal(toPath(['foo'], ['bar'], ['baz']), 'foo.bar.baz'); 55 | }); 56 | }); 57 | --------------------------------------------------------------------------------