What is clean-deep?
The clean-deep npm package is used to deeply clean JavaScript objects by removing properties with specific values such as null, undefined, empty strings, empty arrays, and empty objects. This is useful for sanitizing data before processing or sending it over a network.
What are clean-deep's main functionalities?
Remove null and undefined values
This feature removes properties with null and undefined values from the object.
const cleanDeep = require('clean-deep');
const obj = { a: null, b: undefined, c: 'value' };
const cleanedObj = cleanDeep(obj);
console.log(cleanedObj); // Output: { c: 'value' }
Remove empty strings
This feature removes properties with empty string values from the object.
const cleanDeep = require('clean-deep');
const obj = { a: '', b: 'value' };
const cleanedObj = cleanDeep(obj);
console.log(cleanedObj); // Output: { b: 'value' }
Remove empty arrays
This feature removes properties with empty array values from the object.
const cleanDeep = require('clean-deep');
const obj = { a: [], b: [1, 2, 3] };
const cleanedObj = cleanDeep(obj);
console.log(cleanedObj); // Output: { b: [1, 2, 3] }
Remove empty objects
This feature removes properties with empty object values from the object.
const cleanDeep = require('clean-deep');
const obj = { a: {}, b: { c: 'value' } };
const cleanedObj = cleanDeep(obj);
console.log(cleanedObj); // Output: { b: { c: 'value' } }
Custom values to remove
This feature allows you to specify custom values to be removed from the object.
const cleanDeep = require('clean-deep');
const obj = { a: 'remove me', b: 'keep me' };
const cleanedObj = cleanDeep(obj, { cleanValues: ['remove me'] });
console.log(cleanedObj); // Output: { b: 'keep me' }
Other packages similar to clean-deep
omit-deep
The omit-deep package is used to deeply omit specified properties from an object. Unlike clean-deep, which removes properties based on their values, omit-deep removes properties based on their keys.
lodash
Lodash is a utility library that provides a wide range of functions for manipulating objects, arrays, and other data structures. It includes functions like _.omit and _.omitBy, which can be used to remove properties from objects, but it is more general-purpose compared to clean-deep.
deep-clean
The deep-clean package is similar to clean-deep in that it removes properties with specific values from objects. However, it may have different default behaviors and options for customization.
clean-deep
Removes empty objects, arrays, empty strings, NaN, null and undefined values from objects. Does not alter the original object.
As of version 3.0.0, clean-deep traverses arrays as well as objects.
Status
Installation
Install the package via npm
:
$ npm install clean-deep --save
Usage
Arguments
object
(Object): The source object.[options]
(Object): An optional object with the following options:
Option | Default value | Description |
---|
cleanKeys | [] | Remove specific keys, ie: ['foo', 'bar', ' '] |
cleanValues | [] | Remove specific values, ie: ['foo', 'bar', ' '] |
emptyArrays | true | Remove empty arrays, ie: [] |
emptyObjects | true | Remove empty objects, ie: {} |
emptyStrings | true | Remove empty strings, ie: '' |
NaNValues | false | Remove NaN values, ie: NaN |
nullValues | true | Remove null values, ie: null |
undefinedValues | true | Remove undefined values, ie: undefined |
(Object): Returns the cleansed object.
Example
const cleanDeep = require('clean-deep');
const object = {
bar: {},
baz: null,
biz: 'baz',
foo: '',
net: [],
nit: undefined,
qux: {
baz: 'boz',
txi: ''
}
};
cleanDeep(object);
Tests
$ npm test
Release
npm version [<newversion> | major | minor | patch] -m "Release %s"
License
MIT