@santi100/array-intersection
v0.0.1
Published
Santi's Array Intersection Library: Set theory applied!
Downloads
21
Maintainers
Readme
Santi's Array Intersection Library
What's this?
This is a fast, lightweight library for doing intersection of arrays, a common requirement in things like:
Data Analysis: When working with datasets, finding the intersection of arrays can help identify common elements or patterns across different datasets. For example, you might want to determine the common set of customers who appear in both sales and marketing databases.
Database Queries: In database management systems, intersection operations can be used to retrieve records that satisfy multiple conditions. For instance, you can find the intersection of results from different database queries to obtain a combined set of matching records.
Social Networks: In social network analysis, the intersection of arrays can be employed to discover mutual connections or shared interests between individuals. It can assist in identifying common friends, followers, or communities within a social network.
Set Operations: The intersection of arrays is a fundamental operation in set theory. It helps define relationships between different sets and allows for operations like finding common elements, calculating overlaps, or determining shared characteristics.
Collaborative Filtering: In recommendation systems, the intersection of arrays can be used to find similar items or preferences among users. By identifying shared interests or overlapping preferences, collaborative filtering algorithms can provide personalized recommendations based on the intersection of users' preferences.
Genetic Research: In genetics, the intersection of genetic profiles or DNA sequences can be employed to identify shared genetic markers or regions among individuals or populations. This information can be useful in studying genetic traits, inheritance patterns, or evolutionary relationships.
These are just a few examples!
Installation
- Via NPM:
npm install @santi100/array-intersection
- Via Yarn:
yarn add @santi100/array-intersection
- Via PNPM:
pnpm install @santi100/array-intersection
API
function arrayIntersection<T = unknown>(arr1: T[], arr2: T[]): T[];
Finds all common items between two arrays. | Name | Type | Description | Optional? | Default | |------------|-------------|---------------------------------------------------------------------|-----------|---------| |arr1
|T[]
| The first array from which to generate an intersection witharr2
. | No | N/A | |arr2
|T[]
| The first array from which to generate an intersection witharr1
. | No | N/A |Returns an array containing all items shared by both arrays.
function deepArrayIntersection<T = unknown>(arr1: T[], arr2: T[]): T[];
Finds all deeply common items between two arrays. Deep equality is powered by@santi100/equal-lib
.| Name | Type | Description | Optional? | Default | |------------|-------------|---------------------------------------------------------------------|-----------|---------| |
arr1
|T[]
| The first array from which to generate an intersection witharr2
. | No | N/A | |arr2
|T[]
| The first array from which to generate an intersection witharr1
. | No | N/A |Returns an array containing all items shared by both arrays.
Usage
import { arrayIntersection, deepArrayIntersection } from '@santi100/array-intersection'; // ESM
const { arrayIntersection, deepArrayIntersection } = require('@santi100/array-intersection'); // CJS
// Usage of arrayIntersection
const array1 = ['Alice', 'Bob', 'Charles'];
const array2 = ['Dan', 'Ethan', 'Bob', 'Alice'];
const intersection = arrayIntersection(array1, array2); // => ['Alice', 'Bob']
// Usage of deepArrayIntersection
const array3 = [{ foo: 1 }, { bar: 2 }, { baz: 3 }];
const array4 = [{ bar: 2 }, { baz: 3 }, { qux: 4 }];
const objectIntersection = deepArrayIntersection(array3, array4); // => [{ bar: 2 }, { baz: 3 }]
Contribute
Wanna contribute? File an issue or pull request! Look at the contribution instructions and make sure you follow the contribution Code of Conduct.
Disclaimers
*Hasn't been tested in an actual ES3 environment. Feel free to open an issue or pull request if you find any non-ES3 thing. See "Contribute" for instructions on how to do so.
^The source code is just a few kilobytes in size.