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 | ## Usage example
|
24 |
|
25 | ```js
|
26 | // `base-methods` has `get` and `set` methods that
|
27 | // support object-path notation
|
28 | var Base = require('base-methods');
|
29 | var toPath = require('to-object-path');
|
30 |
|
31 | // inherit Base
|
32 | function App(options) {
|
33 | Base.call(this);
|
34 | this.options = options || {};
|
35 | }
|
36 | Base.extend(App);
|
37 |
|
38 | App.prototype.option = function(key, value) {
|
39 | // use the `get` and `set` methods to set
|
40 | // and get values on `this.options`
|
41 | var path = toPath('options', key);
|
42 | if (arguments.length === 1) {
|
43 | return this.get(path, value);
|
44 | }
|
45 | this.set(path, value);
|
46 | return this;
|
47 | };
|
48 |
|
49 | var app = new App();
|
50 | app.option('foo.bar', 'baz');
|
51 |
|
52 | console.log(app);
|
53 | //=> {options: { foo: { bar: 'baz' }}}
|
54 |
|
55 | console.log(app.option('foo'));
|
56 | //=> { bar: 'baz' }
|
57 | ```
|
58 |
|
59 | See the [working example](./example.js)
|
60 |
|
61 | ## Related projects
|
62 |
|
63 | * [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)
|
64 | * [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)
|
65 | * [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)
|
66 | * [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)
|
67 |
|
68 | ## Running tests
|
69 |
|
70 | Install dev dependencies:
|
71 |
|
72 | ```sh
|
73 | $ npm i -d && npm test
|
74 | ```
|
75 |
|
76 | ## Contributing
|
77 |
|
78 | 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).
|
79 |
|
80 | ## Author
|
81 |
|
82 | **Jon Schlinkert**
|
83 |
|
84 | + [github/jonschlinkert](https://github.com/jonschlinkert)
|
85 | + [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
|
86 |
|
87 | ## License
|
88 |
|
89 | Copyright © 2015 Jon Schlinkert
|
90 | Released under the MIT license.
|
91 |
|
92 | ***
|
93 |
|
94 | _This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on September 02, 2015._
|