1. Common
function searchTerm(inputValue: string, props: Array<string>, originalList: Array<any>) {
let filteredList: any[];
if (inputValue && props && originalList) {
inputValue = inputValue.toLowerCase();
let filtered = originalList.filter((item) => {
let match = false;
for (let prop of props) {
if (
item[prop]
.toString()
.toLowerCase()
.indexOf(inputValue) > -1
) {
match = true;
break;
}
}
return match;
});
filteredList = filtered;
} else {
filteredList = originalList;
}
return filteredList;
}
mapArr2Obj(arr) {
return arr.reduce((accumulator, current) => {
accumulator[current.id] = current;
return accumulator;
}, {});
}
function isArray(obj) {
if (typeof obj === 'object') {
return Object.prototype.toString.call(obj) === '[object Array]';
}
return false;
}
function isFunction(arg) {
if (arg) {
if (typeof /./ !== 'function') {
return typeof arg === 'function';
} else {
return Object.prototype.toString.call(arg) === '[object Function]';
}
}
return false;
}
function parseUrl(url) {
var result = {};
var keys = [
'href',
'origin',
'protocol',
'host',
'hostname',
'port',
'pathname',
'search',
'hash',
];
var i, len;
var regexp = /(([^:]+:)\/\/(([^:\/\?#]+)(:\d+)?))(\/[^?#]*)?(\?[^#]*)?(#.*)?/;
var match = regexp.exec(url);
if (match) {
for (i = keys.length - 1; i >= 0; --i) {
result[keys[i]] = match[i] ? match[i] : '';
}
}
return result;
}
function getQueryStringParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[[\]]/g, '\\$&');
var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, ' '));
}
export function sortByAlphaIgnoreCase(arr) {
arr.sort((a, b) => {
return a.localeCompare(b, undefined , {
sensitivity: 'base',
});
});
}
function deepClone(obj) {
let temp = Array.isArray(obj) ? [] : {};
for (let key in obj) {
temp[key] = typeof obj[key] === 'object' ? deepClone(obj[key]) : obj[key];
}
return temp;
}
function deepClone(obj) {
var _toString = Object.prototype.toString;
if (!obj || typeof obj !== 'object') {
return obj;
}
if (obj.nodeType && 'cloneNode' in obj) {
return obj.cloneNode(true);
}
if (_toString.call(obj) === '[object Date]') {
return new Date(obj.getTime());
}
if (_toString.call(obj) === '[object RegExp]') {
var flags = [];
if (obj.global) {
flags.push('g');
}
if (obj.multiline) {
flags.push('m');
}
if (obj.ignoreCase) {
flags.push('i');
}
return new RegExp(obj.source, flags.join(''));
}
var result = Array.isArray(obj) ? [] : obj.constructor ? new obj.constructor() : {};
for (var key in obj) {
result[key] = deepClone(obj[key]);
}
return result;
}