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

deep-cleaner

v2.0.0-alpha.3

Published

Recursively remove deeply nested properties and/or falsey values (excluding 0), empty objects, and empty arrays.

Downloads

11,878

Readme

Deep Cleaner

npm version Build Status npm DUB

Delete nested key-value pairs by a specified key or remove empty objects, empty strings, null, and undefined values from an object.

This module was created primarily with JSON in mind. The module is light-weight and dependency free.

Installation

npm install --save deep-cleaner

Usage:

To remove empty values on an object:

> var cleaner = require('deep-cleaner');
> var obj = {
...   a: 'value',
...   emptyString: '',
...   emptyArray: [],
...   emptyObject: {},
...   isNull: null,
...   isUndefined: undefined
...   b: {
...     a: 'nother value',
...     anotherEmptyString: '',
...     arr: [
...       {c: null},
...       {d: 'value'}
...     ]
...   }
... }
> cleaner(obj);
> console.log(obj);
{ aValue: 'value', b: { a: 'nother value', arr: [ {}, {d: 'value'} ] } }

To remove a key-value pair, pass in the name of the key as the second parameter:

> var dirtyObject = {
...   a: {
...     b: {
...       dirty: "value"
...     }
...   },
...   dirty: "value",
...   some_list: [
...     {
...       dirty: "value",
...       clean: "value"
...     },
...     {
...       dirty: "value",
...       clean: "value"
...     },
...     {
...       dirty: "value",
...       clean: "value"
...     },
...   ]
... }
> cleaner(dirtyObject, 'dirty');
> console.log(dirtyObject);
{ a: { b: {} }, some_list: [ {
  clean: "value" }, { clean: "value" }, { clean: "value" } ] }

You can also pass in an array of keys.

> var obj = {
...   a: {
...     a: 'foo',
...     b: 'bar',
...     c: 'baz'
...   },
...   b: 'quux',
...   c: 'plugh',
...   d: 'grault'
... }
> cleaner(obj, ['b','c']);
> console.log(obj);
{ a: { a: 'foo' }, d: 'grault' }

As of v1.2.0, deep-cleaner also works on objects with circular references!

> var foo = { bar: null, baz: '' };
> foo.bar = foo;
> cleaner(foo);
> console.log(foo);
{ bar: [Circular] }

API

deepCleaner(obj[, key])

Removes "empty" values from obj. A Value is considered "empty" if it is:

  • an empty string
  • an empty array
  • an empty object
  • null
  • undefined

Arguments

obj

Object - The object to be cleaned.

key

String?|Array? - A key or array of keys to be removed from obj. If key is not specified, then deepCleaner will default to removing empty values, otherwise, only obj[key] will be removed (or obj[key[0]],..., obj[key[n]] if key is an array of keys).

Contributing to Deep Cleaner

Pull requests are welcome! If you notice a bug or want to add a feature, please feel free to contribute to this project.

Here are a few guidelines you should follow.

  • Please make new pull requests into the development branch. A new contribution might prompt me to make additional changes that I'd like packaged together in a single PR to master.
  • Please write as much detail as necessary in your commit messages to clearly explain any changes.
  • Changes to the code that do not have passing unit tests will not be accepted.

Notice a bug but don't have time to fix it? Submit an issue! I strive to keep this package well maintained.

Testing

run npm test.

License

MIT