tiny-clean-object
TypeScript icon, indicating that this package has built-in type declarations

1.4.0 • Public • Published

tiny-clean-object

NPM version NPM monthly download

A tiny and fast utility to clean deep object

Installation

npm install tiny-clean-object

Example:

import {cleanObject} from 'tiny-clean-object';

const object = {
  a: 'ok',
  b: 123,
  c: null,
  d: undefined,
  e: NaN,
  f: Infinity,
  g: {
    deep: 'ok',
    deep2: {
      obj: {},
      arr: [],
      empty: '',
    },
    deep3: null,
    deep4: NaN,
  },
  h: [],
  i: {},
  k: '',
};

cleanObject(object, {deep: true, emptyObjects: true});

Result

{
  "a": "ok",
  "b": 123,
  "g": {
    "deep": "ok",
    "deep2": {
      "arr": []
    }
  },
  "h": []
}

Options

Option Default value Description
deep false Recursive. Clean deep object
skipNull false Skip keys with null as the value
emptyStrings true Remove empty strings, ie: ''
emptyInvalidNumbers true Remove NaN and Infinity
emptyArrays false Remove empty arrays, ie: []
emptyObjects false Remove empty objects, ie: {}

License

MIT

Package Sidebar

Install

npm i tiny-clean-object

Weekly Downloads

2,993

Version

1.4.0

License

MIT

Unpacked Size

13.2 kB

Total Files

7

Last publish

Collaborators

  • nghiepit