aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/utils/arrays.js
blob: aed951cdd70a4fd0d8890bc46c87583dfa1f0676 (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));
}