Skip to content

Commit 5b53b7e

Browse files
committed
Initial commit
0 parents  commit 5b53b7e

13 files changed

+5931
-0
lines changed

.babelrc

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"presets": ["@babel/env"],
3+
"env": {
4+
"production-esm": {
5+
"presets": [
6+
["@babel/env", { "modules": false }]
7+
]
8+
},
9+
"test": {
10+
"presets": [
11+
["@babel/env", {
12+
"targets": {
13+
"node": "current"
14+
}
15+
}]
16+
]
17+
}
18+
}
19+
}

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
**/dist/*

.eslintrc.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "airbnb-base",
3+
"env": {
4+
"browser": true,
5+
"es6": true,
6+
"jest": true
7+
},
8+
"rules": {
9+
"no-restricted-globals": "off"
10+
}
11+
}

.gitattributes

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto
3+
4+
# Custom for Visual Studio
5+
*.cs diff=csharp
6+
7+
# Standard to msysgit
8+
*.doc diff=astextplain
9+
*.DOC diff=astextplain
10+
*.docx diff=astextplain
11+
*.DOCX diff=astextplain
12+
*.dot diff=astextplain
13+
*.DOT diff=astextplain
14+
*.pdf diff=astextplain
15+
*.PDF diff=astextplain
16+
*.rtf diff=astextplain
17+
*.RTF diff=astextplain

.github/FUNDING.yml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
github: wojtekmaj

.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.DS_Store
2+
.idea
3+
.vscode
4+
dist
5+
coverage
6+
node_modules
7+
npm-debug.log
8+
yarn-error.log

.travis.yml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
language: node_js
2+
node_js: "node"

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Wojciech Maj
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# async-array-utils
2+
A collection of array-related async utilities.
3+
4+
## tl;dr
5+
* Install by executing `npm install @wojtekmaj/async-array-utils` or `yarn add @wojtekmaj/async-array-utils`.
6+
* Import by adding `import * as asyncArrayUtils from '@wojtekmaj/async-array-utils'`.
7+
* Do stuff with it!
8+
```js
9+
const asyncMappedArr = await asyncMap([1, 2, 3], async (x) => x * 2);
10+
```
11+
12+
## User guide
13+
14+
### General utils
15+
16+
#### `asyncMap()`
17+
18+
Creates a new array populated with the results of calling a provided asynchronous function on every element in the calling array.
19+
20+
Note: For optimization purposes, all iterations are ran concurrently. If you rely on any side effects, consider `asyncMapStrict()` instead.
21+
22+
##### Sample usage
23+
24+
```js
25+
import { asyncMap } from '@wojtekmaj/async-array-utils';
26+
27+
const asyncMappedArr = await asyncMap([1, 2, 3], async (el, index) => el * 2); // [2, 4, 6]
28+
```
29+
30+
#### `asyncMapStrict()`
31+
32+
Like `asyncMap()`, but runs iterations non-concurrently.
33+
34+
##### Sample usage
35+
36+
```js
37+
import { asyncMapStrict } from '@wojtekmaj/async-array-utils';
38+
39+
const indexes = [];
40+
await asyncMapStrict([1, 2, 3], async (el, index) => { indexes.push(index); return el * 2; }); // [2, 4, 6]
41+
console.log(indexes); // [0, 1, 2]
42+
```
43+
44+
#### `asyncForEach()`
45+
46+
Executes a provided asynchronous function once for each array element.
47+
48+
Note: For optimization purposes, all iterations are ran concurrently. If you rely on any side effects, consider `asyncForEachStrict()` instead.
49+
50+
##### Sample usage
51+
52+
```js
53+
import { asyncForEach } from '@wojtekmaj/async-array-utils';
54+
55+
await asyncForEach(
56+
[1, 2, 3],
57+
async (el) => { console.log(el * 2); }
58+
); // undefined; 3 console.logs
59+
```
60+
61+
#### `asyncForEachStrict()`
62+
63+
Like `asyncForEach()`, but runs iterations non-concurrently.
64+
65+
##### Sample usage
66+
67+
```js
68+
import { asyncForEachStrict } from '@wojtekmaj/async-array-utils';
69+
70+
const indexes = [];
71+
await asyncForEachStrict(
72+
[1, 2, 3],
73+
async (el) => { indexes.push(index); console.log(el * 2); }
74+
); // undefined; 3 console.logs
75+
console.log(indexes); // [0, 1, 2]
76+
```
77+
78+
#### `asyncReduce()`
79+
80+
Executes a reducer asynchronous function (that you provide) on each element of the array, resulting in a single output value.
81+
82+
##### Sample usage
83+
84+
```js
85+
import { asyncReduce } from '@wojtekmaj/async-array-utils';
86+
87+
const result = await asyncReduce(
88+
[1, 2, 3],
89+
async (tmp, cur, idx) => { return tmp + cur },
90+
0
91+
); // 6
92+
```
93+
94+
## License
95+
96+
The MIT License.
97+
98+
## Author
99+
100+
<table>
101+
<tr>
102+
<td>
103+
<img src="https://github.com/wojtekmaj.png?s=100" width="100">
104+
</td>
105+
<td>
106+
Wojciech Maj<br />
107+
<a href="mailto:kontakt@wojtekmaj.pl">kontakt@wojtekmaj.pl</a><br />
108+
<a href="http://wojtekmaj.pl">http://wojtekmaj.pl</a>
109+
</td>
110+
</tr>
111+
</table>

package.json

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"name": "@wojtekmaj/async-array-utils",
3+
"version": "1.0.0",
4+
"description": "A collection of array-related async utilities.",
5+
"main": "dist/umd/index.js",
6+
"module": "dist/esm/index.js",
7+
"source": "src/index.js",
8+
"sideEffects": false,
9+
"scripts": {
10+
"build": "yarn build-esm && yarn build-umd",
11+
"build-esm": "cross-env BABEL_ENV=production-esm babel src -d dist/esm --ignore **/*.spec.js",
12+
"build-umd": "cross-env BABEL_ENV=production-umd babel src -d dist/umd --ignore **/*.spec.js",
13+
"clean": "rimraf dist",
14+
"jest": "jest",
15+
"jest-coverage": "jest --coverage",
16+
"lint": "eslint src/ --ext .jsx,.js",
17+
"prepublishOnly": "yarn clean && yarn build",
18+
"test": "yarn lint && yarn jest"
19+
},
20+
"keywords": [
21+
"array",
22+
"async",
23+
"promise",
24+
"utils"
25+
],
26+
"author": {
27+
"name": "Wojciech Maj",
28+
"email": "kontakt@wojtekmaj.pl"
29+
},
30+
"license": "MIT",
31+
"devDependencies": {
32+
"@babel/cli": "^7.8.0",
33+
"@babel/core": "^7.9.0",
34+
"@babel/preset-env": "^7.9.0",
35+
"cross-env": "^7.0.0",
36+
"eslint": "^6.8.0",
37+
"eslint-config-airbnb-base": "^14.0.0",
38+
"eslint-plugin-import": "^2.18.2",
39+
"jest": "^25.1.0",
40+
"rimraf": "^3.0.0"
41+
},
42+
"files": [
43+
"LICENSE",
44+
"README.md",
45+
"dist/",
46+
"src/"
47+
],
48+
"repository": {
49+
"type": "git",
50+
"url": "https://github.com/wojtekmaj/async-array-utils.git"
51+
}
52+
}

src/index.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
export async function asyncMap(arr, fn) {
2+
return Promise.all(arr.map(fn));
3+
}
4+
5+
export async function asyncMapStrict(arr, fn) {
6+
const result = [];
7+
8+
for (let idx = 0; idx < arr.length; idx += 1) {
9+
const cur = arr[idx];
10+
11+
// eslint-disable-next-line no-await-in-loop
12+
result.push(await fn(cur, idx, arr));
13+
}
14+
15+
return result;
16+
}
17+
18+
export async function asyncForEach(arr, fn) {
19+
await asyncMap(arr, fn);
20+
}
21+
22+
export async function asyncForEachStrict(arr, fn) {
23+
await asyncMapStrict(arr, fn);
24+
}
25+
26+
export async function asyncReduce(arr, fn, initialValue) {
27+
let temp = initialValue;
28+
29+
for (let idx = 0; idx < arr.length; idx += 1) {
30+
const cur = arr[idx];
31+
32+
// eslint-disable-next-line no-await-in-loop
33+
temp = await fn(temp, cur, idx);
34+
}
35+
36+
return temp;
37+
}

0 commit comments

Comments
 (0)