aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/utils/arrays.js
blob: 69ef242c7d1cabbae1f175b0e00bed89bbc7f2f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// ==========================================================================
// Array utils
// ==========================================================================

import is from './is';

// Remove duplicates in an array
export function dedupe(array) {
    if (!is.array(array)) {
        return array;
    }

    return array.filter((item, index) => array.indexOf(item) === index);
}

// Get the closest value in an array
export function closest(array, value) {
    if (!is.array(array) || !array.length) {
        return null;
    }

    return array.reduce((prev, curr) => (Math.abs(curr - value) < Math.abs(prev - value) ? curr : prev));
}