mirror of
https://github.com/ascribe/onion.git
synced 2024-11-15 09:35:10 +01:00
Sanitize utility should not modify given object
Mutating arguments and then returning them is redundant and confusing behaviour (why pass it back if they already have it? Am I getting a new copy since it returns something?).
This commit is contained in:
parent
d23331d9b9
commit
1e328b722b
@ -1,29 +1,23 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
import _ from 'lodash';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Takes an object and deletes all keys that are
|
* Takes an object and returns a shallow copy without any keys
|
||||||
*
|
* that fail the passed in filter function.
|
||||||
* tagged as false by the passed in filter function
|
* Does not modify the passed in object.
|
||||||
*
|
*
|
||||||
* @param {object} obj regular javascript object
|
* @param {object} obj regular javascript object
|
||||||
* @return {object} regular javascript object without null values or empty strings
|
* @return {object} regular javascript object without null values or empty strings
|
||||||
*/
|
*/
|
||||||
export function sanitize(obj, filterFn) {
|
export function sanitize(obj, filterFn) {
|
||||||
if(!filterFn) {
|
if (!filterFn) {
|
||||||
// By matching null with a double equal, we can match undefined and null
|
// By matching null with a double equal, we can match undefined and null
|
||||||
// http://stackoverflow.com/a/15992131
|
// http://stackoverflow.com/a/15992131
|
||||||
filterFn = (val) => val == null || val === '';
|
filterFn = (val) => val == null || val === '';
|
||||||
}
|
}
|
||||||
|
|
||||||
Object
|
return _.omit(obj, filterFn);
|
||||||
.keys(obj)
|
|
||||||
.map((key) => {
|
|
||||||
if(filterFn(obj[key])) {
|
|
||||||
delete obj[key];
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return obj;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user