UNPKG

@rep2recall/r2r-sqlite

Version:
33 lines (32 loc) 951 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); function shuffle(a) { for (let i = a.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [a[i], a[j]] = [a[j], a[i]]; } return a; } exports.shuffle = shuffle; function chunk(array, size) { var results = [], length = Math.ceil(array.length / size); for (var i = 0; i < length; i++) { results.push(array.slice(i * size, (i + 1) * size)); } return results; } exports.chunk = chunk; Array.prototype.distinctBy = function (mapFn) { const uniqueKeys = new Set(this.map(mapFn)); return this.filter((el) => uniqueKeys.has(mapFn(el))); }; Array.prototype.mapAsync = async function (mapFn) { return await Promise.all(this.map(async (value, index, array) => { try { return await mapFn(value, index, array); } catch (e) { throw e; } })); };