From 04f561c8c9a68921e08eea6358a7fb0d8c36c93d Mon Sep 17 00:00:00 2001 From: David Guijarro Date: Fri, 19 Jun 2020 17:05:43 +0200 Subject: [PATCH] feat: difference and occurrences --- src/difference.spec.ts | 33 +++++++++++++++++++++++++++++++++ src/difference.ts | 20 ++++++++++++++++++++ src/index.ts | 2 ++ src/occurrences.spec.ts | 16 ++++++++++++++++ src/occurrences.ts | 13 +++++++++++++ 5 files changed, 84 insertions(+) create mode 100644 src/difference.spec.ts create mode 100644 src/difference.ts create mode 100644 src/occurrences.spec.ts create mode 100644 src/occurrences.ts diff --git a/src/difference.spec.ts b/src/difference.spec.ts new file mode 100644 index 0000000..e6c5f6d --- /dev/null +++ b/src/difference.spec.ts @@ -0,0 +1,33 @@ +import { difference } from './difference'; + +describe('difference', () => { + it('should return a new array the difference between two arrays', () => { + const a = ['a', 'b', 'c', 'd']; + const b = ['b', 'c']; + const e = ['a', 'd']; + + expect(difference(a, b)).toEqual(e); + }); + + it('should support second array being bigger than first', () => { + const a = ['b', 'c']; + const b = ['a', 'b', 'c', 'd']; + const e = ['a', 'd']; + + expect(difference(a, b)).toEqual(e); + }); + + it('should return empty array if both input arrays are equal', () => { + const a = ['a', 'b', 'c', 'd']; + const b = [...a]; + expect(difference(a, b)).toEqual([]); + }); + + it('should support arrays of objects', () => { + const a = [{ first: 'first', second: 'second' }, { third: 'third' }]; + const b = [{ third: 'third' }]; + const e = [{ first: 'first', second: 'second' }]; + + expect(difference(a, b)).toEqual(e); + }); +}); diff --git a/src/difference.ts b/src/difference.ts new file mode 100644 index 0000000..e11af9d --- /dev/null +++ b/src/difference.ts @@ -0,0 +1,20 @@ +import { isEqual } from './is-equal'; + +/** + * Takes two arrays and returns the difference between them as a new array. + * In this case, "difference" is to be understood as the elements of one array not present in the other. + * + * @param first First array. + * @param second Second array. + */ + +export function difference(first: T[], second: T[]): T[] { + if (isEqual(first, second)) { + return []; + } + + if (second.length > first.length) { + [second, first] = [first, second]; + } + return first.filter(f => !second.some(s => isEqual(f, s))); +} diff --git a/src/index.ts b/src/index.ts index 73a5627..f982223 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,3 +4,5 @@ export * from './random'; export * from './sample'; export * from './shuffle'; export * from './omit'; +export * from './occurrences'; +export * from './difference'; diff --git a/src/occurrences.spec.ts b/src/occurrences.spec.ts new file mode 100644 index 0000000..0735590 --- /dev/null +++ b/src/occurrences.spec.ts @@ -0,0 +1,16 @@ +import { occurrences } from './occurrences'; + +describe('occurrences', () => { + it('should count one character occurrences in a string', () => { + expect(occurrences('mississippi', 'i')).toBe(4); + }); + + it('should count substring occurrences in a string', () => { + expect(occurrences('mississippi', 'ssi')).toBe(2); + }); + + it('should return zero if any argument is an empty string', () => { + expect(occurrences('', 'e')).toBe(0); + expect(occurrences('mississippi', '')).toBe(0); + }); +}); diff --git a/src/occurrences.ts b/src/occurrences.ts new file mode 100644 index 0000000..4bc679b --- /dev/null +++ b/src/occurrences.ts @@ -0,0 +1,13 @@ +/** + * Returns the number of times that a substring `search` appears in a string `input`. + * + * @param input The string to search in. + * @param search The substring to be searched. + */ + +export function occurrences(input: string, search: string): number { + if (input === '' || search === '') { + return 0; + } + return (input.match(new RegExp(search, 'g')) || []).length; +}