Skip to content

Commit

Permalink
feat: added setContainsAnyValue()
Browse files Browse the repository at this point in the history
- updated signature of containsAnyValue to use two iterables
  • Loading branch information
dereekb committed Mar 4, 2022
1 parent b366c76 commit ea0ee9a
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 7 deletions.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
"prepare": "husky install"
},
"private": true,
"repository": {
"type": "git",
"url": "https://github.com/dereekb/dbcomponents"
},
"dependencies": {
"@angular/animations": "^13.0.0",
"@angular/cdk": "^13.0.0",
Expand Down Expand Up @@ -50,7 +54,7 @@
"lodash.isequal": "^4.5.0",
"mat-progress-buttons": "git+https://git@github.com/dereekb/mat-progress-buttons.git#60b0374a45644e8756f20b8d761738151ca3df64",
"ms": "^3.0.0-canary.1",
"ng-overlay-container": "git+https://git@github.com/dereekb/ng-overlay-container.git#24931e7094c4f8f76bda061d044426febf810a4f",
"ng-overlay-container": "^13.0.0",
"ngx-editor": "^12.1.1",
"ngx-infinite-scroll": "^10.0.1",
"ngx-mat-intl-tel-input": "^4.1.0",
Expand Down
12 changes: 8 additions & 4 deletions packages/util/src/lib/array/array.set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,15 @@ export function filterValuesFromSet<T>(values: T[], set: Set<T>, exclude = false
/**
* Returns true if the input array contains any value from the second array.
*/
export function containsAnyValue<T>(values: T[], valuesToFind: T[]): boolean {
export function containsAnyValue<T>(values: Iterable<T>, valuesToFind: Iterable<T>): boolean {
const set = new Set(valuesToFind);
return setContainsAnyValue(values, set);
return containsAnyValueFromSet(values, set);
}

export function setContainsAnyValue<T>(values: T[], valuesToFind: Set<T>): boolean {
return (values) ? values.findIndex((x) => valuesToFind.has(x)) !== -1 : false;
export function containsAnyValueFromSet<T>(values: Iterable<T>, valuesToFind: Set<T>): boolean {
return setContainsAnyValue(valuesToFind, values);
}

export function setContainsAnyValue<T>(valuesSet: Set<T>, valuesToFind: Iterable<T>): boolean {
return (valuesSet) ? Array.from(valuesToFind).findIndex((x) => valuesSet.has(x)) !== -1 : false;
}
67 changes: 65 additions & 2 deletions packages/util/src/lib/array/array.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { mergeIntoArray, range } from '@dereekb/util';
import { containsAnyValue, containsAnyValueFromSet, setContainsAnyValue } from '..';
import { takeLast } from './array';

describe('takeLast', () => {
Expand Down Expand Up @@ -73,7 +74,7 @@ describe('mergeIntoArray', () => {
it('should add the values from the second array into the target array in the same order.', () => {

const initial = [0];
const second = range({ start: 1, end: 5});
const second = range({ start: 1, end: 5 });

const target = [...initial];
const result = mergeIntoArray(target, second);
Expand All @@ -89,7 +90,7 @@ describe('mergeIntoArray', () => {
it('should add the values from the second and third array into the target array in the same order.', () => {

const initial = [0];
const second = range({ start: 1, end: 5});
const second = range({ start: 1, end: 5 });

const target = [...initial];
const result = mergeIntoArray(target, second, second); // second twice
Expand All @@ -107,3 +108,65 @@ describe('mergeIntoArray', () => {
});

});

describe('containsAnyValue', () => {

it('should return true if the array contains any value from the set.', () => {
const value = 'a';
const set = new Set([value]);

expect(set.has(value));
expect(containsAnyValue(set, [value])).toBe(true);
});

it('should return true if the array contains any value from the array.', () => {
const value = 'a';
const array = [value];

expect(array.indexOf(value) !== -1);
expect(containsAnyValue(array, [value])).toBe(false);
});

it('should return false if the array does not contain a value from the array.', () => {
const value = 'a';
const array = [value];
expect(containsAnyValue(array, [])).toBe(false);
});

});

describe('containsAnyValueFromSet', () => {

it('should return true if the array contains any value from the set.', () => {
const value = 'a';
const set = new Set([value]);

expect(set.has(value));
expect(containsAnyValueFromSet([value], set)).toBe(true);
});

it('should return false if the array does not contain a value from the set.', () => {
const value = 'a';
const set = new Set();
expect(containsAnyValueFromSet([value], set)).toBe(false);
});

});

describe('setContainsAnyValue', () => {

it('should return true if the set contains the value.', () => {
const value = 'a';
const set = new Set([value]);

expect(set.has(value));
expect(setContainsAnyValue(set, [value])).toBe(true);
});

it('should return false if the set does not contain the value.', () => {
const value = 'a';
const set = new Set();
expect(setContainsAnyValue(set, [value])).toBe(false);
});

});

0 comments on commit ea0ee9a

Please sign in to comment.