super-utils-plus
Version:
A superior alternative to Lodash with improved performance, TypeScript support, and developer experience
306 lines (305 loc) • 9.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.find = find;
exports.findLast = findLast;
exports.findIndex = findIndex;
exports.findLastIndex = findLastIndex;
const is_1 = require("../utils/is");
/**
* Iterates over elements of collection, returning the first element
* predicate returns truthy for.
*
* @param collection - The collection to inspect
* @param predicate - The function invoked per iteration
* @param fromIndex - The index to search from
* @returns The matched element, else undefined
*
* @example
* ```ts
* const users = [
* { 'user': 'barney', 'age': 36, 'active': true },
* { 'user': 'fred', 'age': 40, 'active': false },
* { 'user': 'pebbles', 'age': 1, 'active': true }
* ];
*
* find(users, function(o) { return o.age < 40; });
* // => object for 'barney'
*
* // The `_.matches` iteratee shorthand.
* find(users, { 'age': 1, 'active': true });
* // => object for 'pebbles'
*
* // The `_.matchesProperty` iteratee shorthand.
* find(users, ['active', false]);
* // => object for 'fred'
*
* // The `_.property` iteratee shorthand.
* find(users, 'active');
* // => object for 'barney'
* ```
*/
function find(collection, predicate, fromIndex = 0) {
if (!collection || !collection.length) {
return undefined;
}
// Convert predicate to a function if it's not already one
let predicateFn;
if (typeof predicate === 'function') {
// Function predicate
predicateFn = predicate;
}
else if ((0, is_1.isArray)(predicate) && predicate.length === 2) {
// Property and value pair
const [prop, value] = predicate;
predicateFn = (item) => {
return (0, is_1.isObject)(item) && item[prop] === value;
};
}
else if ((0, is_1.isObject)(predicate)) {
// Object matching
predicateFn = (item) => {
if (!(0, is_1.isObject)(item))
return false;
const objItem = item;
const objPred = predicate;
for (const key in objPred) {
if (objItem[key] !== objPred[key]) {
return false;
}
}
return true;
};
}
else if (typeof predicate === 'string') {
// Property name
const prop = predicate;
predicateFn = (item) => {
return (0, is_1.isObject)(item) && Boolean(item[prop]);
};
}
else {
// Default to identity function
predicateFn = Boolean;
}
const startIndex = Math.max(0, fromIndex);
const endIndex = collection.length;
for (let i = startIndex; i < endIndex; i++) {
if (predicateFn(collection[i], i, collection)) {
return collection[i];
}
}
return undefined;
}
/**
* This method is like find except that it iterates over elements of
* collection from right to left.
*
* @param collection - The collection to inspect
* @param predicate - The function invoked per iteration
* @param fromIndex - The index to search from
* @returns The matched element, else undefined
*
* @example
* ```ts
* findLast([1, 2, 3, 4], function(n) {
* return n % 2 === 1;
* });
* // => 3
* ```
*/
function findLast(collection, predicate, fromIndex) {
if (!collection || !collection.length) {
return undefined;
}
// Convert predicate to a function if it's not already one
let predicateFn;
if (typeof predicate === 'function') {
// Function predicate
predicateFn = predicate;
}
else if ((0, is_1.isArray)(predicate) && predicate.length === 2) {
// Property and value pair
const [prop, value] = predicate;
predicateFn = (item) => {
return (0, is_1.isObject)(item) && item[prop] === value;
};
}
else if ((0, is_1.isObject)(predicate)) {
// Object matching
predicateFn = (item) => {
if (!(0, is_1.isObject)(item))
return false;
const objItem = item;
const objPred = predicate;
for (const key in objPred) {
if (objItem[key] !== objPred[key]) {
return false;
}
}
return true;
};
}
else if (typeof predicate === 'string') {
// Property name
const prop = predicate;
predicateFn = (item) => {
return (0, is_1.isObject)(item) && Boolean(item[prop]);
};
}
else {
// Default to identity function
predicateFn = Boolean;
}
const startIndex = fromIndex !== undefined ? Math.min(fromIndex, collection.length - 1) : collection.length - 1;
for (let i = startIndex; i >= 0; i--) {
if (predicateFn(collection[i], i, collection)) {
return collection[i];
}
}
return undefined;
}
/**
* This method is like find except that it returns the index of the first
* element predicate returns truthy for, instead of the element itself.
*
* @param array - The array to inspect
* @param predicate - The function invoked per iteration
* @param fromIndex - The index to search from
* @returns The index of the found element, else -1
*
* @example
* ```ts
* const users = [
* { 'user': 'barney', 'active': false },
* { 'user': 'fred', 'active': false },
* { 'user': 'pebbles', 'active': true }
* ];
*
* findIndex(users, function(o) { return o.user === 'barney'; });
* // => 0
* ```
*/
function findIndex(array, predicate, fromIndex = 0) {
if (!array || !array.length) {
return -1;
}
// Convert predicate to a function if it's not already one
let predicateFn;
if (typeof predicate === 'function') {
// Function predicate
predicateFn = predicate;
}
else if ((0, is_1.isArray)(predicate) && predicate.length === 2) {
// Property and value pair
const [prop, value] = predicate;
predicateFn = (item) => {
return (0, is_1.isObject)(item) && item[prop] === value;
};
}
else if ((0, is_1.isObject)(predicate)) {
// Object matching
predicateFn = (item) => {
if (!(0, is_1.isObject)(item))
return false;
const objItem = item;
const objPred = predicate;
for (const key in objPred) {
if (objItem[key] !== objPred[key]) {
return false;
}
}
return true;
};
}
else if (typeof predicate === 'string') {
// Property name
const prop = predicate;
predicateFn = (item) => {
return (0, is_1.isObject)(item) && Boolean(item[prop]);
};
}
else {
// Default to identity function
predicateFn = Boolean;
}
const startIndex = Math.max(0, fromIndex);
const endIndex = array.length;
for (let i = startIndex; i < endIndex; i++) {
if (predicateFn(array[i], i, array)) {
return i;
}
}
return -1;
}
/**
* This method is like findIndex except that it iterates over elements
* of collection from right to left.
*
* @param array - The array to inspect
* @param predicate - The function invoked per iteration
* @param fromIndex - The index to search from
* @returns The index of the found element, else -1
*
* @example
* ```ts
* const users = [
* { 'user': 'barney', 'active': true },
* { 'user': 'fred', 'active': false },
* { 'user': 'pebbles', 'active': false }
* ];
*
* findLastIndex(users, function(o) { return o.user === 'pebbles'; });
* // => 2
* ```
*/
function findLastIndex(array, predicate, fromIndex) {
if (!array || !array.length) {
return -1;
}
// Convert predicate to a function if it's not already one
let predicateFn;
if (typeof predicate === 'function') {
// Function predicate
predicateFn = predicate;
}
else if ((0, is_1.isArray)(predicate) && predicate.length === 2) {
// Property and value pair
const [prop, value] = predicate;
predicateFn = (item) => {
return (0, is_1.isObject)(item) && item[prop] === value;
};
}
else if ((0, is_1.isObject)(predicate)) {
// Object matching
predicateFn = (item) => {
if (!(0, is_1.isObject)(item))
return false;
const objItem = item;
const objPred = predicate;
for (const key in objPred) {
if (objItem[key] !== objPred[key]) {
return false;
}
}
return true;
};
}
else if (typeof predicate === 'string') {
// Property name
const prop = predicate;
predicateFn = (item) => {
return (0, is_1.isObject)(item) && Boolean(item[prop]);
};
}
else {
// Default to identity function
predicateFn = Boolean;
}
const startIndex = fromIndex !== undefined ? Math.min(fromIndex, array.length - 1) : array.length - 1;
for (let i = startIndex; i >= 0; i--) {
if (predicateFn(array[i], i, array)) {
return i;
}
}
return -1;
}