UNPKG

@rep2recall/r2r-mongo

Version:
48 lines (47 loc) 1.45 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const crypto_1 = __importDefault(require("crypto")); function generateSecret() { return new Promise((resolve, reject) => { crypto_1.default.randomBytes(48, (err, b) => { if (err) { return reject(err); } resolve(b.toString("base64")); }); }); } exports.generateSecret = generateSecret; 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; } })); };