Skip to content

Commit 4bae7c8

Browse files
committed
Initial commit
0 parents  commit 4bae7c8

File tree

8 files changed

+163
-0
lines changed

8 files changed

+163
-0
lines changed

.babelrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"presets": [
3+
["es2015", {"loose":true}],
4+
"stage-0"
5+
],
6+
"plugins": [
7+
["transform-react-jsx", {"pragma":"h"}]
8+
]
9+
}

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/lib
2+
node_modules
3+
npm-debug.log
4+
.DS_Store

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
language: node_js
2+
node_js:
3+
- stable

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2016 Jason Miller
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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# `asyncro` [![NPM](https://img.shields.io/npm/v/asyncro.svg?style=flat)](https://www.npmjs.org/package/asyncro) [![travis-ci](https://travis-ci.org/developit/asyncro.svg?branch=master)](https://travis-ci.org/developit/asyncro)
2+
3+
Converts JSX to Objects (JSON) <sub>using blood magic</sub>.
4+
5+
```sh
6+
npm install --save asyncro
7+
```
8+
9+
---
10+
11+
### Example
12+
13+
```js
14+
import { map } from 'asyncro';
15+
16+
async function example() {
17+
await map(
18+
['foo', 'bar'],
19+
async name => fetch('./'+name)
20+
)
21+
}
22+
```

package.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "asyncro",
3+
"version": "1.0.0",
4+
"description": "Utilities for working with Arrays asynchronously.",
5+
"main": "lib/index.js",
6+
"jsnext:main": "src/index.js",
7+
"scripts": {
8+
"build": "babel src -d lib -s",
9+
"prepublish": "npm run -s build && npm run -s test",
10+
"test": "babel-node test",
11+
"release": "npm run -s build && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish"
12+
},
13+
"repository": {
14+
"type": "git",
15+
"url": "git+https://github.com/developit/asyncro.git"
16+
},
17+
"keywords": [
18+
"async",
19+
"await",
20+
"arrays"
21+
],
22+
"author": "Jason Miller <[email protected]>",
23+
"license": "ISC",
24+
"bugs": {
25+
"url": "https://github.com/developit/asyncro/issues"
26+
},
27+
"homepage": "https://github.com/developit/asyncro",
28+
"devDependencies": {
29+
"babel-cli": "^6.14.0",
30+
"babel-plugin-transform-react-jsx": "^6.8.0",
31+
"babel-preset-es2015": "^6.14.0",
32+
"babel-preset-stage-0": "^6.5.0"
33+
}
34+
}

src/index.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/** Async version of Array.prototype.reduce()
2+
* await reduce(['/foo', '/bar', '/baz'], async (acc, v) => {
3+
* acc[v] = await (await fetch(v)).json();
4+
* return acc;
5+
* }, {});
6+
*/
7+
export async function reduce(arr, fn, val, pure) {
8+
for (let i=0; i<arr.length; i++) {
9+
let v = await fn(val, arr[i], i, arr);
10+
if (pure!==false) val = v;
11+
}
12+
return val;
13+
}
14+
15+
/** Async version of Array.prototype.map()
16+
* await map(['foo', 'baz'], async v => await fetch(v) )
17+
*/
18+
export async function map(arr, fn) {
19+
return await reduce(arr, async (acc, value, index, arr) => {
20+
acc.push(await fn(value, index, arr));
21+
}, [], false);
22+
}
23+
24+
/** Async version of Array.prototype.filter()
25+
* await filter(['foo', 'baz'], async v => (await fetch(v)).ok )
26+
*/
27+
export async function filter(arr, fn) {
28+
return await reduce(arr, async (acc, value, index, arr) => {
29+
if (await fn(value, index, arr)) acc.push(value);
30+
}, [], false);
31+
}
32+
33+
function identity(x) {
34+
return x;
35+
}
36+
37+
function resolve(list) {
38+
let out = Array.isArray(list) ? [] : {};
39+
for (let i in list) if (list.hasOwnProperty(i)) out[i] = list[i]();
40+
return out;
41+
}
42+
43+
/** Provided by standard lib, replaces async.parallel()
44+
* await parallel([
45+
* () => fetch('foo'),
46+
* () => fetch('baz')
47+
* ])
48+
*/
49+
export async function parallel(list) {
50+
return await Promise.all(resolve(list));
51+
}
52+
53+
/** Replaces async.series()
54+
* await series([
55+
* () => fetch('foo'),
56+
* () => fetch('baz')
57+
* ])
58+
*/
59+
export async function series(list) {
60+
return await map(resolve(list), identity);
61+
}

test/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import assert from 'assert';
2+
import { series, parallel } from '../src';
3+
4+
assert.equal(typeof series, 'function');
5+
6+
assert.equal(typeof parallel, 'function');
7+
8+
console.log('✅ success');
9+
process.exit(0);

0 commit comments

Comments
 (0)