UNPKG

prray

Version:

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

59 lines (58 loc) 3.42 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const ava_1 = require("ava"); const prray_1 = require("../src/prray"); const test_utils_1 = require("./test-utils"); const func1 = (pre, c) => pre + c; const funcAsync1 = (pre, c) => test_utils_1.delay(100).then(() => pre + c); const func2 = (pre, c) => { pre.push(c + 1); return pre; }; const funcAsync2 = (pre, c) => __awaiter(this, void 0, void 0, function* () { yield test_utils_1.delay(100); pre.push(c + 1); return pre; }); ava_1.default('prray reduceAsync 1', (t) => __awaiter(this, void 0, void 0, function* () { const p = prray_1.prray([1, 2, 3]); // FIXME: 无法确定 reduceAsync 的结果是不是数组,应该让 PrrayPromise 处理和兼容非数组的情况? // t.true(p.reduceAsync(func1) instanceof PrrayPromise) // t.true(p.reduceAsync(funcAsync1) instanceof PrrayPromise) t.deepEqual(yield p.reduceAsync(func1), [1, 2, 3].reduce(func1)); t.deepEqual(yield p.reduceAsync(funcAsync1), [1, 2, 3].reduce(func1)); t.deepEqual(yield p.reduceAsync(func1, 10), [1, 2, 3].reduce(func1, 10)); t.deepEqual(yield p.reduceAsync(funcAsync1, 10), [1, 2, 3].reduce(func1, 10)); })); ava_1.default('prray reduceAsync 2', (t) => __awaiter(this, void 0, void 0, function* () { const p = prray_1.prray([1, 2, 3]); // t.true(p.reduceAsync(func2, []) instanceof PrrayPromise) // t.true(p.reduceAsync(funcAsync2, []) instanceof PrrayPromise) t.deepEqual(yield p.reduceAsync(func2, []), [1, 2, 3].reduce(func2, [])); t.deepEqual(yield p.reduceAsync(func2, []), [1, 2, 3].reduce(func2, [])); })); ava_1.default('prraypromise reduceAsync 1', (t) => __awaiter(this, void 0, void 0, function* () { const pp = test_utils_1.toPrrayPromise([1, 2, 3]); // FIXME: 无法确定 reduceAsync 的结果是不是数组,应该让 PrrayPromise 处理和兼容非数组的情况? // t.true(pp.reduceAsync(func1) instanceof PrrayPromise) // t.true(pp.reduceAsync(funcAsync1) instanceof PrrayPromise) t.deepEqual(yield pp.reduceAsync(func1), [1, 2, 3].reduce(func1)); t.deepEqual(yield pp.reduceAsync(funcAsync1), [1, 2, 3].reduce(func1)); t.deepEqual(yield pp.reduceAsync(func1, 10), [1, 2, 3].reduce(func1, 10)); t.deepEqual(yield pp.reduceAsync(funcAsync1, 10), [1, 2, 3].reduce(func1, 10)); })); ava_1.default('prraypromise reduceAsync 2', (t) => __awaiter(this, void 0, void 0, function* () { const pp = test_utils_1.toPrrayPromise([1, 2, 3]); // t.true(pp.reduceAsync(func2, []) instanceof PrrayPromise) // t.true(pp.reduceAsync(funcAsync2, []) instanceof PrrayPromise) t.deepEqual(yield pp.reduceAsync(func2, []), [1, 2, 3].reduce(func2, [])); t.deepEqual(yield pp.reduceAsync(func2, []), [1, 2, 3].reduce(func2, [])); }));