Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add intersection, union, complement, fullIntersection, array utility methods #455

Merged
merged 7 commits into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/modules/esl-utils/misc/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,34 @@ export const uniq = <T> (arr: T[]): T[] => {
return result;
};

/** Crete an array filled with the range [0,..,N-1] */
/** Create an array filled with the range [0,..,N-1] */
export function range(n: number): number[];
/** Crete an array filled with values returned by the filler callback */
/** Create an array filled with values returned by the filler callback */
export function range<T>(n: number, filler: (i: number) => T): T[];
export function range(n: number, filler: (i: number) => any = identity): any[] {
const arr = Array(n);
let i = 0;
while (i < n) arr[i] = filler(i++);
return arr;
}

/** Create an array of unique values from two arrays*/
ala-n marked this conversation as resolved.
Show resolved Hide resolved
export function intersection(a: any, b: any): any[] {
const arr = new Set([...a, ...b]);
return [...arr];
ala-n marked this conversation as resolved.
Show resolved Hide resolved
}

/** Create an array from two arrays*/
ala-n marked this conversation as resolved.
Show resolved Hide resolved
export function union(a: any, b: any): any[] {
return [...a, ...b];
ala-n marked this conversation as resolved.
Show resolved Hide resolved
}

/** Creates an array of unique values from the first array that are not present in the second array*/
ala-n marked this conversation as resolved.
Show resolved Hide resolved
export function complement(a: any, b: any): any[] {
return a.filter((item: any) => !b.includes(item));
}

/** Check for elements from array B in array A*/
export function fullIntersection(a: any, b: any): boolean {
return a.length === 0 && b.length === 0 ? true : !b.filter((item: any) => !a.includes(item)).length;
}
38 changes: 37 additions & 1 deletion src/modules/esl-utils/misc/test/array.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {flat, range, tuple, uniq, wrap} from '../array';
import {flat, range, tuple, uniq, wrap, intersection, union, complement, fullIntersection} from '../array';

describe('misc/array helper tests', () => {
test('tuple', () => {
Expand Down Expand Up @@ -45,3 +45,39 @@ describe('misc/array helper tests', () => {
expect(range(9, (x) => x / 8)).toEqual([...Array(9).keys()].map((x) => x / 8))
});
});

test('intersection', () => {
ala-n marked this conversation as resolved.
Show resolved Hide resolved
const a = {a:3};
expect(intersection([],[])).toEqual([]);
expect(intersection([1],[1])).toEqual([1]);
expect(intersection([1, 2], [1, 2])).toEqual([1, 2]);
expect(intersection([1, [2, 3], a], [a, 1, [2, 3]])).toEqual([1, [2, 3], a, [2, 3]]);
expect(intersection([null, 1, 2, 3, null, [6]], [4, 5])).toEqual([null, 1, 2, 3, [6], 4, 5, ]);
});

test('union', () => {
const a = {a:3};
expect(union([],[])).toEqual([]);
expect(union([1],[1])).toEqual([1, 1]);
expect(union([1, 2], [1, 2])).toEqual([1, 2, 1, 2]);
expect(union([1, [2, 3], a], [a, 1, [2, 3]])).toEqual([1, [2, 3], a, a, 1, [2, 3]]);
expect(union([null, 1, 2, 3, null, [6]], [4, 5])).toEqual([null, 1, 2, 3, null, [6], 4, 5, ]);
});

test('complement', () => {
const a = {a: 3};
expect(complement([],[])).toEqual([]);
expect(complement([1],[1])).toEqual([]);
expect(complement([1, 2, 3], [1, 2])).toEqual([3]);
expect(complement([1, [2, 3], a], [a, 1, [2, 3]])).toEqual([[2, 3]]);
expect(complement([null, 1, 2, 3, null, [6]], [4, 5])).toEqual([null, 1, 2, 3, null, [6]]);
});

test('fullIntersection', () => {
const a = {a: 3};
expect(fullIntersection([],[])).toEqual(true);
expect(fullIntersection([1],[1])).toEqual(true);
expect(fullIntersection([1, 2, 3], [1, 2])).toEqual(true);
expect(fullIntersection([1, [2, 3], a], [a, 1, [2, 3]])).toEqual(false);
expect(fullIntersection([null, 1, 2, 3, null, [6]], [4, 5])).toEqual(false);
});