2015-05-19 17:01:28 +02:00
|
|
|
// TODO: Create Unittests that test all functions
|
|
|
|
|
|
|
|
let GeneralUtils = {
|
|
|
|
/**
|
|
|
|
* Removes undefined and null values from an key-value object.
|
|
|
|
*/
|
|
|
|
sanitize(obj) {
|
|
|
|
Object
|
|
|
|
.keys(obj)
|
|
|
|
.map((key) => {
|
|
|
|
// By matching null with a double equal, we can match undefined and null
|
|
|
|
// http://stackoverflow.com/a/15992131
|
2015-05-21 17:54:27 +02:00
|
|
|
if(obj[key] == null || obj[key] === '') {
|
2015-05-19 17:01:28 +02:00
|
|
|
delete obj[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return obj;
|
2015-05-20 14:48:57 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the values of an object.
|
|
|
|
*/
|
|
|
|
valuesOfObject(obj) {
|
|
|
|
return Object
|
|
|
|
.keys(obj)
|
|
|
|
.map(key => obj[key]);
|
2015-05-20 16:10:02 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sums up a list of numbers. Like a Epsilon-math-kinda-sum...
|
|
|
|
*/
|
|
|
|
sumNumList(l) {
|
|
|
|
let sum = 0;
|
|
|
|
l.forEach((num) => sum += parseFloat(num) || 0);
|
|
|
|
return sum;
|
2015-05-19 17:01:28 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-05-21 17:54:27 +02:00
|
|
|
export default GeneralUtils;
|