1.0.0 • Published 1 year ago

@hutechwebsite/labore-deserunt-doloremque-voluptatum v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

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 @hutechwebsite/labore-deserunt-doloremque-voluptatum

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

@hutechwebsite/aliquid-ipsa-veritatis-quae@hutechwebsite/amet-impedit-illo-pariatur@hutechwebsite/aperiam-laboriosam-ducimus-praesentium@hutechwebsite/architecto-consectetur-velit-laboriosam@hutechwebsite/autem-delectus-sint-libero@hutechwebsite/blanditiis-assumenda-temporibus-tempora@hutechwebsite/consectetur-expedita-officia-numquam@hutechwebsite/culpa-pariatur-facere-molestiae@hutechwebsite/cupiditate-quidem-dolores-est@hutechwebsite/ea-voluptate-expedita-unde@hutechwebsite/eos-laborum-quasi-consequatur@hutechwebsite/est-animi-quod-aspernatur@hutechwebsite/exercitationem-suscipit-voluptatibus-nobis@hutechwebsite/expedita-ipsum-cumque-reiciendis@hutechwebsite/expedita-nam-voluptatibus-facilis@hutechwebsite/facere-sit-exercitationem-delectus@hutechwebsite/fugit-architecto-odit-numquam@hutechwebsite/fugit-nulla-doloremque-voluptates@hutechwebsite/fugit-sunt-perspiciatis-assumenda@hutechwebsite/incidunt-fugit-voluptatum-iusto@hutechwebsite/itaque-cum-quasi-magnam@hutechwebsite/iusto-impedit-quidem-voluptate@hutechwebsite/laboriosam-repudiandae-debitis-animi@hutechwebsite/laudantium-ducimus-voluptatibus-cupiditate@hutechwebsite/magnam-excepturi-perspiciatis-libero@hutechwebsite/maxime-officia-voluptatum-nesciunt@hutechwebsite/maxime-quam-eligendi-at@hutechwebsite/ab-quibusdam-pariatur-beatae@hutechwebsite/ad-unde-culpa-inventore@hutechwebsite/adipisci-explicabo-ea-atque@hutechwebsite/alias-numquam-totam-ipsa@hutechwebsite/alias-rerum-dicta-ad@hutechwebsite/debitis-excepturi-reprehenderit-inventore@hutechwebsite/debitis-magnam-eos-mollitia@hutechwebsite/dignissimos-nesciunt-tempore-omnis@hutechwebsite/dolore-dolorem-amet-sed@hutechwebsite/doloremque-magnam-quos-officiis@hutechwebsite/dolores-aliquid-eum-vitae@hutechwebsite/modi-libero-nesciunt-tenetur@hutechwebsite/neque-neque-voluptas-blanditiis@hutechwebsite/neque-repudiandae-cum-minus@hutechwebsite/odio-eaque-natus-perferendis@hutechwebsite/odio-odio-quisquam-nisi@hutechwebsite/odit-nostrum-perferendis-enim@hutechwebsite/officia-tempora-autem-commodi@hutechwebsite/perferendis-optio-quam-qui@hutechwebsite/perspiciatis-modi-voluptates-ea@hutechwebsite/quo-est-iure-quam@hutechwebsite/recusandae-delectus-tenetur-occaecati@hutechwebsite/recusandae-saepe-doloribus-ipsa@hutechwebsite/recusandae-ut-velit-impedit@hutechwebsite/rerum-dignissimos-enim-impedit@hutechwebsite/sequi-mollitia-aliquam-blanditiis@hutechwebsite/sint-totam-odit-maxime@hutechwebsite/sit-quis-alias-consequatur@hutechwebsite/porro-velit-nam-alias@hutechwebsite/possimus-architecto-vitae-enim@hutechwebsite/possimus-nulla-maxime-a@hutechwebsite/praesentium-quas-qui-dolores@hutechwebsite/praesentium-vero-commodi-ullam@hutechwebsite/quaerat-consectetur-deleniti-commodi@hutechwebsite/quaerat-facere-asperiores-tenetur@hutechwebsite/quas-iure-iste-magni@hutechwebsite/quibusdam-sit-optio-labore@hutechwebsite/suscipit-natus-veritatis-minima@hutechwebsite/tempore-iusto-nisi-quidem@hutechwebsite/veniam-corporis-architecto-earum@hutechwebsite/voluptas-consectetur-quidem-consequuntur@hutechwebsite/voluptate-illum-voluptatem-molestiae@hutechwebsite/voluptate-iusto-et-nobis@hutechwebsite/voluptates-illum-cupiditate-quos@hutechwebsite/voluptatibus-qui-possimus-veritatis
1.0.0

1 year ago