1
0
mirror of https://github.com/ascribe/onion.git synced 2024-06-30 13:41:57 +02:00
onion/js/utils/general_utils.js

75 lines
2.0 KiB
JavaScript
Raw Normal View History

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
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]);
},
/**
* 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;
},
/*
Taken from http://stackoverflow.com/a/4795914/1263876
Behaves like C's format string function
2015-05-26 13:33:35 +02:00
Does not throw errors though if a argument's type is not
matching its template's representative!!!
This essentially means you can use %d or %s for anything...
*/
formatText() {
var args = arguments,
string = args[0],
i = 1;
return string.replace(/%((%)|s|d)/g, function (m) {
// m is the matched format, e.g. %s, %d
var val = null;
if (m[2]) {
val = m[2];
} else {
val = args[i];
// A switch statement so that the formatter can be extended. Default is %s
switch (m) {
case '%d':
val = parseFloat(val);
if (isNaN(val)) {
val = 0;
}
break;
}
i++;
}
return val;
});
}
2015-05-19 17:01:28 +02:00
};
export default GeneralUtils;