Git Product home page Git Product logo

flat-obj's Introduction

flat-obj build status codecov

A tiny (173B) utility to flatten an object with customizable glue

This module squashes a nested object (including its internal Arrays) so that the output is a flat object – AKA, it has a single level of depth. By default, the _ character is used to glue/join layers' keys together. This is customizable, of course.

Finally, any keys with nullish values (null and undefined) are not included in the return object.

Install

$ npm install --save flat-obj

Usage

import flatObj from 'flat-obj';

flatObj({
  a: 'hi',
  b: {
    a: null,
    b: '',
    d: 'hello',
    e: {
      a: 'yo',
      b: undefined,
      c: 'sup',
      d: 0
    }
  },
  c: 'world'
});
//=> { a:'hi', b_b:'', b_d:'hello', b_e_a:'yo', b_e_c:'sup', b_e_d:0, c:'world' }

Note: null and undefined values are purged.

API

flatObj(input, [glue])

Returns: Object

Returns a new object with a single level of depth.

input

Type: Object

The object to flatten.

glue

Type: String
Default: _

A string used to join parent key names to nested child key names.

const foo = { bar: 123 };

flatObj({ foo }); //=> { foo_bar: 123 }
flatObj({ foo }, '.'); //=> { 'foo.bar': 123 }

Benchmarks

Running on Node.js v10.13.0

Validation:
  ✔ flat
  ✔ flatten-object
  ✔ [email protected]
  ✔ flat-obj

Benchmark:
  flat               x 187,778 ops/sec ±1.27% (87 runs sampled)
  flatten-object     x 191,514 ops/sec ±0.26% (93 runs sampled)
  [email protected]       x 268,060 ops/sec ±1.21% (94 runs sampled)
  flat-obj           x 622,744 ops/sec ±0.33% (92 runs sampled)

License

MIT © Luke Edwards

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.