2015-06-05 11:06:36 +02:00
|
|
|
'use strict';
|
|
|
|
|
2015-06-08 11:19:18 +02:00
|
|
|
/**
|
2015-06-08 11:50:40 +02:00
|
|
|
* Takes an object and deletes all keys that are
|
2015-06-08 11:19:18 +02:00
|
|
|
*
|
2015-07-13 21:19:45 +02:00
|
|
|
* tagged as false by the passed in filter function
|
2015-06-08 11:50:40 +02:00
|
|
|
*
|
2015-06-08 11:19:18 +02:00
|
|
|
* @param {object} obj regular javascript object
|
|
|
|
* @return {object} regular javascript object without null values or empty strings
|
|
|
|
*/
|
2015-07-13 21:19:45 +02:00
|
|
|
export function sanitize(obj, 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 === '';
|
|
|
|
}
|
|
|
|
|
2015-06-02 13:42:17 +02:00
|
|
|
Object
|
|
|
|
.keys(obj)
|
|
|
|
.map((key) => {
|
2015-07-13 21:19:45 +02:00
|
|
|
if(filterFn(obj[key])) {
|
2015-06-02 13:42:17 +02:00
|
|
|
delete obj[key];
|
|
|
|
}
|
|
|
|
});
|
2015-05-20 14:48:57 +02:00
|
|
|
|
2015-06-02 13:42:17 +02:00
|
|
|
return obj;
|
2015-06-05 11:06:36 +02:00
|
|
|
}
|
2015-05-20 16:10:02 +02:00
|
|
|
|
2015-06-02 13:42:17 +02:00
|
|
|
/**
|
|
|
|
* Sums up a list of numbers. Like a Epsilon-math-kinda-sum...
|
|
|
|
*/
|
|
|
|
export function sumNumList(l) {
|
|
|
|
let sum = 0;
|
|
|
|
l.forEach((num) => sum += parseFloat(num) || 0);
|
|
|
|
return sum;
|
2015-06-05 11:06:36 +02:00
|
|
|
}
|
2015-05-26 13:33:35 +02:00
|
|
|
|
2015-06-02 13:42:17 +02:00
|
|
|
/*
|
|
|
|
Taken from http://stackoverflow.com/a/4795914/1263876
|
|
|
|
Behaves like C's format string function
|
|
|
|
*/
|
|
|
|
export function formatText() {
|
2015-06-08 11:19:18 +02:00
|
|
|
let args = arguments,
|
2015-06-02 13:42:17 +02:00
|
|
|
string = args[0],
|
|
|
|
i = 1;
|
2015-06-08 11:19:18 +02:00
|
|
|
return string.replace(/%((%)|s|d)/g, (m) => {
|
2015-06-02 13:42:17 +02:00
|
|
|
// m is the matched format, e.g. %s, %d
|
2015-06-08 11:19:18 +02:00
|
|
|
let val = null;
|
2015-06-02 13:42:17 +02:00
|
|
|
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;
|
2015-06-02 13:31:12 +02:00
|
|
|
}
|
2015-06-02 13:42:17 +02:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
});
|
2015-06-05 11:06:36 +02:00
|
|
|
}
|
2015-06-04 15:30:21 +02:00
|
|
|
|
2015-07-01 16:19:02 +02:00
|
|
|
/*
|
|
|
|
Checks a list of objects for key duplicates and returns a boolean
|
|
|
|
*/
|
|
|
|
function _doesObjectListHaveDuplicates(l) {
|
|
|
|
let mergedList = [];
|
|
|
|
|
2015-07-08 15:40:32 +02:00
|
|
|
l = l.map((obj) => {
|
|
|
|
if(!obj) {
|
|
|
|
throw new Error('The object you are trying to merge is null instead of an empty object');
|
|
|
|
}
|
|
|
|
|
|
|
|
return Object.keys(obj);
|
|
|
|
});
|
2015-07-01 16:19:02 +02:00
|
|
|
|
|
|
|
// Taken from: http://stackoverflow.com/a/10865042
|
|
|
|
// How to flatten an array of arrays in javascript.
|
2015-07-08 15:40:32 +02:00
|
|
|
// If two objects contain the same key, then these two keys
|
2015-07-01 16:19:02 +02:00
|
|
|
// will actually be represented in the merged array
|
|
|
|
mergedList = mergedList.concat.apply(mergedList, l);
|
|
|
|
|
|
|
|
// Taken from: http://stackoverflow.com/a/7376645/1263876
|
|
|
|
// By casting the array to a set, and then checking if the size of the array
|
|
|
|
// shrunk in the process of casting, we can check if there were any duplicates
|
|
|
|
return (new Set(mergedList)).size !== mergedList.length;
|
|
|
|
}
|
|
|
|
|
2015-06-04 15:30:21 +02:00
|
|
|
/**
|
|
|
|
* Takes a list of object and merges their keys to one object.
|
|
|
|
* Uses mergeOptions for two objects.
|
|
|
|
* @param {[type]} l [description]
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
2015-06-04 15:38:15 +02:00
|
|
|
export function mergeOptions(...l) {
|
2015-07-01 16:19:02 +02:00
|
|
|
// If the objects submitted in the list have duplicates,in their key names,
|
|
|
|
// abort the merge and tell the function's user to check his objects.
|
|
|
|
if(_doesObjectListHaveDuplicates(l)) {
|
|
|
|
throw new Error('The objects you submitted for merging have duplicates. Merge aborted.');
|
|
|
|
}
|
|
|
|
|
2015-06-04 15:30:21 +02:00
|
|
|
let newObj = {};
|
|
|
|
|
|
|
|
for(let i = 1; i < l.length; i++) {
|
2015-06-05 11:06:36 +02:00
|
|
|
newObj = _mergeOptions(newObj, _mergeOptions(l[i - 1], l[i]));
|
2015-06-04 15:30:21 +02:00
|
|
|
}
|
2015-07-08 15:40:32 +02:00
|
|
|
|
2015-06-04 15:30:21 +02:00
|
|
|
return newObj;
|
2015-06-05 11:06:36 +02:00
|
|
|
}
|
2015-06-04 15:30:21 +02:00
|
|
|
|
2015-07-10 18:51:35 +02:00
|
|
|
/**
|
2015-07-17 15:41:09 +02:00
|
|
|
* Merges a number of objects even if there're having duplicates.
|
|
|
|
*
|
|
|
|
* DOES NOT RETURN AN ERROR!
|
2015-07-17 15:52:53 +02:00
|
|
|
*
|
2015-07-10 18:51:35 +02:00
|
|
|
* Takes a list of object and merges their keys to one object.
|
|
|
|
* Uses mergeOptions for two objects.
|
|
|
|
* @param {[type]} l [description]
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
|
|
|
export function mergeOptionsWithDuplicates(...l) {
|
|
|
|
// If the objects submitted in the list have duplicates,in their key names,
|
|
|
|
// abort the merge and tell the function's user to check his objects.
|
|
|
|
let newObj = {};
|
|
|
|
|
|
|
|
for(let i = 1; i < l.length; i++) {
|
|
|
|
newObj = _mergeOptions(newObj, _mergeOptions(l[i - 1], l[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
return newObj;
|
|
|
|
}
|
|
|
|
|
2015-07-14 21:15:10 +02:00
|
|
|
/**
|
|
|
|
* In place update of a dictionary
|
|
|
|
*/
|
|
|
|
export function update(a, ...l) {
|
|
|
|
for(let i = 0; i < l.length; i++) {
|
|
|
|
for (let attrname in l[i]) {
|
|
|
|
a[attrname] = l[i][attrname];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2015-06-04 15:30:21 +02:00
|
|
|
/**
|
|
|
|
* Overwrites obj1's values with obj2's and adds obj2's if non existent in obj1
|
|
|
|
* @param obj1
|
|
|
|
* @param obj2
|
|
|
|
* @returns obj3 a new object based on obj1 and obj2
|
|
|
|
* Taken from: http://stackoverflow.com/a/171256/1263876
|
|
|
|
*/
|
2015-07-01 16:19:02 +02:00
|
|
|
function _mergeOptions(obj1, obj2) {
|
2015-06-05 11:06:36 +02:00
|
|
|
let obj3 = {};
|
2015-06-08 11:19:18 +02:00
|
|
|
|
|
|
|
for (let attrname in obj1) {
|
|
|
|
obj3[attrname] = obj1[attrname];
|
|
|
|
}
|
|
|
|
for (let attrname in obj2) {
|
2015-07-01 16:19:02 +02:00
|
|
|
obj3[attrname] = obj2[attrname];
|
2015-06-08 11:19:18 +02:00
|
|
|
}
|
2015-06-04 15:30:21 +02:00
|
|
|
return obj3;
|
2015-07-14 21:15:10 +02:00
|
|
|
}
|