UNPKG

@tidyjs/tidy

Version:

Tidy up your data with JavaScript, inspired by dplyr and the tidyverse

38 lines (35 loc) 1.15 kB
import { shuffle } from 'd3-array'; import { arrange, desc } from './arrange.js'; function slice(start, end) { const _slice = (items) => items.slice(start, end); return _slice; } const sliceHead = (n) => slice(0, n); const sliceTail = (n) => slice(-n); function sliceMin(n, orderBy) { const _sliceMin = (items) => arrange(orderBy)(items).slice(0, n); return _sliceMin; } function sliceMax(n, orderBy) { const _sliceMax = (items) => typeof orderBy === "function" ? arrange(orderBy)(items).slice(-n).reverse() : arrange(desc(orderBy))(items).slice(0, n); return _sliceMax; } function sliceSample(n, options) { options = options != null ? options : {}; const {replace} = options; const _sliceSample = (items) => { if (!items.length) return items.slice(); if (replace) { const sliced = []; for (let i = 0; i < n; ++i) { sliced.push(items[Math.floor(Math.random() * items.length)]); } return sliced; } return shuffle(items.slice()).slice(0, n); }; return _sliceSample; } export { slice, sliceHead, sliceMax, sliceMin, sliceSample, sliceTail }; //# sourceMappingURL=slice.js.map