unbend
Unbend Flatten tree into an object with { '/complete/path/to/each': value }
E.g. this useful for parametric http routing
Install
npm install --save unbend
Usage
unbend( Object, [ config ] )
const unbend = const source = route: inner: {} str : 'string' list: 'some data' {} def: null
And that will turn into
const expected = {} '/route/inner/str' : 'string' '/route/list' : 'some data' {} '/def' : null
See test data file for complete examples
Configuration
Unbend supports optional config object at second argument.
Default config
const config = separator : '/' skipFirstSeparator: false parseArray : false
Custom separators
unbend(tree, { separator: '.' })
const source = route: inner: {} str : 'string' def: null //And that will turn into const expected = {} '.route.inner.str' : 'string' '.def' : null
Convert nested arrays
unbend(tree, { parseArray: true })
const unbend = const source = list: 'any' null 'field': 'inside array' //And that will turn into const expected = '/list/0' : 'any' '/list/2' : null '/list/3/field': 'inside array'
Skip first separator
unbend(tree, { skipFirstSeparator: true })
const source = route: inner: {} str : 'string' def: null //And that will turn into const expected = {} 'route/inner/str' : 'string' 'def' : null
License
MIT © Zero Bias