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

simple-object-flatten

v0.0.1

Published

Simple to use, fast object flattener

Downloads

4

Readme

Simple Object Flatten

Simple and configurable object flattener

Contents

Install

To install just use npm or yarn:

npm install --save simple-object-flatten
yarn add simple-object-flatten

Usage

In order to use the lib, just import it and use it as a function. The sintax is flatten(object, options). For more information about the options, please check the Options section.

const flatten = require("simple-object-flatten");

//-- output: { "foo.bar": "bar", "foo.arr": [ 1, 2, 3 ] }
let flattened = flatten({ foo: { bar: "bar", arr: [ 1, 2, 3 ] } });

Options

Although there are several good object flatteners out there, I decided to create my own because no one was actually fulfilling all my needs. Therefore, this library comes with the following flattening options:

Separator

You can specify your own separator when flattening your objects. The default value is ..

//-- output: { "foo/bar": "bar", "foo/arr": [ 1, 2, 3 ] }
let flattened = flatten({ foo: { bar: "bar", arr: [ 1, 2, 3 ] } }, { separator: "/" });

Array as object

By default, the flattener will only flatten the keys of regular objects. However, if you want to treat arrays as objects and flatten its keys, you can set the arrayAsObject: true. The default value is false.

//-- output: { "foo.bar": "bar", "foo.arr.0": 1, "foo.arr.1": 2, "foo.arr.2": 3 }
let flattened = flatten({ foo: { bar: "bar", arr: [ 1, 2, 3 ] } }, { arrayAsObject: true });

String as object

By default, the flattener will only flatten the keys of regular objects. However, if you want to treat strings as objects and flatten its keys, you can set the stringAsObject: true. The default value is false.

//-- output: { "foo.bar.0": "b", "foo.bar.1": "a", "foo.bar.2": "r", "foo.arr": [ 1, 2, 3 ] }
let flattened = flatten({ foo: { bar: "bar", arr: [ 1, 2, 3 ] } }, { stringAsObject: true });

Filter out keys

If for any reason you need to suppress some keys from the resulting object, you can do this using the filterOut option. This parameter can be either a string or an array of keys. The default value for this option is undefined which means that all the keys from the input are going to appear in the output.

It is important to highlight that the filtering only happens on areas of the object that will be flattened. So if you have an object inside an array with a key to be filtered out, you need to use arrayAsObject in order for the filtering to take place in that array. Check out the example below:

//-- output: { "foo.bar": "bar", "foo.arr": [ 1, 2, 3, { unwanted_key: 0 } ] }
let f1 = flatten({ foo: { bar: "bar", arr: [ 1, 2, 3, { unwanted_key: 0 } ], unwanted_key: {} }, unwanted_key: "hi" }, { filterOut: "unwanted_key" });

//-- output: { "foo.bar": "bar", "foo.arr.0": 1, "foo.arr.1": 2, "foo.arr.2": 3, "foo.arr.3": {} }
let f2 = flatten({ foo: { bar: "bar", arr: [ 1, 2, 3, { unwanted_key: 0 } ], unwanted_key: {} }, unwanted_key: "hi" }, { filterOut: "unwanted_key", arrayAsObject: true });

License

MIT