Get unlimited public & private packages + team-based management with npm Teams.Learn more »

fast-clean

1.1.0 • Public • Published

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

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',
  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]],
   }
  }

Options

Options is an object that allows you to choose what filters you want to add to the module. Currently we have the nullCleaner only. We will proceed to add different filters step by step carefully because we want to keep what is unique about our module.

  • nullCleaner : remove null values (defaults to false) In the previous example, if we set the nullCleaner to true in the options. This would be the output.
{
  a: 'value',
 falseValue: false,
 zero: 0,
 b: {
   a: 'another value',
   arr: [
     { d: 'value' },
     { a: [
       {
         x: true,
       },
       {
         z: [true],
         subChild: [
           {
             a: true
           }
         ]
       }
     ]}
   ],

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

Values cleaned by default are

  • undefined
  • '' (empty strings)
  • NaN
  • {} (empty objects)

What makes this module unique

  • It's an extremely lightweight library
  • Absolutely no dependencies
  • Extremely fast compared to other modules with the same functionalities. enter image description here You can check how the comparison was made here

Install

npm i fast-clean

DownloadsWeekly Downloads

7

Version

1.1.0

License

ISC

Unpacked Size

9.39 kB

Total Files

10

Last publish

Collaborators

  • avatar