/**
 * @module ext/array.js
 * @author Krzyszot Putyra
 * @date Oct 13, 2021
 * @description A collection of functions that manipulate JavaScript arrays
 */

/**
 * Removes elements from an array that do not satisfy the provided test
 * and performs an action on them.
 * @function
 * @param {Array} arr           An array to be filtered
 * @param {Function} tester     Must return false for elements to be removed
 * @returns {number}            The number of removed elements
 */
export const filterWithin = (arr, tester) => {
    let i=0;
    let s=0;
    let total = arr.length;
    while (i < total) {
        if (tester(arr[i]))
            arr[s++] = arr[i];
        i++;
    }
    arr.length = s;
    return total-s;
};


/**
 * Removes elements from an array that satisfy the provided test
 * @function
 * @param {Array} arr           An array to be filtered
 * @param {Function} tester     Must return false for elements to be removed
 * @returns {number}            The number of removed elements
 */
 export const removeWithin = (arr, tester) => {
    let i=0;
    let s=0;
    let total = arr.length;
    while (i < total) {
        if (!tester(arr[i]))
            arr[s++] = arr[i];
        i++;
    }
    arr.length = s;
    return total-s;
};
