GithubHelp home page GithubHelp logo

araujoigor / simple-object-flatten Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 1.0 4 KB

Simple, fast object flattener library

License: MIT License

JavaScript 100.00%
object-flatten flatten-object flattener flatten object js javascript nodejs

simple-object-flatten's Introduction

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

simple-object-flatten's People

Contributors

araujoigor avatar

Watchers

 avatar  avatar

Forkers

pm-hwks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.