Skip to content

Commit

Permalink
Add custom values support
Browse files Browse the repository at this point in the history
  • Loading branch information
nunofgs committed Oct 15, 2019
1 parent 6c258c0 commit 82be35d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ $ npm install clean-deep --save

Option | Default value | Description
----------------- | ------------- | -----------------------------------
_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: `''`
Expand Down
8 changes: 7 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const transform = require('lodash.transform');
*/

module.exports = function cleanDeep(object, {
cleanValues = [],
emptyArrays = true,
emptyObjects = true,
emptyStrings = true,
Expand All @@ -21,7 +22,12 @@ module.exports = function cleanDeep(object, {
return transform(object, (result, value, key) => {
// Recurse into arrays and objects.
if (Array.isArray(value) || isPlainObject(value)) {
value = cleanDeep(value, { emptyArrays, emptyObjects, emptyStrings, nullValues, undefinedValues });
value = cleanDeep(value, { cleanValues, emptyArrays, emptyObjects, emptyStrings, nullValues, undefinedValues });
}

// Exclude specific values.
if (cleanValues.includes(value)) {
return;
}

// Exclude empty objects.
Expand Down
17 changes: 17 additions & 0 deletions test/src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,23 @@ describe('cleanDeep()', () => {
});
});

it('should support custom values', () => {
const object = {
biz: {
baz: 123
},
foo: {
bar: 'abc'
}
};

expect(cleanDeep(object, { cleanValues: ['abc'] })).toEqual({
biz: {
baz: 123
}
});
});

it('should include empty objects if `emptyObjects` is `false`', () => {
const object = {
biz: {
Expand Down

0 comments on commit 82be35d

Please sign in to comment.