Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
13 kB
1
Indexable
Never
/*******************************************************************************
*
*  Copyright (C) Altran ACT S.A.S. 2018,2019,2020,2021,2022.  All rights reserved.
*
********************************************************************************/

import { Injectable } from '@angular/core';
import { ConfigConstant } from '../../../app.constant';
function _window(): any {
    return window;
}

@Injectable()
export class WindowRef {
    get window(): any {
        return _window();
    }
}
interface SortConfiguration {
    sortOrder: string;
    sortKey: string;
}

@Injectable()
export class CommonUtilitiesLib {
    /**
     * Used for sorting of an array
     * SortConfiguration contain sortKey, sortOrder
     *
     * @param {any} arrayObject
     * @param {SortConfiguration} sortConf
     * @memberof CommonUtilitiesLib
     */
    sort(arrayObject, sortConf: SortConfiguration) {
        arrayObject.sort(function (pivotElement, element) {
            if (sortConf.sortKey === '') {
                if (isNaN(pivotElement) && isNaN(element)) {
                    if (sortConf.sortOrder === ConfigConstant.getData('grid').ascSort) {
                        if (pivotElement.toLowerCase() > element.toLowerCase()) {
                            return 1;
                        }
                        if (pivotElement.toLowerCase() < element.toLowerCase()) {
                            return -1;
                        }
                        return 0;
                    } else {
                        if (pivotElement.toLowerCase() > element.toLowerCase()) {
                            return -1;
                        }
                        if (pivotElement.toLowerCase() < element.toLowerCase()) {
                            return 1;
                        }
                        return 0;
                    }
                } else {
                    if (sortConf.sortOrder === ConfigConstant.getData('grid').ascSort) {
                        if (pivotElement > element) {
                            return 1;
                        }
                        if (pivotElement < element) {
                            return -1;
                        }
                        return 0;
                    } else {
                        if (pivotElement > element) {
                            return -1;
                        }
                        if (pivotElement < element) {
                            return 1;
                        }
                        return 0;
                    }
                }
            } else {
                if (isNaN(pivotElement[sortConf.sortKey]) && isNaN(element[sortConf.sortKey])) {
                    if (sortConf.sortOrder === ConfigConstant.getData('grid').ascSort) {
                        if (pivotElement[sortConf.sortKey].toLowerCase() > element[sortConf.sortKey].toLowerCase()) {
                            return 1;
                        }
                        if (pivotElement[sortConf.sortKey].toLowerCase() < element[sortConf.sortKey].toLowerCase()) {
                            return -1;
                        }
                        return 0;
                    } else {
                        if (pivotElement[sortConf.sortKey].toLowerCase() > element[sortConf.sortKey].toLowerCase()) {
                            return -1;
                        }
                        if (pivotElement[sortConf.sortKey].toLowerCase() < element[sortConf.sortKey].toLowerCase()) {
                            return 1;
                        }
                        return 0;
                    }
                } else {
                    if (sortConf.sortOrder === ConfigConstant.getData('grid').ascSort) {
                        if (pivotElement[sortConf.sortKey] > element[sortConf.sortKey]) {
                            return 1;
                        }
                        if (pivotElement[sortConf.sortKey] < element[sortConf.sortKey]) {
                            return -1;
                        }
                        return 0;
                    } else {
                        if (pivotElement[sortConf.sortKey] > element[sortConf.sortKey]) {
                            return -1;
                        }
                        if (pivotElement[sortConf.sortKey] < element[sortConf.sortKey]) {
                            return 1;
                        }
                        return 0;
                    }
                }
            }
        });
    }

    parseKey(pivotElement, key: string) {
        const keyArray = key.split('.');

        for (let i = 0; i < keyArray.length; i++) {
            pivotElement = pivotElement[keyArray[i]];
        }
        if (pivotElement) {
            return pivotElement;
        } else {
            return '';
        }
    }
    contactSort(arrayObject, sortConf: SortConfiguration) {
        const that = this;
        if (sortConf.sortKey !== 'NETMAPNODES.operatingSystem') {
            arrayObject.sort(function (pivotElement, element) {
                if (sortConf.sortOrder === ConfigConstant.getData('grid').ascSort) {
                    if (that.parseKey(pivotElement, sortConf.sortKey).toLowerCase() >
                        that.parseKey(element, sortConf.sortKey).toLowerCase()) {
                        return 1;
                    }
                    if (that.parseKey(pivotElement, sortConf.sortKey).toLowerCase() <
                        that.parseKey(element, sortConf.sortKey).toLowerCase()) {
                        return -1;
                    }
                    return 0;
                } else {
                    if (that.parseKey(pivotElement, sortConf.sortKey).toLowerCase() >
                        that.parseKey(element, sortConf.sortKey).toLowerCase()) {
                        return -1;
                    }
                    if (that.parseKey(pivotElement, sortConf.sortKey).toLowerCase() <
                        that.parseKey(element, sortConf.sortKey).toLowerCase()) {
                        return 1;
                    }
                    return 0;
                }
            });
        }
    }

    groupBy(inputArray: Array<any>, key: string) {
        return inputArray.reduce(function (newObject, item) {
            (newObject[item[key]] = newObject[item[key]] || []).push(item);
            return newObject;
        }, {});
    }

