Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.

Commit b9d3026

Browse files
ccabralesKent C. Dodds
authored and
Kent C. Dodds
committed
feat(removeDuplicates): Add removeDuplicates function (#54)
closes #52
1 parent e5d1a83 commit b9d3026

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

Diff for: src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import validateEmail from './validateEmail'
1212
import hex2rgb from './hex2rgb'
1313
import isNullOrWhitespace from './is-null-or-whitespace'
1414
import startsWith from './startsWith'
15+
import removeDuplicates from './remove-duplicates'
1516

1617
export {
1718
flatten,
@@ -28,4 +29,5 @@ export {
2829
hex2rgb,
2930
isNullOrWhitespace,
3031
startsWith,
32+
removeDuplicates,
3133
}

Diff for: src/remove-duplicates.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export default removeDuplicates
2+
3+
/**
4+
* Original Source: http://stackoverflow.com/a/18328062
5+
*
6+
* This method will remove duplicate values in an array,
7+
* leaving one of each value.
8+
*
9+
* @param {Array} arr - The Array to remove all duplicates from.
10+
* @return {Array} - The Array without any duplicates.
11+
*/
12+
13+
function removeDuplicates(arr) {
14+
return arr.filter((item, index, inputArray) => {
15+
return inputArray.indexOf(item) === index
16+
})
17+
}

Diff for: test/remove-duplicates.test.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import test from 'ava'
2+
import {removeDuplicates} from '../src'
3+
4+
test('check array with duplicate strings', t => {
5+
const array = ['hello', 'world', 'world', 'array'];
6+
const expected = ['hello', 'world', 'array'];
7+
const actual = removeDuplicates(array);
8+
t.deepEqual(actual, expected);
9+
})
10+
11+
test('check array with duplicate ints', t => {
12+
const array = [1, 2, 3, 4, 4, 5, 5, 4, 1, 6];
13+
const expected = [1, 2, 3, 4, 5, 6];
14+
const actual = removeDuplicates(array);
15+
t.deepEqual(actual, expected);
16+
})
17+
18+
test('check array with no duplicates', t => {
19+
const array = [1, 3, 5, 7, 9];
20+
const expected = [1, 3, 5, 7, 9];
21+
const actual = removeDuplicates(array);
22+
t.deepEqual(actual, expected);
23+
})

0 commit comments

Comments
 (0)