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

fast-clean

v1.3.2

Published

LightWeight, dependency free, javascript object cleaning library

Downloads

1,269

Readme

Fast-Cleaner

Fast cleaner is an npm module designed to clean javascript objects from unwanted values like undefined, NaN, {}(empty objects) .. etc.

(now supports typescript)

What makes this module different. Check out our comparison

Update with version 1.3+

Version 1.3+ now supports cleaning objects in place. This means that the library can avoid creating new objects while cleaning. Instead it can mutate the object passed to it & clean it in place.

This will make the module not only efficient in speed, but in memory consumption as well.

While object mutation might not be a best practice in a lot of cases, in some it will be a better one if you are cleaning large objects & want to avoid huge memory consumption.

This is an OPTIONAL feature. You can simply use it by passing cleanInPlace = true in the options.

The feature is turned off by default for backwards compatibility.

All other options are supported whether you are cleaning in place or not.

Content

Installation


npm i --save fast-clean

Usage


const cleanedObj = cleaner.clean(objectToClean, options);

Example

const obj = {
  a: 'value',
  emptyString: '',
  emptyArray: [],
  emptyObject: {},
  isNull: null,
  falseValue: false,
  zero: 0,
  isUndefined: undefined,
  b: {
    a: 'another value',
    anotherEmptyString: '',
    arr: [
      { c: null },
      { d: 'value' },
      { a: [
        {
          x: true,
          y: NaN
        },
        {
          y: NaN
        },
        {
          z: [null, true],
          subChild: [
            {
              a: true
            },
            {
              
            }
          ]
        }
      ]}
    ],

    secondArr: [{
      a: {
        b: undefined
      }
    }],

    nestedArr1: [[null, true, false], [undefined, undefined]],
    nestedArr2: [[null], [undefined, undefined]],
  }
}

const cleanedObj = cleaner.clean(obj);

Output is

{
  a: 'value',
  falseValue: false,
  zero: 0,
  b: {
    a: 'another value',
    arr: [
      { d: 'value' },
      { a: [
        {
          x: true,
        },
        {
          z: [true],
          subChild: [
            {
              a: true
            }
          ]
        }
      ]}
    ],

    nestedArr1: [[true, false]],
  }
}

Options

Options is an object that allows you to choose what filters you want to add to the module.

  • nullCleaner : remove null values (defaults to false)
  • emptyArraysCleaner : removes empty arrays (defaults to true)
  • emptyObjectsCleaner : removes empty objects (defaults to true)
  • emptyStringsCleaner : removes empty strings (defaults to true)
  • nanCleaner : removes NaN (defaults to true)
  • cleanInPlace : whether the library should create a new object that is cleaned or mutate the object passed to it & clean in place. (defaults to false)

Additional Examples

Based on the mentioned sample object above, here's the output with different options

With nullCleaner = true

{
  a: 'value',
  isNull: null,
  falseValue: false,
  zero: 0,
  b: {
    a: 'another value',
    arr: [
      { c: null },
      { d: 'value' },
      { a: [
        {
          x: true,
        },
        {
          z: [null, true],
          subChild: [
            {
              a: true
            }
          ]
        }
      ]}
    ],

    nestedArr1: [[null, true, false]],
    nestedArr2: [[null]],
  }
}

With nullCleaner = true & emptyArrayCleaner = false

{
  a: 'value',
  emptyArray: [],
  falseValue: false,
  zero: 0,
  b: {
    a: 'another value',
    arr: [
      { d: 'value' },
      {
        a: [
          {
            x: true
          },
          {
            z: [true],
            subChild: [
              {
                a: true
              }
            ]
          }
        ]
      }
    ],

    secondArr: [],
    nestedArr1: [[true, false], []],
    nestedArr2: [[], []]
  }
}

With nullCleaner = true & emptyObjectsCleaner = false

{
  a: 'value',
  emptyObject: {},
  falseValue: false,
  zero: 0,
  b: {
    a: 'another value',
    arr: [
      {},
      { d: 'value' },
      {
        a: [
          {
            x: true,
          },
          {
          },
          {
            z: [true],
            subChild: [
              {
                a: true
              },
              {
              }
            ]
          }
        ]
      }
    ],

    secondArr: [{
      a: {
      }
    }],

    nestedArr1: [[true, false]]
  }
}

With nullCleaner = true & nanCleaner = false

{
  a: 'value',
  falseValue: false,
  zero: 0,
  b: {
    a: 'another value',
    arr: [
      { d: 'value' },
      { a: [
        {
          x: true,
          y: NaN
        },
        {
          y: NaN
        },
        {
          z: [true],
          subChild: [
            {
              a: true
            }
          ]
        }
      ]}
    ],

    nestedArr1: [[true, false]]
  }
}

With nullCleaner, emptyObjectsCleaner, emptyArrayCleaner & emptyStringsCleaner all equal false

{
  a: 'value',
  emptyString: '',
  emptyArray: [],
  emptyObject: {},
  isNull: null,
  falseValue: false,
  zero: 0,
  b: {
    a: 'another value',
    anotherEmptyString: '',
    arr: [
      { c: null },
      { d: 'value' },
      {
        a: [
          {
            x: true
          },
          {
          },
          {
            z: [null, true],
            subChild: [
              {
                a: true
              },
              {

              }
            ]
          }
        ]
      }
    ],

    secondArr: [{
      a: {
      }
    }],

    nestedArr1: [[null, true, false], []],
    nestedArr2: [[null], []],
  }
}

Values cleaned by default are

  • undefined

  • '' (empty strings)

  • NaN

  • {} (empty objects)

  • [] (empty arrays)

What makes this module unique

  • It's an extremely lightweight library.

  • Absolutely no dependencies.

  • Extremely fast compared to other modules with the same functionalities.

  • The ability to clean objects in place without creating new objects in memory.

enter image description here

You can check how the comparison was made here