Packages@accelint/coreArray
includes
Determines whether an array includes a specific element using strict equality
Usage
import { includes } from '@accelint/core';
// Check if array includes a value
const hasThree = includes(3)([1, 2, 3, 4, 5]);
// true
// Partial application
const hasApple = includes('apple');
hasApple(['banana', 'orange', 'apple']); // trueReference
function includes<T>(
x: T
): (arr: T[]) => booleanParameters
| Parameter | Type | Description |
|---|---|---|
x | T | The value to search for in the array. |
Returns
Returns a curried function that accepts an array and returns true if the array includes the value, false otherwise.
Type Parameters
T- The type of elements in the array and the value to search for
Examples
Example: Checking primitive values
import { includes } from '@accelint/core';
const numbers = [1, 2, 3, 4, 5];
includes(3)(numbers); // true
includes(10)(numbers); // false
const strings = ['hello', 'world'];
includes('hello')(strings); // trueExample: Strict equality comparison
import { includes } from '@accelint/core';
// Objects are compared by reference
const obj = { id: 1 };
const arr = [obj, { id: 2 }];
includes(obj)(arr); // true
includes({ id: 1 })(arr); // false (different object reference)Example: Type safety
import { includes } from '@accelint/core';
const values: (string | number)[] = ['a', 1, 'b', 2];
// Type-safe usage
includes('a')(values); // true
includes(1)(values); // trueExample: Composing with other functions
import { includes, filter } from '@accelint/core';
const validIds = [1, 2, 3, 4, 5];
const items = [
{ id: 1, name: 'A' },
{ id: 6, name: 'B' },
{ id: 3, name: 'C' }
];
const filterValid = filter((item: typeof items[0]) =>
includes(item.id)(validIds)
);
filterValid(items);
// [{ id: 1, name: 'A' }, { id: 3, name: 'C' }]Good to know: This function uses strict equality (
===) for comparison. For object comparisons, it checks reference equality, not deep equality.