UNPKG

prray

Version:

'Promisified' Array, comes with async method supports(such as mapAsync). And it is compatible with normal array.

50 lines (49 loc) 1.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const prraypromise_1 = require("../src/prraypromise"); const prray_1 = require("../src/prray"); exports.isGte3Async = (v) => delay(100).then(() => v >= 3); exports.isGte3 = (v) => v >= 3; exports.isEven = (i) => i % 2 === 0; exports.isEvenAsync = (i) => delay(100).then(() => i % 2 === 0); exports.addOneAsync = (i) => delay(100).then(() => i + 1); exports.addOne = (i) => i + 1; /** * Returns a promise that will resolved after special time. */ function delay(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } exports.delay = delay; /** * Generate array with random numbers in special length. */ function genRandArr(len = 5) { const ret = []; for (let i = 0; i < len; i++) { ret.push(Math.random()); } return ret; } exports.genRandArr = genRandArr; function timer() { const startAt = Date.now(); return () => { return Date.now() - startAt; }; } exports.timer = timer; /** * Returns a prraypromise resolved with special array. */ function toPrrayPromise(arr) { if (arr instanceof prray_1.Prray) { return prraypromise_1.prraypromise(Promise.resolve(arr)); } return prraypromise_1.prraypromise(Promise.resolve(prray_1.prray(arr))); } exports.toPrrayPromise = toPrrayPromise; function isClose(n1, n2, opt = { threshold: 100 }) { return Math.abs(n1 - n2) < opt.threshold; } exports.isClose = isClose;