UNPKG

@utilify/array

Version:

A collection of utility functions for array manipulation and transformation. Simplifies common operations like chunking, flattening, shuffling, finding differences, and more.

2 lines (1 loc) 1.23 kB
"use strict";function t(t){return[...new Set(t)]}exports.after=function(t,n){return t.slice(n+1)},exports.before=function(t,n){return t.slice(0,n)},exports.chunk=function(t,n){const e=[];for(let r=0;r<t.length;r+=n)e.push(t.slice(r,r+n));return e},exports.compact=function(t){return t.filter((t=>![void 0,null,!1,NaN,0,0n,""].includes(t)))},exports.difference=function(t,n){return t.filter((t=>!n.includes(t)))},exports.first=function(t,n=1){return t.slice(0,n)},exports.flattenArr=function(t,n=1/0){return t.flat(n)},exports.isIterable=function(t){return null!=t&&"function"==typeof t?.[Symbol.iterator]},exports.last=function(t,n=1){return t.slice(-n)},exports.rotate=function(t,n){const e=t.length;return n=(n%e+e)%e,t.slice(n).concat(t.slice(0,n))},exports.sample=function(t){return t[Math.floor(Math.random()*t.length)]},exports.sanitizeArr=function(t,n,e){const r=[];for(const o of t)n.includes(o)?void 0!==e&&r.push(e):r.push(o);return r},exports.shuffle=function(t){for(let n=t.length-1;n>0;n--){const e=Math.floor(Math.random()*(n+1));[t[n],t[e]]=[t[e],t[n]]}return t},exports.swap=function(t,n,e){return[t[n],t[e]]=[t[e],t[n]],t},exports.union=function(...n){return t(n.reduce(((t,n)=>[...t,...n]),[]))},exports.unique=t;