export * from './formatHours.util'; export const nameOf: (string: keyof T) => string = (name: keyof T) => name?.toString(); export class MwUtils { public static filterArrayByString( mainArr, searchText, property?: string | Array ): any[] { if (!searchText || typeof searchText !== 'string') { return mainArr; } searchText = searchText.toLowerCase(); return mainArr.filter((itemObj) => { return this.searchInObj(itemObj, searchText, property); }); } public static searchInObj( itemObj, searchText, property?: string | Array ): boolean { const search = (value): boolean => { if (typeof value === 'string') { if (this.searchInString(value, searchText)) { return true; } } else if (Array.isArray(value)) { if (this.searchInArray(value, searchText)) { return true; } } if (typeof value === 'object') { if (this.searchInObj(value, searchText)) { return true; } } return false; }; const searchInProperties = (properties): boolean => { for (const i in properties) { if (!itemObj.hasOwnProperty(properties[i])) { continue; } if (search(itemObj[properties[i]])) { return true; } } return false; }; if (itemObj === null) { return false; } if (property) { if (typeof property === 'string' && itemObj.hasOwnProperty(property)) { return search(itemObj[property]); } else if (Array.isArray(property)) { return searchInProperties(property); } } return searchInProperties(Object.getOwnPropertyNames(itemObj)); } public static searchInArray(arr, searchText): boolean { for (const value of arr) { if (typeof value === 'string') { if (this.searchInString(value, searchText)) { return true; } } if (typeof value === 'object') { if (this.searchInObj(value, searchText)) { return true; } } } } public static searchInString(value, searchText): boolean { return value.toLowerCase().includes(searchText); } public static generateGUID(): string { function S4(): string { return Math.floor((1 + Math.random()) * 0x10000) .toString(16) .substring(1); } return S4() + S4(); } public static toggleInArray(item, array): void { if (array.indexOf(item) === -1) { array.push(item); } else { array.splice(array.indexOf(item), 1); } } public static handleize(text): string { return text .toString() .toLowerCase() .replace(/\s+/g, '-') // Replace spaces with - .replace(/[^\w\-]+/g, '') // Remove all non-word chars .replace(/\-\-+/g, '-') // Replace multiple - with single - .replace(/^-+/, '') // Trim - from start of text .replace(/-+$/, ''); // Trim - from end of text } }