# compact-object-deep

> Creates a deep clone of an object with all falsey values removed

Latest version **1.0.0** (published 2022-02-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install compact-object-deep
pnpm add compact-object-deep
yarn add compact-object-deep
bun add compact-object-deep
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2022-02-06 |
| First published | 2022-02-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 5.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Sergey Ledyankin |
| Maintainers | sirgeika |
| Keywords | compact, object, compact-deep |

## Links

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

## Dependencies (2)

- [lodash.isempty](https://npm.io/package/lodash.isempty.md) 4.4.0
- [lodash.isobject](https://npm.io/package/lodash.isobject.md) 3.0.2

## Recent versions

- 1.0.0 (latest) — 2022-02-06

## README

# compact-object-deep

Creates a deep clone of object with all falsey values removed.
The values `false`, `null`, `""`, `undefined`, and `NaN` are falsey.

If `customizer` is provided it's invoked to check value. If `customizer` returns 
`undefined` compacting is handled by the method instead.
The customizer invoked with up to four argument: `(value, key, path, object)`.

The `customizer` can't delete property with not empty value:
- If value of object is not empty - it can change their value.
- If value of object is empty - it can help keep this prop.


```bash
npm install compact-object-deep
```

```js
const compactDeep = require('compact-object-deep');

const source = {
  one: '1',
  two: 2,
  three: '',
  four: null,
  five: undefined,
  six: 0,
  seven: false,
  eight: true
};

const result = compactDeep(source);
/*
    {
      one: '1',
      two: 2,
      six: 0,
      eight: true
    }
*/
```

```js
const source = [
  '1',
  2,
  '',
  'str',
  [ 1 ],
  [],
  {},
  {
    one: 1,
    two: '',
    three: 3,
    four: {
      fone: '',
      ftwo: 2
    }
  }
];

const result = compactDeep(source);
/*
[
  '1',
  2,
  'str',
  [ 1 ],
  {
    one: 1,
    three: 3,
    four: {
      ftwo: 2
    }
  }
] 
*/
```

#### Arrays

```js
const source = {
  arr1: [],
  arr2: [ 1, '', '5'],
  arr3: [ '', [], [ [], 2, [ [ { a: 'a' } ] ] ] ]
};

const result = compactDeep(source);
/*
{
  arr2: [ 1, '5'],
  arr3: [ [ 2, [ [ { a: 'a' } ] ] ] ]
} 
*/
```

### With customizer

#### Keep falsy value only concrete `path`
```js
const source = {
  one: 1,
  two: '',
  arr: [
    1,
    [],
    ''
  ]
};

const result = compactDeep(source, function(val, key, path) {
  if (path === 'arr.[1]') {
    return val;
  }
});

/*
 {
    one: 1,
    arr: [
      1,
      []
    ]
  }
*/
```

#### Keep falsy value only concrete `key`

```js
const source = {
  one: 1,
  saveMe: '',
  notSaveMe: false,
  obj: {
    two: 2,
    saveMe: '',
    obj: {
      saveMe: null,
      poch: null
    }
  }
};

const result = compactDeep(source, function(val, key) {
  if (key === 'saveMe') {
    return val;
  }
});

/*
  {
    one: 1,
    saveMe: '',
    obj: {
      two: 2,
      saveMe: '',
      obj: {
        saveMe: null
      }
    }
  }
*/
```

#### Keep functions

```js
const source = {
  func1: function() {
    return 1;
  },
  obj: {
    func2: function(val) {
      return val;
    }
  }
};

const result = compactDeep(source, function(val) {
  if (typeof val === 'function') {
    return val;
  }
});

result.func1() === 1;
result.obj.func2(45) === 45;
```

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