array.js

define(["require", "exports"], function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.toggleArrayValue = exports.removeTwins = exports.removeFromArray = exports.removeDuplicates = exports.merge = void 0;
    /**
     * @function merge a function to merge a groups of values in one array
     *
     * @param  {...any} args any data type to merge in one array
     *
     * @returns Array of merged elements
     */
    function merge(...args) {
        const result = [];
        for (const arg of args) {
            if (Array.isArray(arg)) {
                result.push(...arg);
            }
            else {
                result.push(arg);
            }
        }
        return result;
    }
    exports.merge = merge;
    /**
     * @function removeDuplicates
     * @description A simple function to remove duplicated elements between two arrays
     *
     * @param {Array} array The original array
     * @param {Array} toRemove the Array of elements to remove
     *
     * @returns New Array without duplicates element
     */
    function removeDuplicates(array) {
        const newArray = [];
        array.forEach((el) => {
            if (!newArray.includes(el)) {
                newArray.push(el);
            }
        });
        return newArray;
    }
    exports.removeDuplicates = removeDuplicates;
    /**
     * @function removeFromArray a function to remove an element array
     *
     * @param  {array} list the array to remove the element from it
     * @param  {any} toRemove any data type to remove
     *
     * @returns an Array of elements without removed value
     */
    function removeFromArray(list, toRemove) {
        return list.filter((el) => el !== toRemove);
    }
    exports.removeFromArray = removeFromArray;
    /**
     * @function removeTwins
     * @description A simple function to remove the element & it twins elements between two arrays
     *
     * @param {Array} array The original array
     * @param {Array} toRemove the Array of elements to remove
     *
     * @returns New Array without duplicates element
     */
    function removeTwins(array, toRemove = []) {
        let newArray = [];
        if (toRemove.length === 0) {
            array.forEach((el) => {
                if (!newArray.includes(el)) {
                    newArray.push(el);
                }
            });
        }
        else {
            toRemove.forEach((subEl) => {
                array = array.filter((el) => el !== subEl);
            });
            newArray = array;
        }
        return newArray;
    }
    exports.removeTwins = removeTwins;
    /**
     * @function toggleArrayValue function to add or remove a value from an array
     *
     * @param {Array} arr array to check value exists & make toggle
     * @param {String|Number} val value to make toggle
     *
     * @returns {Array} new array after toggling
     */
    function toggleArrayValue(arr, val) {
        const index = arr.indexOf(val);
        if (index > -1) {
            arr.splice(index, 1);
        }
        else {
            arr.push(val);
        }
        return arr;
    }
    exports.toggleArrayValue = toggleArrayValue;
});