promisify-node
Advanced tools
Comparing version 0.1.1 to 0.1.2
54
index.js
@@ -13,5 +13,15 @@ const Promise = require("promise"); | ||
* @param {*} exports - Should be a function or an object, identity other. | ||
* @param {Function} test - Optional function to identify async methods. | ||
* @param {String} parentKeyName - Tracks the keyName in a digestable format. | ||
* @returns {*} exports - Identity. | ||
*/ | ||
function processExports(exports) { | ||
function processExports(exports, test, cached, parentKeyName) { | ||
// Return early if this object has already been processed. | ||
if (cached.indexOf(exports) > -1) { | ||
return exports; | ||
} | ||
// Record this object in the cache. | ||
cached.push(exports); | ||
// Pass through if not an object or function. | ||
@@ -22,5 +32,26 @@ if (typeof exports != "object" && typeof exports != "function") { | ||
var name = exports.name + "#"; | ||
// If a function, simply return it wrapped. | ||
if (typeof exports === "function") { | ||
return Promise.denodeify(exports); | ||
// Find properties added to functions. | ||
for (var keyName in exports) { | ||
exports[keyName] = processExports(exports[keyName], test, cached, name); | ||
} | ||
// Assign the new function in place. | ||
var wrapped = Promise.denodeify(exports); | ||
// Find methods on the prototype, if there are any. | ||
if (Object.keys(exports.prototype).length) { | ||
processExports(exports.prototype, test, cached, name); | ||
} | ||
// Attach the augmented prototype. | ||
wrapped.prototype = exports.prototype; | ||
// Ensure attached properties to the previous function are accessible. | ||
wrapped.__proto__ = exports; | ||
return wrapped; | ||
} | ||
@@ -32,2 +63,3 @@ | ||
}).filter(function(keyVal) { | ||
var keyName = keyVal[0]; | ||
var value = keyVal[1]; | ||
@@ -37,6 +69,13 @@ | ||
if (typeof value === "object") { | ||
processExports(exports); | ||
processExports(exports, test, cached, keyName + "."); | ||
} | ||
// Filter to functions with callbacks only. | ||
else if (typeof value === "function") { | ||
// If a filter function exists, use this to determine if the function | ||
// is asynchronous. | ||
if (test) { | ||
// Pass the function itself, its keyName, and the parent keyName. | ||
return test(value, keyName, parentKeyName); | ||
} | ||
// If the callback name exists as the last argument, consider it an | ||
@@ -52,4 +91,4 @@ // asynchronous function. Brittle? Fragile? Effective. | ||
// Assign the new function in place. | ||
exports[keyName] = Promise.denodeify(func); | ||
// Wrap this function and reassign. | ||
exports[keyName] = processExports(func, test, cached, parentKeyName); | ||
}); | ||
@@ -64,5 +103,6 @@ | ||
* @param {*} name - Can be a module name, object, or function. | ||
* @param {Function} test - Optional function to identify async methods. | ||
* @returns {*} exports - The resolved value from require or passed in value. | ||
*/ | ||
module.exports = function(name) { | ||
module.exports = function(name, test) { | ||
var exports = name; | ||
@@ -78,3 +118,3 @@ | ||
// promises. | ||
return processExports(exports); | ||
return processExports(exports, test, []); | ||
}; | ||
@@ -81,0 +121,0 @@ |
{ | ||
"name": "promisify-node", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Wrap Node-callback functions to return Promises.", | ||
@@ -10,6 +10,7 @@ "main": "index.js", | ||
"devDependencies": { | ||
"mocha": "~1.18.2" | ||
"mocha": "~1.18.2", | ||
"istanbul": "~0.2.7" | ||
}, | ||
"scripts": { | ||
"test": "mocha test" | ||
"test": "istanbul cover _mocha -- test" | ||
}, | ||
@@ -16,0 +17,0 @@ "author": "Tim Branyen (@tbranyen)", |
Promisify Node | ||
-------------- | ||
**Stable: 0.1.1** | ||
**Stable: 0.1.2** | ||
@@ -6,0 +6,0 @@ [![Build |
@@ -21,2 +21,9 @@ var promisify = require("../"); | ||
describe("node modules", function() { | ||
it("can be consumed", function() { | ||
var fs = promisify("fs"); | ||
return fs.readFile(__dirname + "/../LICENSE"); | ||
}); | ||
}); | ||
describe("asynchronous method inference", function() { | ||
@@ -70,3 +77,27 @@ var later = function(cb) { | ||
}); | ||
it("can identify an asynchronous function by filter function", function() { | ||
var obj = promisify({ | ||
a: function a() { arguments[0](); } | ||
}, function(func) { | ||
return func.name === "a"; | ||
}); | ||
return obj.a(); | ||
}); | ||
it("can iterate over prototypes", function() { | ||
function Test() {} | ||
Test.prototype = { | ||
a: function a() { arguments[0](); } | ||
}; | ||
promisify(Test, function(func, keyName, parentKeyName) { | ||
return func.name === "a"; | ||
}); | ||
return new Test().a(); | ||
}); | ||
}); | ||
}); |
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
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
10015
193
2