expand-object
Version:
Expand a string into a JavaScript object using a simple notation.
200 lines (142 loc) • 4.03 kB
Markdown
# expand-object [](http://badge.fury.io/js/expand-object)
> Expand a string into a JavaScript object using a simple notation.
I created this to make it easier to generate objects for unit tests. I'd love to hear about other use cases!
**Examples**
```js
expand('a')
//=> {a: ''}
expand('a.b')
//=> {a: {b: ''}}
expand('a|b')
//=> {a: '', b: ''}
expand('a:b')
//=> {a: 'b'}
expand('a,b')
//=> ['a', 'b']
```
Usage with cli:
```sh
❯ expand-object --help
Usage: expand-object [options] <string>
Expand a string into a JavaScript object using a simple notation.
Options:
-h, --help output usage information
-V, --version output the version number
-r, --raw Output as raw javascript object - not stringified
Examples:
$ expand-object "a:b"
$ expand-object --raw "a:b"
$ echo "a:b" | expand-object
```
## Install
Install with [npm](https://www.npmjs.com/)
```sh
$ npm i expand-object --save
```
## Usage
```js
var expand = require('expand-object');
```
### dots
> Expand dots into **child objects**:
```js
expand('a')
//=> {a: ''}
expand('a.b')
//=> {a: {b: ''}}
expand('a.b.c')
//=> {a: {b: {c: ''}}}
expand('a.b.c.d')
//=> {a: {b: {c: {d: ''}}}}
```
### pipes
> Expand pipes into **sibling objects**:
```js
expand('a|b')
//=> {a: '', b: ''}
expand('a|b|c')
//=> {a: '', b: '', c: ''}
expand('a|b|c|d')
//=> {a: '', b: '', c: '', d: ''}
```
### colons
> Expand colons into **key-value pairs**:
```js
expand('a:b')
//=> {a: 'b'}
expand('a.b:c')
//=> {a: {b: 'c'}}
expand('a.b.c:d')
//=> {a: {b: {c: 'd'}}}
```
### commas
> Expand comma separated values into **arrays**:
```js
expand('a,b')
//=> ['a', 'b']
expand('a,b,c')
//=> ['a', 'b', 'c']
expand('a:b,c,d|e:f,g,h')
//=> {a: ['b', 'c', 'd'], e: ['f', 'g', 'h']}
```
## Usage examples
Expand siblings with comma separated values into arrays:
```js
expand('a:b,c,d|e:f,g,h')
//=> {a: ['b', 'c', 'd'], e: ['f', 'g', 'h']}
```
Expand children with comma separated values into arrays:
```js
expand('a.b.c:d,e,f|g.h:i,j,k')
//=> {a: { b: {c: ['d', 'e', 'f']}}, g: {h: ['i', 'j', 'k']}}
```
Expand sibling objects into key-value pairs:
```js
expand('a:b|c:d')
//=> {a: 'b', c: 'd'}
expand('a:b|c:d|e:f')
//=> {a: 'b', c: 'd', e: 'f'}
expand('a:b|c:d|e:f|g:h')
//=> {a: 'b', c: 'd', e: 'f', g: 'h'}
```
Expand child objects into key-value pairs:
```js
expand('a.b:c')
//=> {a: {b: 'c'}}
expand('a.b.c:d')
//=> {a: {b: {c: 'd'}}}
expand('a.b.c.d:e')
//=> {a: {b: {c: {d: 'e'}}}}
```
Expand sibling and child objects into key-value pairs:
```js
expand('a:b|c:d')
//=> {a: 'b', c: 'd'}
expand('a.b.c|d.e:f')
//=> {a: {b: {c: ''}}, d: {e: 'f'}}
expand('a.b:c|d.e:f')
//=> {a: {b: 'c'}, d: {e: 'f'}}
expand('a.b.c:d|e.f.g:h')
//=> {a: {b: {c: 'd'}}, e: {f: {g: 'h'}}}
```
## Related projects
* [get-value](https://github.com/jonschlinkert/get-value): Use property paths (` a.b.c`) get a nested value from an object.
* [has-value](https://github.com/jonschlinkert/has-value): Returns true if a value exists, false if empty. Works with deeply nested values using… [more](https://github.com/jonschlinkert/has-value)
* [stringify-keys](https://github.com/doowb/stringify-keys): Build an array of key paths from an object.
* [set-value](https://github.com/jonschlinkert/set-value): Create nested values and any intermediaries using dot notation (`'a.b.c'`) paths.
## Running tests
Install dev dependencies:
```sh
$ npm i -d && npm test
```
## Contributing
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/expand-object/issues/new)
## Author
**Jon Schlinkert**
+ [github/jonschlinkert](https://github.com/jonschlinkert)
+ [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
## License
Copyright (c) 2015 Jon Schlinkert
Released under the MIT license.
***
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on May 14, 2015._