UNPKG

@aegenet/belt-array-duplicates

Version:

Strip duplicates from array / get duplicates of an array.

46 lines (45 loc) 1.19 kB
function u(n, s = {}) { if (n != null && n.length) { const t = n.slice(0); if (s.compare && !s.sorted) throw new Error("Invalid usage: compare function is only available with sorted array."); if (s.sorted) { let i = s.compare; if (!i) { const e = typeof t[0] == "number" ? [] : Object.keys(t[0]); e.length === 0 ? i = (l, r) => l == r : i = (l, r) => { let c = !0; for (let f = 0; f < e.length; f++) { const o = e[f]; if ((l ? JSON.stringify(l[o]) : null) !== (r ? JSON.stringify(r[o]) : null)) { c = !1; break; } } return c; }; } for (let e = t.length - 1; e > 0; e--) i(t[e], t[e - 1]) && t.splice(e, 1); return t; } else return t.filter((i, e) => t.indexOf(i) === e); } else return []; } function a(n) { const s = []; if (n) { const t = {}; for (let i = 0; i < n.length; i++) { const e = n[i]; e in t ? t[e]++ : t[e] = 0, t[e] === 1 && s.push(e); } } return s; } export { a as getDuplicates, u as stripDuplicates }; //# sourceMappingURL=index.mjs.map