2017-07-28 08:29:29 +00:00
|
|
|
export class FuseUtils
|
|
|
|
{
|
|
|
|
public static filterArrayByString(mainArr, searchText)
|
|
|
|
{
|
2017-07-28 09:26:56 +00:00
|
|
|
if ( searchText === '' )
|
|
|
|
{
|
|
|
|
return mainArr;
|
|
|
|
}
|
|
|
|
|
2017-07-28 08:29:29 +00:00
|
|
|
searchText = searchText.toLowerCase();
|
|
|
|
|
|
|
|
return mainArr.filter(itemObj => {
|
|
|
|
return this.searchInObj(itemObj, searchText);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public static searchInObj(itemObj, searchText)
|
|
|
|
{
|
|
|
|
for ( const prop in itemObj )
|
|
|
|
{
|
|
|
|
if ( !itemObj.hasOwnProperty(prop) )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const value = itemObj[prop];
|
|
|
|
|
|
|
|
if ( typeof value === 'string' )
|
|
|
|
{
|
2017-09-18 07:47:35 +00:00
|
|
|
if ( this.searchInString(value, searchText) )
|
2017-07-28 08:29:29 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static searchInArray(arr, searchText)
|
|
|
|
{
|
|
|
|
for ( const value of arr )
|
|
|
|
{
|
|
|
|
if ( typeof value === 'string' )
|
|
|
|
{
|
2017-09-18 07:47:35 +00:00
|
|
|
if ( this.searchInString(value, searchText) )
|
2017-07-28 08:29:29 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( typeof value === 'object' )
|
|
|
|
{
|
|
|
|
if ( this.searchInObj(value, searchText) )
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-18 07:47:35 +00:00
|
|
|
public static searchInString(value, searchText)
|
2017-07-28 08:29:29 +00:00
|
|
|
{
|
2017-08-17 07:31:04 +00:00
|
|
|
return value.toLowerCase().includes(searchText);
|
2017-07-28 08:29:29 +00:00
|
|
|
}
|
2017-07-28 14:40:11 +00:00
|
|
|
|
2017-08-18 11:50:19 +00:00
|
|
|
public static generateGUID()
|
2017-07-28 14:40:11 +00:00
|
|
|
{
|
|
|
|
function S4()
|
|
|
|
{
|
|
|
|
return (((1 + Math.random()) * 0x10000) || 0).toString(16).substring(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (S4() + S4());
|
|
|
|
}
|
2017-08-30 03:54:11 +00:00
|
|
|
|
|
|
|
public static toggleInArray(item, array)
|
|
|
|
{
|
|
|
|
if ( array.indexOf(item) === -1 )
|
|
|
|
{
|
|
|
|
array.push(item);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
array.splice(array.indexOf(item), 1);
|
|
|
|
}
|
|
|
|
}
|
2017-07-28 08:29:29 +00:00
|
|
|
}
|