mirror of
https://github.com/ascribe/onion.git
synced 2024-11-15 01:25:17 +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';
|
||||
|
||||
import _ from 'lodash';
|
||||
|
||||
/**
|
||||
* Takes an object and deletes all keys that are
|
||||
*
|
||||
* tagged as false by the passed in filter function
|
||||
* Takes an object and returns a shallow copy without any keys
|
||||
* that fail the passed in filter function.
|
||||
* Does not modify the passed in object.
|
||||
*
|
||||
* @param {object} obj regular javascript object
|
||||
* @return {object} regular javascript object without null values or empty strings
|
||||
*/
|
||||
export function sanitize(obj, filterFn) {
|
||||
if(!filterFn) {
|
||||
if (!filterFn) {
|
||||
// By matching null with a double equal, we can match undefined and null
|
||||
// http://stackoverflow.com/a/15992131
|
||||
filterFn = (val) => val == null || val === '';
|
||||
}
|
||||
|
||||
Object
|
||||
.keys(obj)
|
||||
.map((key) => {
|
||||
if(filterFn(obj[key])) {
|
||||
delete obj[key];
|
||||
}
|
||||
});
|
||||
|
||||
return obj;
|
||||
return _.omit(obj, filterFn);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user