-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |