# deep-cleaner

> Recursively remove deeply nested properties and/or falsey values (excluding 0), empty objects, and empty arrays.

Latest version **2.0.0-alpha.3** (published 2022-08-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install deep-cleaner
pnpm add deep-cleaner
yarn add deep-cleaner
bun add deep-cleaner
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0-alpha.3 |
| Published | 2022-08-26 |
| First published | 2017-03-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 41.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 15 |
| Author | Craig Blackburn |
| Maintainers | darksinge |
| Keywords | clean, strip, object, string, array, null, undefined, empty, recursive, recursively, delete, deep, deep-clean, cleaner, deep-cleaner |

## Links

- npm: https://www.npmjs.com/package/deep-cleaner
- Repository: https://github.com/darksinge/deep-cleaner
- Homepage: https://github.com/darksinge/deep-cleaner#readme
- Issues: https://github.com/darksinge/deep-cleaner/issues
- npm.io page: https://npm.io/package/deep-cleaner

## Dependencies (2)

- [lodash](https://npm.io/package/lodash.md) ^4.17.21
- [type-fest](https://npm.io/package/type-fest.md) ^2.19.0

## Alternatives

- [@mce/gif](https://npm.io/package/@mce/gif.md) — 2.6K weekly downloads
- [cleanse](https://npm.io/package/cleanse.md) — 173 weekly downloads
- [str](https://npm.io/package/str.md) — 127 weekly downloads
- [naming](https://npm.io/package/naming.md) — 95 weekly downloads
- [tap-telco-api](https://npm.io/package/tap-telco-api.md) — 19 weekly downloads

## Recent versions

- 2.0.0-alpha.3 (latest) — 2022-08-26
- 2.0.0-alpha.2 — 2022-08-26
- 2.0.0-alpha.1 — 2022-08-26
- 2.0.0-alpha — 2022-08-26
- 1.2.1 — 2019-06-24
- 1.2.0 — 2019-04-06
- 1.1.3 — 2019-04-05
- 1.0.2 — 2019-04-05
- 1.1.2 — 2017-04-26
- 1.1.1 — 2017-03-17
- 1.1.0 — 2017-03-17
- 1.0.1 — 2017-03-15
- 1.0.0 — 2017-03-14

## README

# Deep Cleaner
[![npm version](https://badge.fury.io/js/deep-cleaner.svg)](https://badge.fury.io/js/deep-cleaner)
[![Build Status](https://travis-ci.com/darksinge/deep-cleaner.svg?branch=master)](https://travis-ci.com/darksinge/deep-cleaner)
[![npm](https://img.shields.io/npm/v/npm.svg)]()
[![DUB](https://img.shields.io/dub/l/vibe-d.svg)]()

Delete nested key-value pairs by a specified key or remove empty objects, empty strings, null, and undefined values from an object. 

This module was created primarily with JSON in mind. The module is light-weight and dependency free.

## Installation

`npm install --save deep-cleaner`

## Usage:

To remove empty values on an object:

```
> var cleaner = require('deep-cleaner');
> var obj = {
...   a: 'value',
...   emptyString: '',
...   emptyArray: [],
...   emptyObject: {},
...   isNull: null,
...   isUndefined: undefined
...   b: {
...     a: 'nother value',
...     anotherEmptyString: '',
...     arr: [
...       {c: null},
...       {d: 'value'}
...     ]
...   }
... }
> cleaner(obj);
> console.log(obj);
{ aValue: 'value', b: { a: 'nother value', arr: [ {}, {d: 'value'} ] } }
```

To remove a key-value pair, pass in the name of the key as the second parameter:

```
> var dirtyObject = {
...   a: {
...     b: {
...       dirty: "value"
...     }
...   },
...   dirty: "value",
...   some_list: [
...     {
...       dirty: "value",
...       clean: "value"
...     },
...     {
...       dirty: "value",
...       clean: "value"
...     },
...     {
...       dirty: "value",
...       clean: "value"
...     },
...   ]
... }
> cleaner(dirtyObject, 'dirty');
> console.log(dirtyObject);
{ a: { b: {} }, some_list: [ {
  clean: "value" }, { clean: "value" }, { clean: "value" } ] }
```

You can also pass in an array of keys.

```
> var obj = {
...   a: {
...     a: 'foo',
...     b: 'bar',
...     c: 'baz'
...   },
...   b: 'quux',
...   c: 'plugh',
...   d: 'grault'
... }
> cleaner(obj, ['b','c']);
> console.log(obj);
{ a: { a: 'foo' }, d: 'grault' }
```

As of v1.2.0, `deep-cleaner` also works on objects with circular references!
```
> var foo = { bar: null, baz: '' };
> foo.bar = foo;
> cleaner(foo);
> console.log(foo);
{ bar: [Circular] }
```

## API

<!--lint enable code-block-style-->

### `deepCleaner(obj[, key])`

Removes "empty" values from `obj`. A Value is considered "empty" if it is:

 - an empty string
 - an empty array
 - an empty object
 - null
 - undefined

#### Arguments

##### `obj`

`Object` - The object to be cleaned.

##### `key` 

`String?|Array?` - A key or array of keys to be removed from `obj`. If `key` is not
specified, then `deepCleaner` will default to removing empty values, otherwise, only
`obj[key]` will be removed (or `obj[key[0]]`,..., `obj[key[n]]` if `key` is an array of keys).

## Contributing to Deep Cleaner
Pull requests are welcome! If you notice a bug or want to add a feature, please feel free to contribute
to this project.

Here are a few guidelines you should follow.
 - Please make new pull requests into the `development` branch. A new contribution might prompt me to make additional changes that I'd like packaged together in a single PR to `master`.
 - Please write as much detail as necessary in your commit messages to clearly explain any changes.
 - Changes to the code that do not have passing unit tests will not be accepted.
 
 Notice a bug but don't have time to fix it? Submit an issue! I strive to keep this package well maintained.

## Testing

run `npm test`.

## License

[MIT][license]


<!-- links -->

[license]: LICENSE.md

---
_Source: https://npm.io/package/deep-cleaner · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
