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

object-fx

v1.1.0

Published

Flatten and eXpand for Javascript Objects.

Downloads

445

Readme

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