Coder Social home page Coder Social logo

object-fx's Introduction

Object FX

Flatten and eXpand for Javascript Objects.

Build Status Test Coverage Code Climate

Dependency Status devDependencies Status

Standard - JavaScript Style Guide

Installation

$ npm install object-fx

Usage

Basic example:

const objectFx = require('object-fx')

let nestedObject = {
    hello: {
        world: [1, 2, 3, '!']
    }
}

let flattened = objectFx.flatten(nestedObject)
console.log(flattened)
/*
{
    'hello.world.0': 1,
    'hello.world.1': 2,
    'hello.world.2': 3,
    'hello.world.3': '!',
}
*/

let expanded = objectFx.expand(flattened)
console.log(expanded)
/*
{
    hello: {
        world: [1, 2, 3, '!']
    }
}
*/

Common Use Cases

  • Directly utilize redis hashes: Store, manipulate and retrieve Javascript object data easily
  • Simplify updates of e.g. MongoDB documents: Don't lose unset properties by using dot notation
  • For convenience and "because of the clarity" (e.g. data conversion, debug output)
  • ...add your own here!

API Methods

flatten(obj, opt)

Flattens an object.

Options:

CircularityCheck
{Boolean}, defaults to false
Perform a check for circular references before flattening an object.
Without prior testing circular objects will throw RangeError: Maximum call stack size exceeded.

CustomDelimiter
{String}, defaults to '.' (dot)
You can use any char or character chain, but avoid those that are already used within keys.

ExplicitArrays
{Boolean}, defaults to false
If set to true arrays are flattened in bracket notation, e.g. to arr[0] instead of arr.0

MaxDepth
{Number}, defaults to 0
Maximum number of (nested) levels to flatten.

Example:

const objectFx = require('object-fx')

const nestedObject = {
  lets: {
    count: [1, 2, 3, '...']
  },
  Is: {
    this: {
      deeply: {
        nested: {
          '?': 'YEES!!!'
        }
      }
    }
  }
}

let flattened = objectFx.flatten(nestedObject, { ExplicitArrays: true, MaxDepth: 3 })
console.log(flattened)
/*
{
  'lets.count[0]': 1,
  'lets.count[1]': 2,
  'lets.count[2]': 3,
  'lets.count[3]': '...',
  'Is.this.deeply': {
    nested: {
      '?': 'YEES!!!'
    }
  }
}
*/

expand(obj, opt)

Expands an object.

Options:

AutocreateArrays
{Boolean}, defaults to true
Per default, keys consisting of whole numbers are expanded to array indices, e.g. { a.0: 'value'} โ‡’ { a: [ 'value' ] }
Set this option to false to create object keys instead: { a.0: 'value'} โ‡’ { a: { 0: 'value' }

CustomDelimiter
{String}, defaults to '.' (dot)
You can use any char or character chain, but avoid those that are already used within keys.

ExplicitArrays
{Boolean}, defaults to false
If set to true, bracket notations like arr[0] will be expanded into arrays, otherwise ignored.

Example:

const objectFx = require('object-fx')

let flatObject = {
  'lets.count[0]': 1,
  'lets.count[1]': 2,
  'lets.count[2]': 3,
  'lets.count[3]': '...',
  'Is.this.deeply.nested.?': 'YEES!!!'
}

let expanded = objectFx.expand(flatObject, { ExplicitArrays: true })
console.log(expanded)
/*
{
  lets: {
    count: [1, 2, 3, '...']
  },
  Is: {
    this: {
      deeply: {
        nested: {
          '?': 'YEES!!!'
        }
      }
    }
  }
}
*/

unflatten(obj, opt)

Expands an object (alternate method name).
Uses expand under the hood.

License

ISC

object-fx's People

Contributors

nodexo avatar

Watchers

James Cloos avatar VaciDesign avatar

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.