Skip to content

Commit 5e3391b

Browse files
committed
aligning the entire protocol to use options.
1 parent 2688b7c commit 5e3391b

16 files changed

Lines changed: 213 additions & 239 deletions

File tree

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
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"coverage": "istanbul cover ./node_modules/jasmine-node/bin/jasmine-node test",
1111
"travis": "npm run lint && istanbul cover ./node_modules/jasmine-node/bin/jasmine-node test --captureExceptions && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage",
1212
"browserify": "browserify lib/index.js -s spexLib -o spex.js",
13-
"lint": "./node_modules/.bin/eslint ./lib"
13+
"lint": "./node_modules/.bin/eslint ./lib ./test/**/Spec*.js"
1414
},
1515
"files": [
1616
"lib",

test/es6Spec.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
try {
4-
eval("(function *(){})");
4+
eval('(function *(){})');
55
} catch (e) {
66
return; // ES6 not supported, exit.
77
}
@@ -11,4 +11,3 @@ require('./ext/batch/es6');
1111
require('./ext/page/es6');
1212
require('./ext/sequence/es6');
1313
require('./ext/stream/es6');
14-

0 commit comments

Comments
 (0)