Skip to content

Commit

Permalink
adds method for getting difference
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank M. Taylor committed Sep 19, 2024
1 parent 87e8a4b commit 8fcbb94
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/functions.comparisons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import union from 'set.prototype.union';
import intersection from 'set.prototype.intersection';
// @ts-ignore
import symmetricDifference from 'set.prototype.symmetricDifference';
// @ts-ignore
import difference from 'set.prototype.difference';

union.shim();
intersection.shim();
symmetricDifference.shim();
difference.shim();

/** An array of items that occur in two iterables */
type Intersection = Array<string>;
Expand Down Expand Up @@ -89,6 +92,28 @@ function getDisjunctiveUnion(
return disjunctiveUnion2dArray;

}

/**
* @description returns the items unique only to the first iterable
* @param {Map|Array} iterable1 A map or array
* @param {Map|Array} iterable2 A map or array
* @returns {Array<string>} An array of arrays of the unique items. The first item is the first parameter, 2nd item second param
*/
function getDifference(
iterable1: Map<string, string> | Array<string>,
iterable2: Map<string, string> | Array<string>,
) {
const array1 = Array.isArray(iterable1) ? iterable1 : [...iterable1.keys()];
const array2 = Array.isArray(iterable2) ? iterable2 : [...iterable2.keys()];

const set1 = new Set(array1);
const set2 = new Set(array2);

const difference = set1.difference(set2);

return [...difference];
}

/** The type of way that two NGramSequences can be evaluated */
type SequenceComparisonType = 'intersection' | 'disjunctiveUnion';

Expand Down Expand Up @@ -116,6 +141,7 @@ export {
getIntersection,
getUnion,
getDisjunctiveUnion,
getDifference,
getComparison,
SequenceComparisonType,
SequenceComparison,
Expand Down
43 changes: 43 additions & 0 deletions test/ts/functions.comparisons.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
getIntersection,
getUnion,
getDisjunctiveUnion,
getDifference
} from '../../src/functions.comparisons';

describe('comparisons', () => {
Expand Down Expand Up @@ -137,6 +138,48 @@ describe('comparisons', () => {
expect(set2).toEqual(map2);
});
});
describe('difference', () => {
it('will get the difference of two maps', () => {
const map1 = new Map([
['he', 1],
['el', 1],
['ll', 1],
['lo', 1],
]);
const map2 = new Map([
['he', 1],
['el', 1],
['lp', 1],
['ps', 1],
]);

const difference = getDifference(map1, map2);
expect(difference.includes('ll')).toEqual(true);
expect(difference.includes('lo')).toEqual(true);
expect(difference.includes('he')).toEqual(false);
expect(difference.includes('el')).toEqual(false);
});
it('will get a difference of two arrays', () => {
const map1 = [
'he',
'el',
'll',
'lo',
];
const map2 = [
'he',
'el',
'lp',
'ps',
];

const difference = getDifference(map1, map2);
expect(difference.includes('ll')).toEqual(true);
expect(difference.includes('lo')).toEqual(true);
expect(difference.includes('he')).toEqual(false);
expect(difference.includes('el')).toEqual(false);;
});
});
});
describe('getUnion', () => {
it('turns tio and ion into tion', () => {
Expand Down

0 comments on commit 8fcbb94

Please sign in to comment.