Skip to content

Commit 6c0b01c

Browse files
committed
Merge pull request #11 from nodegit/revert-7-fix-refactoring
Revert "Undo revert of refactored code"
2 parents 7be4dad + b9dc516 commit 6c0b01c

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

index.js

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,14 @@ function processExports(exports, test, cached, parentKeyName) {
3737
exports[keyName] = processExports(exports[keyName], test, cached, name);
3838
}
3939

40+
// Assign the new function in place.
41+
var wrapped = Promise.denodeify(exports);
42+
4043
// Find methods on the prototype, if there are any.
4144
if (Object.keys(exports.prototype).length) {
4245
processExports(exports.prototype, test, cached, name);
4346
}
4447

45-
// If a filter function exists, use this to determine if the function
46-
// is asynchronous.
47-
if (test) {
48-
// Pass the function itself, its keyName, and the parent keyName.
49-
if (!test(exports, exports.name || parentKeyName, parentKeyName)) {
50-
return exports;
51-
}
52-
}
53-
54-
// If the callback name exists as the last argument, consider it an
55-
// asynchronous function. Brittle? Fragile? Effective.
56-
else if (callbacks.indexOf(args(exports).slice(-1)[0]) === -1) {
57-
return exports;
58-
}
59-
60-
// Assign the new function in place.
61-
var wrapped = Promise.denodeify(exports);
62-
6348
// Attach the augmented prototype.
6449
wrapped.prototype = exports.prototype;
6550

@@ -72,17 +57,35 @@ function processExports(exports, test, cached, parentKeyName) {
7257
Object.keys(exports).map(function(keyName) {
7358
// Convert to values.
7459
return [keyName, exports[keyName]];
75-
}).forEach(function(keyVal) {
60+
}).filter(function(keyVal) {
7661
var keyName = keyVal[0];
7762
var value = keyVal[1];
7863

7964
// If an object is encountered, recursively traverse.
8065
if (typeof value === "object") {
8166
processExports(exports, test, cached, keyName + ".");
8267
}
68+
// Filter to functions with callbacks only.
69+
else if (typeof value === "function") {
70+
// If a filter function exists, use this to determine if the function
71+
// is asynchronous.
72+
if (test) {
73+
// Pass the function itself, its keyName, and the parent keyName.
74+
return test(value, keyName, parentKeyName);
75+
}
76+
77+
// If the callback name exists as the last argument, consider it an
78+
// asynchronous function. Brittle? Fragile? Effective.
79+
if (callbacks.indexOf(args(value).slice(-1)[0]) > -1) {
80+
return true;
81+
}
82+
}
83+
}).forEach(function(keyVal) {
84+
var keyName = keyVal[0];
85+
var func = keyVal[1];
8386

8487
// Wrap this function and reassign.
85-
exports[keyName] = processExports(value, test, cached, parentKeyName);
88+
exports[keyName] = processExports(func, test, cached, parentKeyName);
8689
});
8790

8891
return exports;

test/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ describe("Promisify", function() {
9898
a: function a() { arguments[0](); }
9999
};
100100

101-
promisify(Test, function(func, keyName) {
102-
return keyName === "a";
101+
promisify(Test, function(func, keyName, parentKeyName) {
102+
return func.name === "a";
103103
});
104104

105105
return new Test().a();

0 commit comments

Comments
 (0)