2015-06-05 11:06:36 +02:00
|
|
|
'use strict';
|
|
|
|
|
2015-12-08 20:55:13 +01:00
|
|
|
/**
|
|
|
|
* Checks shallow equality
|
|
|
|
* Re-export of shallow from shallow-equals
|
|
|
|
*/
|
|
|
|
export { default as isShallowEqual } from 'shallow-equals';
|
|
|
|
|
2015-06-08 11:19:18 +02:00
|
|
|
/**
|
2015-10-30 17:46:51 +01:00
|
|
|
* 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.
|
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) {
|
2015-10-30 17:46:51 +01:00
|
|
|
if (!filterFn) {
|
2015-07-13 21:19:45 +02:00
|
|
|
// By matching null with a double equal, we can match undefined and null
|
|
|
|
// http://stackoverflow.com/a/15992131
|
|
|
|
filterFn = (val) => val == null || val === '';
|
|
|
|
}
|
|
|
|
|
2015-11-02 18:57:56 +01:00
|
|
|
return omitFromObject(obj, filterFn);
|
2015-06-05 11:06:36 +02:00
|
|
|
}
|
2015-05-20 16:10:02 +02:00
|
|
|
|
2015-08-25 16:33:26 +02:00
|
|
|
/**
|
|
|
|
* Removes all falsy values (undefined, null, false, ...) from a list/array
|
|
|
|
* @param {array} l the array to sanitize
|
|
|
|
* @return {array} the sanitized array
|
|
|
|
*/
|
|
|
|
export function sanitizeList(l) {
|
|
|
|
let sanitizedList = [];
|
|
|
|
|
|
|
|
for(let i = 0; i < l.length; i++) {
|
|
|
|
if(l[i]) {
|
|
|
|
sanitizedList.push(l[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sanitizedList;
|
|
|
|
}
|
|
|
|
|
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-11-02 18:27:45 +01:00
|
|
|
/**
|
|
|
|
* Checks a list of objects for key duplicates and returns a boolean
|
2015-07-01 16:19:02 +02:00
|
|
|
*/
|
|
|
|
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
|
2016-01-18 17:31:56 +01:00
|
|
|
return new Set(mergedList).size !== mergedList.length;
|
2015-07-01 16:19:02 +02:00
|
|
|
}
|
|
|
|
|
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.
|
2015-12-08 20:55:13 +01:00
|
|
|
if (_doesObjectListHaveDuplicates(l)) {
|
2015-07-01 16:19:02 +02:00
|
|
|
throw new Error('The objects you submitted for merging have duplicates. Merge aborted.');
|
|
|
|
}
|
|
|
|
|
2015-11-02 18:27:45 +01:00
|
|
|
return Object.assign({}, ...l);
|
2015-07-10 18:51:35 +02:00
|
|
|
}
|
|
|
|
|
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-07-29 18:00:49 +02:00
|
|
|
/**
|
|
|
|
* Escape HTML in a string so it can be injected safely using
|
|
|
|
* React's `dangerouslySetInnerHTML`
|
|
|
|
*
|
|
|
|
* @param s the string to be sanitized
|
|
|
|
*
|
|
|
|
* Taken from: http://stackoverflow.com/a/17546215/597097
|
|
|
|
*/
|
|
|
|
export function escapeHTML(s) {
|
|
|
|
return document.createElement('div').appendChild(document.createTextNode(s)).parentNode.innerHTML;
|
2015-09-08 11:44:05 +02:00
|
|
|
}
|
|
|
|
|
2015-11-02 18:31:01 +01:00
|
|
|
/**
|
|
|
|
* Returns a copy of the given object's own and inherited enumerable
|
|
|
|
* properties, omitting any keys that pass the given filter function.
|
|
|
|
*/
|
2015-12-08 18:22:11 +01:00
|
|
|
function applyFilterOnObject(obj, filterFn) {
|
2015-11-02 18:31:01 +01:00
|
|
|
const filteredObj = {};
|
|
|
|
|
|
|
|
for (let key in obj) {
|
|
|
|
const val = obj[key];
|
|
|
|
if (filterFn == null || !filterFn(val, key)) {
|
|
|
|
filteredObj[key] = val;
|
2015-09-08 11:44:05 +02:00
|
|
|
}
|
|
|
|
}
|
2015-11-02 18:31:01 +01:00
|
|
|
|
|
|
|
return filteredObj;
|
|
|
|
}
|
|
|
|
|
2015-12-08 18:22:11 +01:00
|
|
|
/**
|
|
|
|
* Abstraction for selectFromObject and omitFromObject
|
|
|
|
* for DRYness
|
|
|
|
* @param {boolean} isInclusion True if the filter should be for including the filtered items
|
|
|
|
* (ie. selecting only them vs omitting only them)
|
|
|
|
*/
|
|
|
|
function filterFromObject(obj, filter, { isInclusion = true } = {}) {
|
|
|
|
if (filter && filter.constructor === Array) {
|
|
|
|
return applyFilterOnObject(obj, isInclusion ? ((_, key) => filter.indexOf(key) < 0)
|
|
|
|
: ((_, key) => filter.indexOf(key) >= 0));
|
|
|
|
} else if (filter && typeof filter === 'function') {
|
|
|
|
// Flip the filter fn's return if it's for inclusion
|
|
|
|
return applyFilterOnObject(obj, isInclusion ? (...args) => !filter(...args)
|
|
|
|
: filter);
|
|
|
|
} else {
|
|
|
|
throw new Error('The given filter is not an array or function. Exclude aborted');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Similar to lodash's _.pick(), this returns a copy of the given object's
|
|
|
|
* own and inherited enumerable properties, selecting only the keys in
|
|
|
|
* the given array or whose value pass the given filter function.
|
|
|
|
* @param {object} obj Source object
|
|
|
|
* @param {array|function} filter Array of key names to select or function to invoke per iteration
|
|
|
|
* @return {object} The new object
|
|
|
|
*/
|
|
|
|
export function selectFromObject(obj, filter) {
|
|
|
|
return filterFromObject(obj, filter);
|
|
|
|
}
|
|
|
|
|
2015-11-02 18:31:01 +01:00
|
|
|
/**
|
|
|
|
* Similar to lodash's _.omit(), this returns a copy of the given object's
|
|
|
|
* own and inherited enumerable properties, omitting any keys that are
|
|
|
|
* in the given array or whose value pass the given filter function.
|
|
|
|
* @param {object} obj Source object
|
|
|
|
* @param {array|function} filter Array of key names to omit or function to invoke per iteration
|
|
|
|
* @return {object} The new object
|
|
|
|
*/
|
|
|
|
export function omitFromObject(obj, filter) {
|
2015-12-08 18:22:11 +01:00
|
|
|
return filterFromObject(obj, filter, { isInclusion: false });
|
2015-09-25 11:20:12 +02:00
|
|
|
}
|
|
|
|
|
2015-11-23 17:12:47 +01:00
|
|
|
/**
|
|
|
|
* Recursively tests an object against a "match" object to see if the
|
|
|
|
* object is similar to the "match" object. In other words, this will
|
|
|
|
* deeply traverse the "match" object's properties and check them
|
|
|
|
* against the object by using the testFn.
|
|
|
|
*
|
|
|
|
* The object is considered a match if all primitive properties in the
|
|
|
|
* "match" object are found and accepted in the object by the testFn.
|
|
|
|
*
|
|
|
|
* @param {object} obj Object to test
|
|
|
|
* @param {object} match "Match" object to test against
|
|
|
|
* @param {(function)} testFn Function to use on each property test.
|
|
|
|
* Return true to accept the match.
|
|
|
|
* By default, applies strict equality using ===
|
|
|
|
* @return {boolean} True if obj matches the "match" object
|
|
|
|
*/
|
2016-02-05 15:17:59 +01:00
|
|
|
export function deepMatchObject(obj, match, testFn = (objProp, matchProp) => objProp === matchProp) {
|
2015-11-23 17:12:47 +01:00
|
|
|
if (typeof match !== 'object') {
|
|
|
|
throw new Error('Your specified match argument was not an object');
|
|
|
|
}
|
|
|
|
if (typeof testFn !== 'function') {
|
2016-02-05 15:17:59 +01:00
|
|
|
throw new Error('Your specified test function was not a function');
|
2015-11-23 17:12:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return Object
|
|
|
|
.keys(match)
|
|
|
|
.reduce((result, matchKey) => {
|
|
|
|
if (!result) { return false; }
|
|
|
|
|
2016-02-05 15:17:59 +01:00
|
|
|
const objProp = obj && obj[matchKey];
|
2015-11-23 17:12:47 +01:00
|
|
|
const matchProp = match[matchKey];
|
|
|
|
|
2015-11-25 18:58:56 +01:00
|
|
|
if (typeof matchProp === 'object') {
|
|
|
|
return (typeof objProp === 'object') ? deepMatchObject(objProp, matchProp, testFn)
|
|
|
|
: false;
|
|
|
|
} else {
|
|
|
|
return testFn(objProp, matchProp);
|
|
|
|
}
|
2015-11-23 17:12:47 +01:00
|
|
|
}, true);
|
|
|
|
}
|
|
|
|
|
2015-09-25 11:20:12 +02:00
|
|
|
/**
|
|
|
|
* Takes a string and breaks it at the supplied index and replaces it
|
|
|
|
* with a (potentially) short string that also has been provided
|
2015-09-25 15:22:16 +02:00
|
|
|
* @param {string} text The string to truncate
|
|
|
|
* @param {number} charIndex The char number at which the text should be truncated
|
2015-09-25 11:20:12 +02:00
|
|
|
* @param {String} replacement All text after charIndex will be replaced with this string
|
2015-09-25 15:22:16 +02:00
|
|
|
* @return {string} The truncated text
|
2015-09-25 11:20:12 +02:00
|
|
|
*/
|
2015-09-25 15:22:16 +02:00
|
|
|
export function truncateTextAtCharIndex(text, charIndex, replacement = '...') {
|
2015-09-25 15:25:34 +02:00
|
|
|
let truncatedText = '';
|
2015-09-25 11:20:12 +02:00
|
|
|
|
2015-09-25 15:25:34 +02:00
|
|
|
truncatedText = text.slice(0, charIndex);
|
|
|
|
truncatedText += text.length > charIndex ? replacement : '';
|
2015-09-25 11:20:12 +02:00
|
|
|
|
2015-09-25 15:25:34 +02:00
|
|
|
return truncatedText;
|
2015-10-13 18:33:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param index, int, the starting index of the substring to be replaced
|
|
|
|
* @param character, substring to be replaced
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
export function replaceSubstringAtIndex(baseString, substrToReplace, stringToBePut) {
|
|
|
|
let index = baseString.indexOf(substrToReplace);
|
|
|
|
return baseString.substr(0, index) + stringToBePut + baseString.substr(index + substrToReplace.length);
|
2015-10-15 10:06:39 +02:00
|
|
|
}
|
|
|
|
|
2015-09-29 14:58:56 +02:00
|
|
|
/**
|
|
|
|
* Extracts the user's subdomain from the browser's window.
|
|
|
|
* If no subdomain is found (for example on a naked domain), the default "www" is just assumed.
|
|
|
|
* @return {string} subdomain as a string
|
|
|
|
*/
|
|
|
|
export function getSubdomain() {
|
|
|
|
let { host } = window.location;
|
2015-09-30 11:09:04 +02:00
|
|
|
let tokens = host.split('.');
|
|
|
|
return tokens.length > 2 ? tokens[0] : 'www';
|
2015-10-20 18:10:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes two lists and returns their intersection as a list
|
|
|
|
* @param {Array} a
|
|
|
|
* @param {Array} b
|
|
|
|
* @return {[Array]} Intersected list of a and b
|
|
|
|
*/
|
|
|
|
export function intersectLists(a, b) {
|
|
|
|
return a.filter((val) => b.indexOf(val) > -1);
|
2015-10-22 13:38:49 +02:00
|
|
|
}
|