Skip to content

Commit 4e17ca4

Browse files
committed
Extract API functions into separate files that can be required independently
1 parent 2877629 commit 4e17ca4

File tree

13 files changed

+199
-122
lines changed

13 files changed

+199
-122
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
99
### Major
1010

1111
### Minor
12+
- Extract API functions into separate files that can be required independently
1213

1314
### Patch
1415

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,17 @@ Also see [default values for array destructuring][5].
77

88
[Change Log][6]
99

10-
## Example
10+
## Require
11+
12+
### Require an Object With All Functions
13+
14+
const catchify = require('catchify');
15+
16+
### Require Functions Individually
1117

12-
###
18+
const all = require('catchify/all');
19+
20+
## Example
1321

1422
```
1523
const catchify = require('catchify');
@@ -22,6 +30,8 @@ async function example(promise){
2230
2331
```
2432

33+
34+
2535
## API
2636

2737
* **catchify(value)**

all.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
3+
const arrayToObj = require('./lib/arrayToObj');
4+
const objToArray = require('./lib/objToArray');
5+
6+
function onThenAllFactory (keys) {
7+
return function onThenAll (value) {
8+
return [null, arrayToObj(keys, value)];
9+
};
10+
}
11+
12+
function onCatchAllFactory (keys) {
13+
return function onCatchAll (error) {
14+
return [error, keys ? {} : []];
15+
};
16+
}
17+
18+
function all (iterable) {
19+
const [keys, values] = objToArray(iterable);
20+
return Promise
21+
.all(values)
22+
.then(onThenAllFactory(keys), onCatchAllFactory(keys));
23+
}
24+
25+
module.exports = all;

index.js

Lines changed: 6 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,12 @@
11
'use strict';
22

3-
const arrayToObj = require('./lib/arrayToObj');
4-
const objToArray = require('./lib/objToArray');
5-
6-
function onThenAllFactory (keys) {
7-
return function onThenAll (value) {
8-
return [null, arrayToObj(keys, value)];
9-
};
10-
}
11-
12-
function onThen (value) {
13-
return [null, value];
14-
}
15-
16-
function onCatch (error) {
17-
return [error, null];
18-
}
19-
20-
function onCatchAllFactory (keys) {
21-
return function onCatchAll (error) {
22-
return [error, keys ? {} : []];
23-
};
24-
}
25-
26-
function catchify (p) {
27-
return Promise
28-
.resolve(p)
29-
.then(onThen, onCatch);
30-
}
3+
const catchify = require('./resolve');
314

5+
catchify.all = require('./all');
6+
catchify.limit = require('./limit');
7+
catchify.race = require('./race');
8+
catchify.reject = require('./reject');
329
catchify.resolve = catchify;
33-
34-
catchify.race = function catchifyRace (iterable) {
35-
return Promise
36-
.race(iterable)
37-
.then(onThen, onCatch);
38-
};
39-
40-
catchify.all = function catchifyAll (iterable) {
41-
const [keys, values] = objToArray(iterable);
42-
return Promise
43-
.all(values)
44-
.then(onThenAllFactory(keys), onCatchAllFactory(keys));
45-
};
46-
47-
catchify.reject = function catchifyReject (reason) {
48-
return Promise
49-
.reject(reason)
50-
.catch(onCatch);
51-
};
52-
53-
function onValue (value) {
54-
return {
55-
error: null,
56-
value: value
57-
};
58-
}
59-
60-
function onError (error) {
61-
return {
62-
error: error,
63-
value: null
64-
};
65-
}
66-
67-
function extractValue (result) {
68-
return result.value || null;
69-
}
70-
71-
function extractError (result) {
72-
return result.error || null;
73-
}
74-
75-
function onThenWithErrorsFactory (keys) {
76-
return function onThenWithErrors (results) {
77-
return [
78-
arrayToObj(keys, results.map(extractError)),
79-
arrayToObj(keys, results.map(extractValue))
80-
];
81-
};
82-
}
83-
84-
catchify.some = function catchifySome (iterable) {
85-
const [keys, items] = objToArray(iterable);
86-
const promises = [];
87-
for (let value of items) {
88-
promises.push(Promise
89-
.resolve(value)
90-
.then(onValue, onError)
91-
);
92-
}
93-
return Promise
94-
.all(promises)
95-
.then(onThenWithErrorsFactory(keys));
96-
};
97-
98-
catchify.limit = async function catchifyLimit (iterable, limit = 2, exitOnError = false) {
99-
const [keys, items] = objToArray(iterable);
100-
const allErrors = [];
101-
const allValues = [];
102-
const queue = [];
103-
for (let value of items) {
104-
queue.push(typeof value === 'function' ? value() : value);
105-
if (queue.length === limit) {
106-
const [errors, values] = await catchify.some(queue);
107-
allErrors.push(...errors);
108-
allValues.push(...values);
109-
queue.length = 0;
110-
if (exitOnError && errors.filter((err) => err).length > 0) {
111-
break;
112-
}
113-
}
114-
}
115-
if (queue.length > 0) {
116-
const [errors, values] = await catchify.some(queue);
117-
allErrors.push(...errors);
118-
allValues.push(...values);
119-
}
120-
return [
121-
arrayToObj(keys, allErrors),
122-
arrayToObj(keys, allValues)
123-
];
124-
};
10+
catchify.some = require('./some');
12511

12612
module.exports = catchify;

lib/onCatch.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
function onCatch (error) {
4+
return [error, null];
5+
}
6+
7+
module.exports = onCatch;

lib/onCatch.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
const subject = require('./onCatch');
4+
const test = require('ava');
5+
6+
test('onCatch - error returned in array', t => {
7+
const [error, value] = subject(1);
8+
t.is(error, 1);
9+
t.is(value, null);
10+
});

lib/onThen.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
function onThen (value) {
4+
return [null, value];
5+
}
6+
7+
module.exports = onThen;

lib/onThen.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
const subject = require('./onThen');
4+
const test = require('ava');
5+
6+
test('onThen - return array with value', t => {
7+
const [error, value] = subject(1);
8+
t.is(error, null);
9+
t.is(value, 1);
10+
});

limit.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
const arrayToObj = require('./lib/arrayToObj');
4+
const objToArray = require('./lib/objToArray');
5+
const some = require('./some');
6+
7+
async function limit (iterable, limit = 2, exitOnError = false) {
8+
const [keys, items] = objToArray(iterable);
9+
const allErrors = [];
10+
const allValues = [];
11+
const queue = [];
12+
for (let value of items) {
13+
queue.push(typeof value === 'function' ? value() : value);
14+
if (queue.length === limit) {
15+
const [errors, values] = await some(queue);
16+
allErrors.push(...errors);
17+
allValues.push(...values);
18+
queue.length = 0;
19+
if (exitOnError && errors.filter((err) => err).length > 0) {
20+
break;
21+
}
22+
}
23+
}
24+
if (queue.length > 0) {
25+
const [errors, values] = await some(queue);
26+
allErrors.push(...errors);
27+
allValues.push(...values);
28+
}
29+
return [
30+
arrayToObj(keys, allErrors),
31+
arrayToObj(keys, allValues)
32+
];
33+
}
34+
35+
module.exports = limit;

race.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
const onCatch = require('./lib/onCatch');
4+
const onThen = require('./lib/onThen');
5+
6+
function race (iterable) {
7+
return Promise
8+
.race(iterable)
9+
.then(onThen, onCatch);
10+
}
11+
12+
module.exports = race;

0 commit comments

Comments
 (0)