Skip to content

Commit 8c4fe93

Browse files
authored
Merge pull request #11 from vitaly-t/stream-read
Stream read
2 parents 2dcf32c + 5e3391b commit 8c4fe93

22 files changed

Lines changed: 349 additions & 355 deletions

.eslintrc.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
"jasmine": true
66
},
77
"extends": "eslint:recommended",
8-
"parserOptions": {
9-
},
108
"rules": {
119
"no-else-return": "error",
1210
"no-multi-spaces": "error",
@@ -15,6 +13,13 @@
1513
"new-cap": "error",
1614
"no-console": "error",
1715
"comma-dangle": "error",
16+
"indent": [
17+
"error",
18+
4,
19+
{
20+
"SwitchCase": 1
21+
}
22+
],
1823
"quotes": [
1924
"error",
2025
"single"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ $ npm run coverage
6969

7070
## License
7171

72-
Copyright © 2016 [Vitaly Tomilov](https://github.com/vitaly-t);
72+
Copyright © 2017 [Vitaly Tomilov](https://github.com/vitaly-t);
7373
Released under the MIT license.
7474

7575

lib/ext/batch.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ var BatchError = require('../errors/batch');
55
/**
66
* @method batch
77
* @description
8-
* **Alternative Syntax:**
9-
* `batch(values, {cb})` ⇒ `Promise`
10-
*
118
* Settles (resolves or rejects) every [mixed value]{@tutorial mixed} in the input array.
129
*
1310
* The method resolves with an array of results, the same as the standard $[promise.all],
@@ -20,7 +17,10 @@ var BatchError = require('../errors/batch');
2017
* Passing in anything other than an array will reject with {@link external:TypeError TypeError} =
2118
* `Method 'batch' requires an array of values.`
2219
*
23-
* @param {Function|generator} [cb]
20+
* @param {Object} [options]
21+
* Optional Parameters.
22+
*
23+
* @param {Function|generator} [options.cb]
2424
* Optional callback (or generator) to receive the result for each settled value.
2525
*
2626
* Callback Parameters:
@@ -53,7 +53,7 @@ var BatchError = require('../errors/batch');
5353
* - notification callback `cb` returned a rejected promise or threw an error
5454
*
5555
*/
56-
function batch(values, cb, config) {
56+
function batch(values, options, config) {
5757

5858
var $p = config.promise, $utils = config.utils;
5959

@@ -67,8 +67,11 @@ function batch(values, cb, config) {
6767
return $p.resolve(empty);
6868
}
6969

70-
cb = $utils.wrap(cb);
71-
var self = this, start = Date.now();
70+
options = options || {};
71+
72+
var cb = $utils.wrap(options.cb),
73+
self = this, start = Date.now();
74+
7275
return $p(function (resolve, reject) {
7376
var cbTime, errors = [], remaining = values.length,
7477
result = new Array(remaining);
@@ -143,10 +146,7 @@ function batch(values, cb, config) {
143146
}
144147

145148
module.exports = function (config) {
146-
return function (values, cb) {
147-
if (cb && typeof cb === 'object') {
148-
return batch.call(this, values, cb.cb, config);
149-
}
150-
return batch.call(this, values, cb, config);
149+
return function (values, options) {
150+
return batch.call(this, values, options, config);
151151
};
152152
};

lib/ext/page.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ var PageError = require('../errors/page');
55
/**
66
* @method page
77
* @description
8-
* **Alternative Syntax:**
9-
* `page(source, {dest, limit})` ⇒ `Promise`
10-
*
118
* Resolves a dynamic sequence of pages/arrays with [mixed values]{@tutorial mixed}.
129
*
1310
* The method acquires pages (arrays of [mixed values]{@tutorial mixed}) from the `source` function, one by one,
@@ -33,7 +30,10 @@ var PageError = require('../errors/page');
3330
*
3431
* Passing in anything other than a function will reject with {@link external:TypeError TypeError} = `Parameter 'source' must be a function.`
3532
*
36-
* @param {Function|generator} [dest]
33+
* @param {Object} [options]
34+
* Optional Parameters.
35+
*
36+
* @param {Function|generator} [options.dest]
3737
* Optional destination function (or generator), to receive a resolved {@link batch} of data
3838
* for each page, process it and respond as required.
3939
*
@@ -51,7 +51,7 @@ var PageError = require('../errors/page');
5151
* If the function throws an error or returns a rejected promise, the sequence terminates,
5252
* and the method rejects with {@link errors.PageError PageError}, which will have property `dest` set.
5353
*
54-
* @param {Number} [limit=0]
54+
* @param {Number} [options.limit=0]
5555
* Limits the maximum number of pages to be requested from the `source`. If the value is greater
5656
* than 0, the method will successfully resolve once the specified limit has been reached.
5757
*
@@ -70,19 +70,21 @@ var PageError = require('../errors/page');
7070
* When the method fails, it rejects with {@link errors.PageError PageError}.
7171
*
7272
*/
73-
function page(source, dest, limit, config) {
73+
function page(source, options, config) {
7474

7575
var $p = config.promise, $spex = config.spex, $utils = config.utils;
7676

7777
if (typeof source !== 'function') {
7878
return $p.reject(new TypeError('Parameter \'source\' must be a function.'));
7979
}
8080

81-
limit = (limit > 0) ? parseInt(limit) : 0;
81+
options = options || {};
8282
source = $utils.wrap(source);
83-
dest = $utils.wrap(dest);
8483

85-
var self = this, request, srcTime, destTime, start = Date.now(), total = 0;
84+
var request, srcTime, destTime,
85+
limit = (options.limit > 0) ? parseInt(options.limit) : 0,
86+
dest = $utils.wrap(options.dest),
87+
self = this, start = Date.now(), total = 0;
8688

8789
return $p(function (resolve, reject) {
8890

@@ -176,10 +178,7 @@ function page(source, dest, limit, config) {
176178
}
177179

178180
module.exports = function (config) {
179-
return function (source, dest, limit) {
180-
if (dest && typeof dest === 'object') {
181-
return page.call(this, source, dest.dest, dest.limit, config);
182-
}
183-
return page.call(this, source, dest, limit, config);
181+
return function (source, options) {
182+
return page.call(this, source, options, config);
184183
};
185184
};

lib/ext/sequence.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ var SequenceError = require('../errors/sequence');
55
/**
66
* @method sequence
77
* @description
8-
* **Alternative Syntax:**
9-
* `sequence(source, {dest, limit, track})` ⇒ `Promise`
10-
*
118
* Resolves a dynamic sequence of [mixed values]{@tutorial mixed}.
129
*
1310
* The method acquires [mixed values]{@tutorial mixed} from the `source` function, one at a time, and resolves them,
@@ -31,7 +28,10 @@ var SequenceError = require('../errors/sequence');
3128
*
3229
* Passing in anything other than a function will reject with {@link external:TypeError TypeError} = `Parameter 'source' must be a function.`
3330
*
34-
* @param {Function|generator} [dest]
31+
* @param {Object} [options]
32+
* Optional Parameters.
33+
*
34+
* @param {Function|generator} [options.dest=null]
3535
* Optional destination function (or generator), to receive resolved data for each index,
3636
* process it and respond as required.
3737
*
@@ -49,7 +49,7 @@ var SequenceError = require('../errors/sequence');
4949
* If the function throws an error or returns a rejected promise, the sequence terminates,
5050
* and the method rejects with {@link errors.SequenceError SequenceError}, which will have property `dest` set.
5151
*
52-
* @param {Number} [limit=0]
52+
* @param {Number} [options.limit=0]
5353
* Limits the maximum size of the sequence. If the value is greater than 0, the method will
5454
* successfully resolve once the specified limit has been reached.
5555
*
@@ -58,7 +58,7 @@ var SequenceError = require('../errors/sequence');
5858
* - `source` either returns or resolves with `undefined`
5959
* - either `source` or `dest` functions throw an error or return a rejected promise
6060
*
61-
* @param {Boolean} [track=false]
61+
* @param {Boolean} [options.track=false]
6262
* Changes the type of data to be resolved by this method. By default, it is `false`
6363
* (see the return result). When set to be `true`, the method tracks/collects all resolved data
6464
* into an array internally, and resolves with that array once the method has finished successfully.
@@ -79,19 +79,21 @@ var SequenceError = require('../errors/sequence');
7979
*
8080
* When the method fails, it rejects with {@link errors.SequenceError SequenceError}.
8181
*/
82-
function sequence(source, dest, limit, track, config) {
82+
function sequence(source, options, config) {
8383

8484
var $p = config.promise, $utils = config.utils;
8585

8686
if (typeof source !== 'function') {
8787
return $p.reject(new TypeError('Parameter \'source\' must be a function.'));
8888
}
8989

90-
limit = (limit > 0) ? parseInt(limit) : 0;
9190
source = $utils.wrap(source);
92-
dest = $utils.wrap(dest);
9391

94-
var self = this, data, srcTime, destTime, result = [], start = Date.now();
92+
options = options || {};
93+
94+
var limit = (options.limit > 0) ? parseInt(options.limit) : 0,
95+
dest = $utils.wrap(options.dest),
96+
self = this, data, srcTime, destTime, result = [], start = Date.now();
9597

9698
return $p(function (resolve, reject) {
9799

@@ -104,7 +106,7 @@ function sequence(source, dest, limit, track, config) {
104106
if (data === undefined) {
105107
success();
106108
} else {
107-
if (track) {
109+
if (options.track) {
108110
result.push(data);
109111
}
110112
if (dest) {
@@ -164,7 +166,7 @@ function sequence(source, dest, limit, track, config) {
164166

165167
function success() {
166168
var length = Date.now() - start;
167-
if (track) {
169+
if (options.track) {
168170
$utils.extend(result, 'duration', length);
169171
} else {
170172
result = {
@@ -186,10 +188,7 @@ function sequence(source, dest, limit, track, config) {
186188
}
187189

188190
module.exports = function (config) {
189-
return function (source, dest, limit, track) {
190-
if (dest && typeof dest === 'object') {
191-
return sequence.call(this, source, dest.dest, dest.limit, dest.track, config);
192-
}
193-
return sequence.call(this, source, dest, limit, track, config);
191+
return function (source, options) {
192+
return sequence.call(this, source, options, config);
194193
};
195194
};

lib/ext/stream/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,3 @@ module.exports = function (config) {
6565
Object.freeze(res);
6666
return res;
6767
};
68-

0 commit comments

Comments
 (0)