includes
collection
Checks if value is in collection.
Installation
Import
import { includes } from '@tulx/utils';Source Code
Implementation
/**
* Checks if value is in collection.
*
* @param collection - The collection to inspect.
* @param value - The value to search for.
* @param fromIndex - The index to search from.
* @returns Returns true if value is found, else false.
*
* @example
* ```ts
* includes([1, 2, 3], 1); // true
* includes([1, 2, 3], 1, 2); // false
* includes({ 'a': 1, 'b': 2 }, 1); // true
* includes('abcd', 'bc'); // true
* ```
*/
export function includes(
collection: readonly unknown[] | Record<string, unknown> | string,
value: unknown,
fromIndex: number = 0
): boolean {
if (typeof collection === 'string') {
return collection.includes(String(value), fromIndex);
}
if (Array.isArray(collection)) {
const startIndex =
fromIndex < 0 ? Math.max(collection.length + fromIndex, 0) : fromIndex;
for (let i = startIndex; i < collection.length; i++) {
if (collection[i] === value) {
return true;
}
}
return false;
}
const record = collection as Record<string, unknown>;
for (const key in record) {
if (Object.prototype.hasOwnProperty.call(record, key)) {
if (record[key] === value) {
return true;
}
}
}
return false;
}
Example
import { includes } from '@tulx/utils';
includes([1, 2, 3], 1); // true
includes([1, 2, 3], 1, 2); // false
includes({ 'a': 1, 'b': 2 }, 1); // true
includes('abcd', 'bc'); // trueRelated Functions
countBy
Creates an object composed of keys generated from the results of running each element of collection thru iteratee. The corresponding value of each key is the number of times the key was returned by iteratee.
each
Iterates over elements of collection and invokes iteratee for each element. The iteratee is invoked with three arguments: (value, index|key, collection).
eachRight
This method is like each except that it iterates over elements of collection from right to left.
every
Checks if predicate returns truthy for all elements of collection.
filter
Iterates over elements of collection, returning an array of all elements predicate returns truthy for.
find
Iterates over elements of collection, returning the first element predicate returns truthy for.