    /**
     * Filter an array by a given Object
     * Object key must be in array
     *
     * @param {Array<any>} inputArray
     * @param {*} filterArray
     * @returns
     * @memberof CommonUtilitiesLib
     */
    filterArrayByMultipleArray(inputArray: Array<any>, filterArray: any) {
        let temp = inputArray.filter(function (arrayObject) {
            const filterKeys = Object.keys(filterArray);
            return filterKeys.every(eachKey => {
                return filterArray[eachKey].every(value => {
                    return arrayObject[eachKey].includes(value);
                });
            });
        });
        return temp;
    }

    /**
     * Get the different keys of two object
     *
     * @param {*} baseObject
     * @param {*} newObject
     * @returns {*}
     * @memberof CommonUtilitiesLib
     */
    getObjectDifference(baseObject: any, newObject: any): any {
        const mergedObject = Object.assign(baseObject, newObject);
        return Object.keys(mergedObject).reduce((map, key) => {
            if (!newObject.hasOwnProperty(key)) {
                map[key] = mergedObject[key];
            }
            return map;
        }, {});
    }

    /**
     * Get all key and value pair which has changed in newobject from the base reference
     *
     * @param {*} baseObject
     * @param {*} newObject
     * @returns {*}
     * @memberof CommonUtilitiesLib
     */
    getObjectDirtyKeyRevised(baseObject: any, newObject: any): any {
        return Object.keys(newObject).reduce(function (map, key) {
            if ((baseObject[key] || newObject[key]) && newObject[key] !== baseObject[key]) {
                map[key] = newObject[key];
            }
            return map;
        }, {});
    }

    getObjectDirtyKey(baseObject: any, newObject: any): any {
        return Object.keys(newObject).reduce(function (map, key) {
            if ((baseObject[key] || newObject[key]) && newObject[key] !== baseObject[key]) {
                map[key] = newObject[key];
            }
            return map;
        }, {});
    }

    getObjectDirtyKeyInherited(baseObject, newObject, map) {
        return Object.keys(newObject).reduce(function (map, key) {
            if (newObject[key] && baseObject[key] && Object.keys(newObject[key]).length && Object.keys(baseObject[key]).length) {
                this.getObjectDirtyKeyInherited(newObject[key], baseObject[key], map);
            } else {
                if (newObject[key] !== baseObject[key]) {
                    map[key] = newObject[key];
                }
            }
            return map;
        }, map);
    }



    /**
     * copy the element by copy not reference
     *
     * @param {*} oldObject
     * @returns {*}
     * @memberof CommonUtilitiesLib
     */
    copy(oldObject: any): any {
        return JSON.parse(JSON.stringify(oldObject));
    }

    /**
     * Check object is empty or not
     *
     * @param {*} oldObject
     * @returns {*}
     * @memberof CommonUtilitiesLib
     */
    isEmptyObject(oldObject: any): any {
        return Object.keys(oldObject).length === 0 && oldObject.constructor === Object;
    }

    formatUserPassword(Object: any, id: string, pass: string) {
        if (Object[id] && Object[pass]) {
            return (Object[id] + ',' + Object[pass]);
        }
        return '';
    }

    checkInPartners(checkString: string, key: string, partners: any[]) {
        for (let i = 0; i < partners.length; i++) {
            if (partners[i][key] === checkString) {
                return true;
            }
        }
        return false;
    }


    shortByValue(list, val, keyGetter) {
        let sortValArray = [];
        let rawArray = [];
        list.forEach((item) => {
            let key = keyGetter(item);
            if (key === val) {
                sortValArray.push(item);
            } else {
                rawArray.push(item);
            }
        });
        return sortValArray.concat(rawArray);
    }

    groupBySessionVal(list, val, keyGetter) {
        let map = [];
        let keyArray = [];
        let sortValArray = [];
        let rawArray = [];
        list.forEach((item) => {
            let key = keyGetter(item);
            if (!map[key]) {
                map[key] = [item];
                keyArray.push(key);
            } else {
                map[key].push(item);
            }
        });

        for (let i = 0; i < keyArray.length; i++) {
            for (let j = 0; j < map[keyArray[i]].length; j++) {
                    if (keyArray[i] === val.toUpperCase()) {
                        sortValArray.push(map[keyArray[i]][j]);
                    } else {
                        rawArray.push(map[keyArray[i]][j]);
                    }
            }
        }
        return sortValArray.concat(rawArray);
    }

    groupBySortKeyVal(list, val, keyGetter) {
        let map = [];
        let keyArray = [];
        let sortValArray = [];
        let rawArray = [];
        list.forEach((item) => {
            let key = keyGetter(item);
            if (!map[key]) {
                map[key] = [item];
                keyArray.push(key);
            } else {
                map[key].push(item);
            }
        });
        for (let i = 0; i < keyArray.length; i++) {
            for (let j = 0; j < map[keyArray[i]].length; j++) {
                if (Array.isArray(val)) {
                    if (!val.includes(keyArray[i])) {
                        sortValArray.push(map[keyArray[i]][j]);
                    } else {
                        rawArray.push(map[keyArray[i]][j]);
                    }
                } else {

                    if (keyArray[i] === val) {
                        sortValArray.push(map[keyArray[i]][j]);
                    } else {
                        rawArray.push(map[keyArray[i]][j]);
                    }
                }
            }
        }
        return sortValArray.concat(rawArray);
    }

    copyToClipboard(elementId) {
        const range = document.createRange();
        range.selectNode(document.getElementById(elementId));
        window.getSelection().removeAllRanges();
        window.getSelection().addRange(range);
        document.execCommand('copy', true, null);
    }
}