promise-thunk
Advanced tools
Comparing version 0.1.9 to 0.1.10
{ | ||
"name": "promise-thunk", | ||
"version": "0.1.9", | ||
"version": "0.1.10", | ||
"description": "Promise and thunk", | ||
@@ -5,0 +5,0 @@ "main": "promise-thunk.js", |
@@ -32,4 +32,4 @@ // promise-thunk.js | ||
function setConst(obj, prop, val) { | ||
defProp(obj, prop, {value: val}); } : | ||
function setConst(obj, prop, val) { obj[prop] = val; }; | ||
defProp(obj, prop, {value: val}); return val; } : | ||
function setConst(obj, prop, val) { return obj[prop] = val; }; | ||
@@ -40,4 +40,4 @@ // setValue(obj, prop, val) | ||
defProp(obj, prop, {value: val, | ||
writable: true, configurable: true}); } : | ||
function setValue(obj, prop, val) { obj[prop] = val; }; | ||
writable: true, configurable: true}); return val; } : | ||
function setValue(obj, prop, val) { return obj[prop] = val; }; | ||
@@ -327,2 +327,3 @@ // getProto(obj) | ||
function promisify(fn, options) { | ||
// promisify(object target, string method, [string postfix]) : undefined | ||
if (arguments[0] && arguments[1] && | ||
@@ -332,9 +333,14 @@ typeof arguments[0] === 'object' && | ||
var object = arguments[0], method = arguments[1]; | ||
var postfix = typeof arguments[2] === 'string' ? arguments[2] : 'A'; | ||
var methodAcached = method + postfix + 'cached'; | ||
var postfix = arguments[2] && typeof arguments[2] === 'string' ? arguments[2] : | ||
arguments[2] && typeof arguments[2].postfix === 'string' ? arguments[2].postfix : | ||
arguments[2] && typeof arguments[2].suffix === 'string' ? arguments[2].suffix : 'Async'; | ||
var methodAcached = method + postfix + 'Cached'; | ||
Object.defineProperty(object, method + postfix, { | ||
get: function () { | ||
return this[methodAcached] ? this[methodAcached] : | ||
this[methodAcached] = promisify(this, this[method]); | ||
} | ||
return this.hasOwnProperty(methodAcached) && | ||
typeof this[methodAcached] === 'function' ? | ||
this[methodAcached] : | ||
setValue(this, methodAcached, promisify(this, this[method])); | ||
}, | ||
configurable: true | ||
}); | ||
@@ -344,2 +350,3 @@ return; | ||
// promisify([object ctx,] function fn) : function | ||
var ctx = typeof this !== 'function' ? this : undefined; | ||
@@ -351,3 +358,6 @@ if (typeof options === 'function') ctx = fn, fn = options, options = arguments[2]; | ||
return function promisified() { | ||
// promisified | ||
promisified.promisified = true; | ||
return promisified; | ||
function promisified() { | ||
var args = arguments; | ||
@@ -376,2 +386,3 @@ return PromiseThunk(function (res, rej) { | ||
function thunkify(fn, options) { | ||
// thunkify(object target, string method, [string postfix]) : undefined | ||
if (arguments[0] && arguments[1] && | ||
@@ -381,9 +392,14 @@ typeof arguments[0] === 'object' && | ||
var object = arguments[0], method = arguments[1]; | ||
var postfix = typeof arguments[2] === 'string' ? arguments[2] : 'A'; | ||
var methodAcached = method + postfix + 'cached'; | ||
var postfix = arguments[2] && typeof arguments[2] === 'string' ? arguments[2] : | ||
arguments[2] && typeof arguments[2].postfix === 'string' ? arguments[2].postfix : | ||
arguments[2] && typeof arguments[2].suffix === 'string' ? arguments[2].suffix : 'Async'; | ||
var methodAcached = method + postfix + 'Cached'; | ||
Object.defineProperty(object, method + postfix, { | ||
get: function () { | ||
return this[methodAcached] ? this[methodAcached] : | ||
this[methodAcached] = thunkify(this, this[method]); | ||
} | ||
return this.hasOwnProperty(methodAcached) && | ||
typeof this[methodAcached] === 'function' ? | ||
this[methodAcached] : | ||
setValue(this, methodAcached, thunkify(this, this[method])); | ||
}, | ||
configurable: true | ||
}); | ||
@@ -393,2 +409,3 @@ return; | ||
// thunkify([object ctx,] function fn) : function | ||
var ctx = typeof this !== 'function' ? this : undefined; | ||
@@ -401,3 +418,5 @@ if (typeof options === 'function') ctx = fn, fn = options, options = arguments[2]; | ||
// thunkified | ||
return function thunkified() { | ||
thunkified.thunkified = true; | ||
return thunkified; | ||
function thunkified() { | ||
var result, callbacks = [], unhandled; | ||
@@ -453,2 +472,34 @@ arguments[arguments.length++] = function callback(err, val) { | ||
// PromiseThunk.promisifyAll(object, options) | ||
setValue(PromiseThunk, 'promisifyAll', function promisifyAll(object, options) { | ||
var keys = []; | ||
if (Object.getOwnPropertyNames) keys = Object.getOwnPropertyNames(object); | ||
else if (Object.keys) keys = Object.keys(object); | ||
else for (var method in object) if (object.hasOwnProperty(method)) keys.push(i); | ||
keys.forEach(function (method) { | ||
if (typeof object[method] === 'function' && | ||
!object[method].promisified && | ||
!object[method].thunkified) | ||
promisify(object, method, options); | ||
}); | ||
return object; | ||
}); | ||
// PromiseThunk.thunkifyAll(object, options) | ||
setValue(PromiseThunk, 'thunkifyAll', function thunkifyAll(object, options) { | ||
var keys = []; | ||
if (Object.getOwnPropertyNames) keys = Object.getOwnPropertyNames(object); | ||
else if (Object.keys) keys = Object.keys(object); | ||
else for (var method in object) if (object.hasOwnProperty(method)) keys.push(i); | ||
keys.forEach(function (method) { | ||
if (typeof object[method] === 'function' && | ||
!object[method].promisified && | ||
!object[method].thunkified) | ||
thunkify(object, method, options); | ||
}); | ||
return object; | ||
}); | ||
// PromiseThunk.resolve(val) | ||
@@ -455,0 +506,0 @@ setValue(PromiseThunk, 'resolve', function resolve(val) { |
@@ -198,3 +198,3 @@ [promise-thunk](https://www.npmjs.org/package/promise-thunk) - npm | ||
+ `options`: options object. | ||
+ `conbtext`: context object. | ||
+ `context`: context object. | ||
@@ -207,7 +207,7 @@ also thenable, yieldable, callable. | ||
var pg = require('pg'); | ||
var pg_connect = promisify(pg, pg.connect); // -> yield pg_connect() | ||
var client_query = promisify(client, client.query); // -> yield client_query() | ||
var pg_connect = PromiseThunk.promisify(pg, pg.connect); // -> yield pg_connect() | ||
var client_query = PromiseThunk.promisify(client, client.query); // -> yield client_query() | ||
``` | ||
### PromiseThunk.promisify(object, method, [postfix]) | ||
### PromiseThunk.promisify(object, method, [options]) | ||
@@ -218,3 +218,5 @@ `promisify()` defines method promisified function returns promise-thunk. | ||
+ `method`: method name string. | ||
+ `postfix`: method name postfix or suffix. default: 'A'. | ||
+ `options`: method name postfix or suffix. default: 'Async'. or options object. | ||
+ `postfix`: method name postfix or suffix. default: 'Async'. | ||
+ `suffix`: method name postfix or suffix. default: 'Async'. | ||
@@ -225,7 +227,32 @@ #### postgres `pg` example: | ||
var pg = require('pg'); | ||
promisify(pg, 'connect'); // -> yield pg.connectA() | ||
promisify(pg.Client.prototype, 'connect'); // -> yield client.connectA() | ||
promisify(pg.Client.prototype, 'query'); // -> yield client.queryA() | ||
PromiseThunk.promisify(pg, 'connect', {postfix: 'A'}); // -> yield pg.connectA() | ||
PromiseThunk.promisify(pg.Client.prototype, 'connect'); // -> yield client.connectAsync() | ||
PromiseThunk.promisify(pg.Client.prototype, 'query'); // -> yield client.queryAsync() | ||
``` | ||
### PromiseThunk.promisifyAll(object, [options]) | ||
`promisifyAll()` defines all methods promisified function returns promise-thunk. | ||
+ `object`: target object. | ||
+ `options`: method name postfix or suffix. default: 'Async'. or options object. | ||
+ `postfix`: method name postfix or suffix. default: 'Async'. | ||
+ `suffix`: method name postfix or suffix. default: 'Async'. | ||
#### file system `fs` example: | ||
```js | ||
var fs = require('fs'); | ||
PromiseThunk.promisifyAll(fs, {postfix: 'A'}); // -> yield fs.readFileA() | ||
``` | ||
#### postgres `pg` example: | ||
```js | ||
var pg = require('pg'); | ||
PromiseThunk.promisifyAll(pg.constructor.prototype, {postfix: 'A'}); // -> yield pg.connectA() | ||
PromiseThunk.promisifyAll(pg.Client.prototype); // -> yield client.connectAsync() | ||
// -> yield client.queryAsync() | ||
``` | ||
### PromiseThunk.thunkify([ctx,] fn, [options]) | ||
@@ -239,3 +266,3 @@ | ||
+ `options`: options object. | ||
+ `conbtext`: context object. | ||
+ `context`: context object. | ||
@@ -252,3 +279,3 @@ also yieldable, callable. | ||
### PromiseThunk.thunkify(object, method, [postfix]) | ||
### PromiseThunk.thunkify(object, method, [options]) | ||
@@ -259,3 +286,5 @@ `thunkify()` defines method thunkified function returns thunk. | ||
+ `method`: method name string. | ||
+ `postfix`: method name postfix or suffix. default: 'A'. | ||
+ `options`: method name postfix or suffix. default: 'Async'. or options object. | ||
+ `postfix`: method name postfix or suffix. default: 'Async'. | ||
+ `suffix`: method name postfix or suffix. default: 'Async'. | ||
@@ -266,7 +295,32 @@ #### postgres `pg` example: | ||
var pg = require('pg'); | ||
thunkify(pg, 'connect'); // -> yield pg.connectA() | ||
thunkify(pg.Client.prototype, 'connect'); // -> yield client.connectA() | ||
thunkify(pg.Client.prototype, 'query'); // -> yield client.queryA() | ||
PromiseThunk.thunkify(pg, 'connect', {postfix: 'A'}); // -> yield pg.connectA() | ||
PromiseThunk.thunkify(pg.Client.prototype, 'connect'); // -> yield client.connectAsync() | ||
PromiseThunk.thunkify(pg.Client.prototype, 'query'); // -> yield client.queryAsync() | ||
``` | ||
### PromiseThunk.thunkifyAll(object, [options]) | ||
`thunkifyAll()` defines all methods thunkified function returns thunk. | ||
+ `object`: target object. | ||
+ `options`: method name postfix or suffix. default: 'Async'. or options object. | ||
+ `postfix`: method name postfix or suffix. default: 'Async'. | ||
+ `suffix`: method name postfix or suffix. default: 'Async'. | ||
#### file system `fs` example: | ||
```js | ||
var fs = require('fs'); | ||
PromiseThunk.thunkifyAll(fs, {postfix: 'A'}); // -> yield fs.readFileA() | ||
``` | ||
#### postgres `pg` example: | ||
```js | ||
var pg = require('pg'); | ||
PromiseThunk.thunkifyAll(pg.constructor.prototype, {postfix: 'A'}); // -> yield pg.connectA() | ||
PromiseThunk.thunkifyAll(pg.Client.prototype); // -> yield client.connectAsync() | ||
// -> yield client.queryAsync() | ||
``` | ||
### promise.then(onFulfilled, onRejected) | ||
@@ -273,0 +327,0 @@ |
30397
549
419