diff --git a/src/data/array/index.ts b/src/data/array/index.ts index 88e2e77..71bb001 100644 --- a/src/data/array/index.ts +++ b/src/data/array/index.ts @@ -216,3 +216,13 @@ export const find = (list: A[], cb: FindFunc): Maybe => { */ export const isEqual = (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 = (list1: A[], list2: A[]): A[] => + [...list1, ...list2].filter( + value => !contains(list1, value) || !contains(list2, value) + ); diff --git a/test/data/array_test.ts b/test/data/array_test.ts index cadb01d..3aa7c3a 100644 --- a/test/data/array_test.ts +++ b/test/data/array_test.ts @@ -18,7 +18,8 @@ import { flatten, concat, find, - isEqual + isEqual, + diff } from '../../src/data/array'; describe('array', () => { @@ -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([]); + }); + }); });