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
expand('a')
expand('a.b')
expand('a|b')
expand('a:b')
expand('a,b')
Install
Install with npm
npm i expand-object --save
Usage
var expand = require('expand-object');
dots
Expand dots into child objects:
expand('a')
expand('a.b')
expand('a.b.c')
expand('a.b.c.d')
pipes
Expand pipes into sibling objects:
expand('a|b')
expand('a|b|c')
expand('a|b|c|d')
colons
Expand colons into key-value pairs:
expand('a:b')
expand('a.b:c')
expand('a.b.c:d')
commas
Expand comma separated values into arrays:
expand('a,b')
expand('a,b,c')
expand('a:b,c,d|e:f,g,h')
Usage examples
Expand siblings with comma separated values into arrays:
expand('a:b,c,d|e:f,g,h')
Expand children with comma separated values into arrays:
expand('a.b.c:d,e,f|g.h:i,j,k')
Expand sibling objects into key-value pairs:
expand('a:b|c:d')
expand('a:b|c:d|e:f')
expand('a:b|c:d|e:f|g:h')
Expand child objects into key-value pairs:
expand('a.b:c')
expand('a.b.c:d')
expand('a.b.c.d:e')
Expand sibling and child objects into key-value pairs:
expand('a:b|c:d')
expand('a.b.c|d.e:f')
expand('a.b:c|d.e:f')
expand('a.b.c:d|e.f.g:h')
Related projects
- get-value: Use property paths (
a.b.c
) get a nested value from an object. - has-value: Returns true if a value exists, false if empty. Works with deeply nested values using… more
- stringify-keys: Build an array of key paths from an object.
- set-value: Create nested values and any intermediaries using dot notation (
'a.b.c'
) paths.
Running tests
Install dev dependencies:
npm i -d && npm test
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue
Author
Jon Schlinkert
License
Copyright (c) 2015 Jon Schlinkert
Released under the MIT license.
This file was generated by verb-cli on May 12, 2015.