UNPKG

5.26 kBJavaScriptView Raw
1"use strict";
2/**
3 * This file is part of the @egodigital/egoose distribution.
4 * Copyright (c) e.GO Digital GmbH, Aachen, Germany (https://www.e-go-digital.com/)
5 *
6 * @egodigital/egoose is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation, version 3.
9 *
10 * @egodigital/egoose is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18Object.defineProperty(exports, "__esModule", { value: true });
19const assert = require("assert");
20const mocha_1 = require("mocha");
21const index_1 = require("../index");
22mocha_1.describe('#forEachAsync()', function () {
23 mocha_1.describe('Array', function () {
24 mocha_1.it('should work fill output array by sync action', async function () {
25 const IN_ARR = [1, 2, 3, 4, 5];
26 const OUT_ARR = [];
27 await index_1.forEachAsync(IN_ARR, (item, i) => {
28 OUT_ARR.push(`${item * 10}::${i}`);
29 });
30 assert.equal(OUT_ARR.length, IN_ARR.length);
31 assert.strictEqual(OUT_ARR.length, IN_ARR.length);
32 for (let i = 0; i < OUT_ARR.length; i++) {
33 const ITEM = OUT_ARR[i];
34 const EXPECTED = `${IN_ARR[i] * 10}::${i}`;
35 assert.equal(ITEM, EXPECTED);
36 }
37 });
38 mocha_1.it('should work fill output array by async action', async function () {
39 const IN_ARR = [1, 2, 3, 4, 5];
40 const OUT_ARR = [];
41 await index_1.forEachAsync(IN_ARR, async (item, i) => {
42 OUT_ARR.push(`${item * 10}::${i}`);
43 });
44 assert.equal(OUT_ARR.length, IN_ARR.length);
45 assert.strictEqual(OUT_ARR.length, IN_ARR.length);
46 for (let i = 0; i < OUT_ARR.length; i++) {
47 const ITEM = OUT_ARR[i];
48 const EXPECTED = `${IN_ARR[i] * 10}::${i}`;
49 assert.equal(ITEM, EXPECTED);
50 }
51 });
52 });
53 mocha_1.describe('Generators', function () {
54 const TO_ITERATOR = function* (arr) {
55 for (const ITEM of arr) {
56 yield ITEM;
57 }
58 };
59 mocha_1.it('should work fill output array by sync action', async function () {
60 const IN_ARR = [1, 2, 3, 4, 5];
61 const OUT_ARR = [];
62 await index_1.forEachAsync(TO_ITERATOR(IN_ARR), (item, i) => {
63 OUT_ARR.push(`${item * 10}::${i}`);
64 });
65 assert.equal(OUT_ARR.length, IN_ARR.length);
66 assert.strictEqual(OUT_ARR.length, IN_ARR.length);
67 for (let i = 0; i < OUT_ARR.length; i++) {
68 const ITEM = OUT_ARR[i];
69 const EXPECTED = `${IN_ARR[i] * 10}::${i}`;
70 assert.equal(ITEM, EXPECTED);
71 }
72 });
73 mocha_1.it('should work fill output array by async action', async function () {
74 const IN_ARR = [1, 2, 3, 4, 5];
75 const OUT_ARR = [];
76 await index_1.forEachAsync(TO_ITERATOR(IN_ARR), async (item, i) => {
77 OUT_ARR.push(`${item * 10}::${i}`);
78 });
79 assert.equal(OUT_ARR.length, IN_ARR.length);
80 assert.strictEqual(OUT_ARR.length, IN_ARR.length);
81 for (let i = 0; i < OUT_ARR.length; i++) {
82 const ITEM = OUT_ARR[i];
83 const EXPECTED = `${IN_ARR[i] * 10}::${i}`;
84 assert.equal(ITEM, EXPECTED);
85 }
86 });
87 });
88 mocha_1.describe('Strings', function () {
89 mocha_1.it('should work fill output array by sync action', async function () {
90 const IN_STR = '12345';
91 const OUT_ARR = [];
92 await index_1.forEachAsync(IN_STR, (item, i) => {
93 OUT_ARR.push(`${parseInt(item) * 10}::${i}`);
94 });
95 assert.equal(OUT_ARR.length, IN_STR.length);
96 assert.strictEqual(OUT_ARR.length, IN_STR.length);
97 for (let i = 0; i < OUT_ARR.length; i++) {
98 const ITEM = OUT_ARR[i];
99 const EXPECTED = `${parseInt(IN_STR[i]) * 10}::${i}`;
100 assert.equal(ITEM, EXPECTED);
101 }
102 });
103 mocha_1.it('should work fill output array by async action', async function () {
104 const IN_STR = '12345';
105 const OUT_ARR = [];
106 await index_1.forEachAsync(IN_STR, (item, i) => {
107 OUT_ARR.push(`${parseInt(item) * 10}::${i}`);
108 });
109 assert.equal(OUT_ARR.length, IN_STR.length);
110 assert.strictEqual(OUT_ARR.length, IN_STR.length);
111 for (let i = 0; i < OUT_ARR.length; i++) {
112 const ITEM = OUT_ARR[i];
113 const EXPECTED = `${parseInt(IN_STR[i]) * 10}::${i}`;
114 assert.equal(ITEM, EXPECTED);
115 }
116 });
117 });
118});
119//# sourceMappingURL=forEachAsync.js.map
\No newline at end of file