Skip to content

Commit

Permalink
feat: difference and occurrences
Browse files Browse the repository at this point in the history
  • Loading branch information
davguij committed Jun 19, 2020
1 parent a15282b commit 04f561c
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/difference.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
20 changes: 20 additions & 0 deletions src/difference.ts
Original file line number Diff line number Diff line change
@@ -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<T>(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)));
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ export * from './random';
export * from './sample';
export * from './shuffle';
export * from './omit';
export * from './occurrences';
export * from './difference';
16 changes: 16 additions & 0 deletions src/occurrences.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
13 changes: 13 additions & 0 deletions src/occurrences.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 04f561c

Please sign in to comment.