unbend

2.1.2 • Public • Published

unbend

NPM version Unix Build Status Windows Build Status Coveralls Status Dependency Status

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 = require('unbend')
 
const source = {
  route: {
    inner: {
      func: () => {},
      str : 'string'
    },
    list: [ 'some data', {} ]
  },
  def: null
}
 
unbend( source )

And that will turn into

const expected = {
  '/route/inner/func': () => {},
  '/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: {
      func: () => {},
      str : 'string'
    }
  },
  def: null
}
 
unbend( source, { separator: '.' } )
 
//And that will turn into
 
const expected = {
  '.route.inner.func': () => {},
  '.route.inner.str' : 'string',
  '.def'             : null
}

Convert nested arrays

unbend(tree, { parseArray: true })

const unbend = require('unbend')
 
const source = {
    list: [
        'any',
        { },
        null,
        { 'field': 'inside array' }
    ]
}
 
unbend( source, { parseArray: true } )
 
//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: {
      func: () => {},
      str : 'string'
    }
  },
  def: null
}
 
unbend( source, { skipFirstSeparator: true } )
 
//And that will turn into
 
const expected = {
  'route/inner/func': () => {},
  'route/inner/str' : 'string',
  'def'             : null
}

License

MIT © Zero Bias

Package Sidebar

Install

npm i unbend

Weekly Downloads

0

Version

2.1.2

License

MIT

Last publish

Collaborators

  • drelliot