-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
28 lines (25 loc) · 817 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
'use strict';
const arrayHasValue = require('array-has-value');
/**
*
* @param {array} sourceArray | required |
* @param {object} opts | optional | controls the uniqueness of the array based on the value of caseSensitive and strictType
* @returns {array} deduplicated array
*/
const deduplicateArray = (sourceArray, opts) => {
if (!sourceArray) {
throw new Error('sourceArray is missing.')
}
if (!Array.isArray(sourceArray)) {
throw new Error('sourceArray must be an array.');
}
let deduplicatedArray = [];
for (let item of sourceArray) {
const isPresent = opts ? arrayHasValue(deduplicatedArray, item, opts) : arrayHasValue(deduplicatedArray, item);
if (!isPresent) {
deduplicatedArray.push(item);
}
}
return deduplicatedArray;
}
module.exports = deduplicateArray;