Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom values support #31

Merged
merged 1 commit into from
Oct 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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