Skip to content

Commit

Permalink
close #137: add array diff function
Browse files Browse the repository at this point in the history
  • Loading branch information
metasansana committed Jul 21, 2024
1 parent fc366dc commit 243f1cd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/data/array/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,13 @@ export const find = <A>(list: A[], cb: FindFunc<A>): Maybe<A> => {
*/
export const isEqual = <A>(list1: A[], list2: A[]): boolean =>
list1.every((val, idx) => list2[idx] === val);

/**
* diff returns a simple difference between two arrays.
*
* Comparison is done via contains()
*/
export const diff = <A>(list1: A[], list2: A[]): A[] =>
[...list1, ...list2].filter(
value => !contains(list1, value) || !contains(list2, value)
);
17 changes: 16 additions & 1 deletion test/data/array_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import {
flatten,
concat,
find,
isEqual
isEqual,
diff
} from '../../src/data/array';

describe('array', () => {
Expand Down Expand Up @@ -267,4 +268,18 @@ describe('array', () => {
assert(isEqual([1, 2, 3], [1, 3, 2])).false();
});
});

describe('diff', () => {
it('should produce the diff elements ', () => {
assert(diff([1, 2, 3], [2, 3, 4])).equate([1, 4]);
});

it('should work with empty arrays ', () => {
assert(diff([1, 2, 3], [])).equate([1, 2, 3]);

assert(diff([], [2, 3, 4])).equate([2, 3, 4]);

assert(diff([], [])).equate([]);
});
});
});

0 comments on commit 243f1cd

Please sign in to comment.