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-squish

v1.1.0

Published

Flatten nested objects to a single level

Downloads

881

Readme

Object-Squish

A node.js utility for flattening objects to a single level.

Getting Started

Install object-squish via npm:

npm install object-squish --save

Then require in your file

var squish = require('object-squish');

Usage

Use object-squish to collapse pesky nested objects down to one level.


var data = {
  spiders: { are: 'ok' },
  they: {
    just: {
      want: 'to',
      be: 'friends'
    }
  }
};

var flattened = squish(data);

// === Outputs ->
{
  'spiders.are': 'ok',
  'they.just.want': 'to',
  'they.just.be': 'friends'
}

arrays are skipped over by default but can be included by passing the includeArrays option.


var data = { spiders: [1, 2, 3] };

var flattened = squish(data, { includeArrays: true });
// === Outputs ->
{
    'spiders.0': 1,
    'spiders.1': 2,
    'spiders.2': 3,
}

See below for a comprehensive list of Options

Options

options.depth

Type: Number Default value: Infinity

Set the depth at which to stop flattening.

options.includeArrays

Type: Boolean Default value: false

Set whether to flatten arrays, keys will appear as array indexes 0, 1, 2, ....

options.seperator

Type: String Default value: '.'

Set the seperating character/sequence in the derived path

options.modifyKey

Change the keys of the object, this can be one of:

'lowercase' -> ensure all keys are lowercase

'uppercase' -> ensure all keys are uppercase

Function -> a function which is passed the key and returns the modified key

options.stopWhen

A predicate, called with the object being processed, returning a boolean indicating whether to descend into the object or not.

Usage Examples

Example using modifyKey

var data = {
  spiders: { are: 'ok' },
  they: {
    just: {
      want: 'to',
      be: 'friends'
    }
  }
};

// we can use modifyKey to make all the keys uppercase
var uppercased = squish(data, { modifyKey: 'uppercase' });
// === Outputs ->
{
  'SPIDERS.ARE': 'ok',
  'THEY.JUST.WANT': 'to',
  'THEY.JUST.BE': 'friends'
}


// or use a function to perform more complex processing
var mutate = function (key) {
    return key.replace('are', 'arent');
};

var modified = squish(data, { modifyKey: mutate });
// === Outputs ->
{
    'spiders.arent': 'ok',
    'they.just.want': 'to',
    'they.just.be': 'friends'
}

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality.

Release History

  • v 1.0.0 - Initial Release