bluebird-tools
Advanced tools
Comparing version 1.7.0 to 1.8.0
{ | ||
"name": "bluebird-tools", | ||
"version": "1.7.0", | ||
"version": "1.8.0", | ||
"description": "", | ||
"main": "src/index.js", | ||
"scripts": { | ||
"test": "node ./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha tests --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage" | ||
"test": "node ./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage" | ||
}, | ||
@@ -9,0 +9,0 @@ "repository": { |
@@ -31,2 +31,16 @@ # Bluebird Tools | ||
#### promise.for(start, end, method) -> `promise` | ||
Calls `method` passing the `iterator` and the resolved `value` | ||
```js | ||
Promise.resolve(123) | ||
.for(0, 5, (i, val) => console.log('iterator:', i, 'value:', val)); | ||
/* output: | ||
iterator: 0 value: 123 | ||
iterator: 1 value: 123 | ||
iterator: 2 value: 123 | ||
iterator: 3 value: 123 | ||
iterator: 4 value: 123 | ||
*/ | ||
``` | ||
#### promise.when(condition, success) -> `promise` | ||
@@ -33,0 +47,0 @@ Calls `success` if pass the `condition` or calls `fail` |
168
src/index.js
const Promise = require('bluebird'); | ||
const now = require('performance-now'); | ||
Promise.configureLog = function configureLog(log) { | ||
this._log = function(level, text, ...args) { | ||
log(level, text, ...args); | ||
} | ||
}; | ||
require('./flow-control')(Promise); | ||
require('./log')(Promise); | ||
require('./manipulation')(Promise); | ||
require('./monitor')(Promise); | ||
Promise.log = function log(level, text, ...args) { | ||
if (this._log) { | ||
this._log(level, text, ...args); | ||
} | ||
else { | ||
console.log(level, text, ...args); | ||
} | ||
return Promise.resolve(); | ||
}; | ||
Promise.prototype.log = function log(level, text, ...args) { | ||
return this.tap(() => Promise.log(level, text, ...args)); | ||
}; | ||
Promise.prototype.silly = function log(text, ...args) { | ||
return this.log('silly', text, ...args); | ||
}; | ||
Promise.prototype.debug = function log(text, ...args) { | ||
return this.log('debug', text, ...args); | ||
}; | ||
Promise.prototype.verbose = function log(text, ...args) { | ||
return this.log('verbose', text, ...args); | ||
}; | ||
Promise.prototype.info = function log(text, ...args) { | ||
return this.log('info', text, ...args); | ||
}; | ||
Promise.prototype.warning = function log(text, ...args) { | ||
return this.log('warning', text, ...args); | ||
}; | ||
Promise.prototype.error = function log(text, ...args) { | ||
return this.log('error', text, ...args); | ||
}; | ||
Promise.prototype.iif = function iif(conditional, success, fail) { | ||
return this.then( | ||
(val) => new Promise( | ||
(resolve) => resolve(conditional(val) ? success(val) : fail(val)))); | ||
}; | ||
Promise.iif = function iif(conditional, success, fail) { | ||
return new Promise( | ||
(resolve) => resolve(conditional() ? success() : fail())); | ||
}; | ||
Promise.prototype.when = function when(conditional, success) { | ||
return this.iif(conditional, success, echo); | ||
}; | ||
Promise.when = function when(conditional, success) { | ||
return Promise.iif(conditional, success, echo); | ||
}; | ||
Promise.prototype.whenLog = function when(level, conditional, text, ...args) { | ||
return this.then(() => { | ||
if (conditional(this.value())) { | ||
Promise.log(level, text, ...args); | ||
} | ||
return this; | ||
}); | ||
}; | ||
Promise.prototype.unless = function unless(conditional, fail) { | ||
return this.iif(conditional, echo, fail); | ||
}; | ||
Promise.unless = function unless(conditional, fail) { | ||
return Promise.iif(conditional, echo, fail); | ||
}; | ||
Promise.prototype.unlessLog = function unless(level, conditional, text, ...args) { | ||
return this.then(() => { | ||
if (!conditional(this.value())) { | ||
Promise.log(level, text, ...args); | ||
} | ||
return this; | ||
} | ||
); | ||
}; | ||
Promise.prototype.thenMonitor = function thenMonitor(name, method) { | ||
return this.then(x => { | ||
const start = now(); | ||
this.debug('starting', name); | ||
return method(x) | ||
.debug('finished', name, 'in', (now() - start).toFixed(3), 'ms'); | ||
}); | ||
}; | ||
Promise.prototype.iifMonitor = function iifMonitor(name, conditional, success, fail) { | ||
const start = now(); | ||
return this.then( | ||
val => new Promise((resolve) => { | ||
this.debug('starting', name); | ||
const cond = conditional(val); | ||
let result; | ||
if (cond) { | ||
this.debug('process', name, 'has success'); | ||
result = success(val); | ||
} else { | ||
this.debug('process', name, 'has fail'); | ||
result = fail(val); | ||
} | ||
return resolve(result); | ||
})) | ||
.debug('finished', name, 'in', (now() - start).toFixed(3), 'ms'); | ||
}; | ||
Promise.prototype.whenMonitor = function thenMonitor(name, conditional, method) { | ||
return this.when(conditional, x => { | ||
const start = now(); | ||
this.debug('starting', name); | ||
return method(x) | ||
.debug('finished', name, 'in', (now() - start).toFixed(3), 'ms'); | ||
}); | ||
}; | ||
Promise.prototype.unlessMonitor = function unlessMonitor(name, conditional, method) { | ||
return this.unlessLog(conditional, x => { | ||
const start = now(); | ||
this.debug('starting', name); | ||
return method(x) | ||
.debug('finished', name, 'in', (now() - start).toFixed(3), 'ms'); | ||
}); | ||
}; | ||
Promise.monitor = function monitor(name, method) { | ||
const start = now(); | ||
Promise.log('debug', 'starting', name); | ||
return method() | ||
.debug('finished', name, 'in', (now() - start).toFixed(3), 'ms'); | ||
}; | ||
Promise.prototype.isBluebird = true; | ||
Promise.convert = function convert(promise) { | ||
return promise.isBluebird | ||
? promise | ||
: new Promise((resolve, reject) => { | ||
promise | ||
.then(resolve) | ||
.catch(reject); | ||
}); | ||
}; | ||
module.exports = Promise; | ||
function echo(ping) { | ||
return ping; | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
50526
57
1244
208
1