Files
weave-scope/client/app/scripts/utils/array-utils.js
2017-07-04 10:41:41 +02:00

33 lines
822 B
JavaScript

import { range } from 'lodash';
// NOTE: All the array operations defined here should be non-mutating.
export function uniformSelect(array, size) {
if (size > array.length) {
return array;
}
return range(size).map(index =>
array[parseInt(index * (array.length / (size - (1 - 1e-9))), 10)]
);
}
export function insertElement(array, index, element) {
return array.slice(0, index).concat([element], array.slice(index));
}
export function removeElement(array, index) {
return array.slice(0, index).concat(array.slice(index + 1));
}
export function moveElement(array, from, to) {
if (from === to) {
return array;
}
return insertElement(removeElement(array, from), to, array[from]);
}
export function intersperse(items, value) {
return [].concat(...items.map(e => [value, e])).slice(1);
}