npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fl4t

v0.0.6

Published

Take a nested Javascript object and flat it

Downloads

181

Readme

fl4t

Take a nested Javascript object and flat it.

Installation

$ npm install fl4t

Methods

flat: (target: Object, options: Object) => Object

Flat the object - it'll return an object one level deep, regardless of how nested the original object was:

import flat from 'fl4t'

flat({
  lorem: {
    ipsum: 'dolor'
  },
  sit: {
    amet: 'consectetur'
  },
  adipiscing: { elit: { sed: { do: [ 1, 2, 3, { eiusmod: 'tempor' } ] } } }
})

//  {
//    'lorem.ipsum': 'dolor',
//    'sit.amet': 'consectetur',
//    'adipiscing.elit.sed.do.0': 1,
//    'adipiscing.elit.sed.do.1': 2,
//    'adipiscing.elit.sed.do.2': 3,
//    'adipiscing.elit.sed.do.3.tempor': 'tempor'
//  }

Options

delimiter: string

Use a custom delimiter for flating your objects, instead of ..

maxDepth: number

Maximum number of nested objects to flat.

import flat from 'fl4t'

flat({
  lorem: {
    ipsum: 'dolor'
  },
  sit: {
    amet: 'consectetur'
  },
  adipiscing: { elit: { sed: { do: { eiusmod: 'tempor' } } } }
}, { maxDepth: 2 })

//  {
//    'lorem.ipsum': 'dolor',
//    'sit.amet': 'consectetur',
//    'adipiscing.elit': {
//      sed: {
//        do: {
//          eiusmod: 'tempor'
//        }
//      }
//    }
//  }

transformKey: (prefix: string, key: string) => string

In additional to use a custom delimiter you may use transformKey function for more flexibility. By default transformKey function defined like:

const transformKey = (prevKey, key, delimiter = '.') => `${prevKey}${delimiter}${key}`

For some cases you maybe want to get flat object like {'hello[world][great][again]': 'hi there'}. transformKey function will look like:

import flat from 'fl4t'

flat({
  hello: {
    world: {
      great: {
        again: 'hi there'
      }
    }
  }
}, {
  transformKey: (prevKey, key) => `${prefix}[${key}]`
})

//  {
//    'hello[world][great][again]': 'hi there'
//  }

shouldTraverse: (value: any, depth: ?number) => boolean

shouldTraverse function control which value will be flat. Function takes value and current depth. If it returns true then next step of recursion will call.

By default shouldTraverse defined as:

const shouldTraverse = (value, depth) => {
  const type = Object.prototype.toString.call(value)
  return (type === '[object Object]' || type === '[object Array]') && (opts.depth ? opts.depth > depth : true)
}

If you want preserve arrays you should use the following function:

const shouldTraverse = (value) => {
  const type = Object.prototype.toString.call(value)
  return type === '[object Object]'
}

If you want preserve instance of specific class you may use something like this:

import flat from 'fl4t'

function Cat (name) {
  this.name = name
}

const maru = new Cat('maru')

flat({
  hello: {
    world: {
      great: {
        again: 'hi there'
      }
    },
    cat: maru
  }
}, {
  shouldTraverse: (value) =>
    Object.prototype.toString.call(value) === '[object Object]' &&
    !(value instanceof Cat)
})

// {
//   'hello.world.great.again': 'hi there',
//   'hello.cat': {
//     name: 'maru'
//   }
// }