45 lines
2.2 KiB
JavaScript
Raw Normal View History

2026-05-31 13:21:13 +02:00
const require_dropRightWhile = require("../../array/dropRightWhile.js");
const require_identity = require("../../function/identity.js");
const require_isArrayLike = require("../predicate/isArrayLike.js");
const require_property = require("../object/property.js");
const require_matches = require("../predicate/matches.js");
const require_matchesProperty = require("../predicate/matchesProperty.js");
//#region src/compat/array/dropRightWhile.ts
/**
* Removes elements from the end of an array until the predicate returns false.
*
* This function iterates over an array and drops elements from the end until the provided
* predicate function returns false. It then returns a new array with the remaining elements.
*
* @template T - The type of elements in the array.
* @param {ArrayLike<T> | null | undefined} arr - The array from which to drop elements.
* @param {(item: T, index: number, arr: T[]) => unknown} predicate - A predicate function that determines
* whether to continue dropping elements. The function is called with each element, index, and array, and dropping
* continues as long as it returns true.
* @returns {T[]} A new array with the elements remaining after the predicate returns false.
*
* @example
* const array = [3, 2, 1];
* const result = dropRightWhile(array, (item, index, arr) => index >= 1);
* // Returns: [3]
*/
function dropRightWhile(arr, predicate = require_identity.identity) {
if (!require_isArrayLike.isArrayLike(arr)) return [];
return dropRightWhileImpl(Array.from(arr), predicate);
}
function dropRightWhileImpl(arr, predicate) {
switch (typeof predicate) {
case "function": return require_dropRightWhile.dropRightWhile(arr, (item, index, arr) => Boolean(predicate(item, index, arr)));
case "object": if (Array.isArray(predicate) && predicate.length === 2) {
const key = predicate[0];
const value = predicate[1];
return require_dropRightWhile.dropRightWhile(arr, require_matchesProperty.matchesProperty(key, value));
} else return require_dropRightWhile.dropRightWhile(arr, require_matches.matches(predicate));
case "symbol":
case "number":
case "string": return require_dropRightWhile.dropRightWhile(arr, require_property.property(predicate));
}
}
//#endregion
exports.dropRightWhile = dropRightWhile;