putil-waterfall
Advanced tools
Comparing version 2.0.3 to 2.1.0
@@ -46,4 +46,3 @@ /* putil-waterfall | ||
// If promise | ||
if (o && typeof o === 'object' && | ||
typeof o.then === 'function' && typeof o.catch === 'function') { | ||
if (promisify.isPromise(o)) { | ||
o.then((...args) => next(null, ...args)) | ||
@@ -70,3 +69,18 @@ .catch(e => callback(e, ...args)); | ||
waterfall.every = function(values, fn, callback) { | ||
return iterateValues(1, values, fn, callback); | ||
}; | ||
/** | ||
* | ||
* @param {Array<*>} values | ||
* @param {Function} fn | ||
* @param {Function} [callback] | ||
* @return {Promise|null} | ||
*/ | ||
waterfall.some = function(values, fn, callback) { | ||
return iterateValues(2, values, fn, callback); | ||
}; | ||
function iterateValues(op, values, fn, callback) { | ||
if (!Array.isArray(values)) | ||
@@ -79,19 +93,29 @@ throw new Error('Invalid argument. Array value are required as first argument'); | ||
if (!callback) | ||
return promisify.fromCallback((cb) => waterfall.every(values, fn, cb)); | ||
return promisify.fromCallback((cb) => iterateValues(op, values, fn, cb)); | ||
/* istanbul ignore next */ | ||
callback = callback || (() => {}); | ||
const arr = values.slice(0); | ||
values = values.slice(0); | ||
let index = -1; | ||
const next = (...args) => { | ||
index++; | ||
const error = args[0]; | ||
setImmediate(() => { | ||
if (error || !arr.length) | ||
if (args[0] || !values.length) | ||
return callback(...args); | ||
args.shift(); // remove error arg | ||
const v = arr.shift(); | ||
if (op === 2 && args[0]) { | ||
return callback(null, ...args); | ||
} | ||
const v = values.shift(); | ||
const curIndex = index; | ||
const callNext = (...args) => { | ||
// Allow calling "next" once | ||
if (curIndex === index) | ||
next(...args); | ||
}; | ||
try { | ||
const o = fn(next, v, index, ...args); | ||
const o = fn(callNext, v, index, ...args); | ||
if (promisify.isPromise(o)) { | ||
o.then(v => next(null, v)) | ||
o.then(v => callNext(null, v)) | ||
.catch((e) => callback(e, v, index, ...args)); | ||
@@ -104,5 +128,5 @@ } | ||
}; | ||
next(null); | ||
}; | ||
next(); | ||
} | ||
module.exports = waterfall; |
{ | ||
"name": "putil-waterfall", | ||
"description": "Simple, fast async waterfall NodeJs module for JavaScript", | ||
"version": "2.0.3", | ||
"version": "2.1.0", | ||
"author": "Panates Ltd.", | ||
@@ -24,6 +24,6 @@ "contributors": [ | ||
"devDependencies": { | ||
"babel-eslint": "^9.0.0", | ||
"eslint": "^5.4.0", | ||
"eslint-config-google": "^0.9.1", | ||
"istanbul": "^0.4.5", | ||
"babel-eslint": "^10.0.1", | ||
"eslint": "^5.7.0", | ||
"eslint-config-google": "^0.11.0", | ||
"nyc": "^13.1.0", | ||
"mocha": "^5.2.0" | ||
@@ -42,7 +42,10 @@ }, | ||
], | ||
"nyc": { | ||
"temp-directory": "./coverage/.nyc_output" | ||
}, | ||
"scripts": { | ||
"test": "mocha --require test/support/env --reporter spec --bail --check-leaks test/", | ||
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --require test/support/env --reporter dot --check-leaks test/", | ||
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --require test/support/env --reporter spec --check-leaks test/" | ||
"test": "mocha --require ./test/support/env --reporter spec --bail --check-leaks test/", | ||
"cover": "nyc --reporter html --reporter text npm run test", | ||
"travis-cover": "nyc --reporter lcovonly npm run test" | ||
} | ||
} |
8815
110