All Downloads are FREE. Search and download functionalities are using the official Maven repository.

src.app.shared.tools.array-utils.ts Maven / Gradle / Ivy

The newest version!
/*
 * SPDX-FileCopyrightText: 2017-2024 Enedis
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 */

export function newInstance(collection: Array): Array {
    return [].concat(collection);
}

export function distinct(collection: Array,
                            equalityFunction: (val1: T, val2: T) => boolean = (val1, val2) => val1 === val2
): Array {
    const distinctValues = [];

    collection.forEach((value) => {
        if (!contains(distinctValues, value, equalityFunction)) {
            distinctValues.push(value);
        }
    });

    return distinctValues;
}

export function flatMap(collection: Array,
                              mappingFunction: (value: T) => Array
): Array {
    return collection.reduce((acc, x) => acc.concat(mappingFunction(x)), []);
}

export function contains(collection: Array, valueToSearch: T,
                            equalityFunction: (val1: T, val2: T) => boolean = (val1, val2) => val1 === val2
): boolean {
    for (let i = 0; i < collection.length; i++) {
        if (equalityFunction(collection[i], valueToSearch)) {
            return true;
        }
    }
    return false;
}

export function containsAll(collection1: Array, collection2: Array,
                               equalityFunction: (val1: T, val2: T) => boolean = (val1, val2) => val1 === val2
): boolean {
    let found = false;
    for (let i = 0; i < collection1.length; i++) {
        found = contains(collection2, collection1[i], equalityFunction);
        if (found === false) {
            return false;
        }
    }
    return found;
}


export function intersection(firstCollection: Array,
                                secondCollection: Array,
                                equalityFunction: (val1: T, val2: T) => boolean = (val1, val2) => val1 === val2
): Array {
    return distinct(
        firstCollection
            .concat(secondCollection)
    )
        .filter((item) =>
            contains(firstCollection, item, equalityFunction)
            && contains(secondCollection, item, equalityFunction)
        );
}

function compare(a: T, b: T): number {
    if (a < b) {
        return -1;
    }
    if (a > b) {
        return 1;
    }
    return 0;
}

export function sortByAndOrder(collection: Array, keyExtractor: (T) => any, reverseOrder: boolean): Array {
    collection.sort((a, b) => {
        if (reverseOrder) {
            return compare(keyExtractor(b), keyExtractor(a));
        }
        return compare(keyExtractor(a), keyExtractor(b));
    });
    return collection;
}

export function pairwise(list: Array): Array> {
    if (list.length < 2) { return []; }
    const first = list[0],
        rest = list.slice(1),
        pairs = rest.map(function (x) {
            return [first, x];
        });
    return pairs.concat(pairwise(rest));
}

function normalize(value) {
    if (Array.isArray(value)) {
        value = value.toString();
    }
    return value.toLowerCase ?
        value.toLowerCase().normalize('NFD').replace(/[\u0300-\u036f]/g, '') :
        '' + value;
}

export function match(item: any, query: string): boolean {
return !!item && normalize(item).indexOf(normalize(query)) !== -1
}

export function filterOnTextContent(input: any, filter: string, attr: any[]) {
    if (!input || !filter || !attr) { return input; }
    if (attr.length === 0) { return input; }

    return input.filter((item) => {
        return filter === undefined
            || filter === ''
            || attr.map(a => match(item[a], filter))
                .reduce((p, c) => p || c);
    });
}