-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (47 loc) · 1.42 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* dualAPI
* https://github.com/anseki/dual-api
*
* Copyright (c) 2018 anseki
* Licensed under the MIT license.
*/
/* eslint no-underscore-dangle: [2, {"allow": ["_dualApi_methodNames"]}] */
'use strict';
function implementApi(method) {
return function() {
var args = Array.prototype.slice.call(arguments), cb;
if (args.length && args[args.length - 1] instanceof Function) {
// Callback
cb = args.pop();
args.unshift(
// resolve: cb(null, rtn1, rtn2 ...)
function() {
Array.prototype.unshift.call(arguments, null);
cb.apply(null, arguments);
},
// reject: cb(error, rtn1, rtn2 ...)
function() { cb.apply(null, arguments); });
return method.apply(null, args);
} else {
// Promise
return new Promise(function(resolve, reject) {
args.unshift(resolve, reject);
method.apply(null, args);
});
}
};
}
var baseModuleObj = module.parent;
if (baseModuleObj.exports) {
if (baseModuleObj.exports instanceof Function) {
baseModuleObj.exports = implementApi(baseModuleObj.exports);
} else {
(baseModuleObj.exports._dualApi_methodNames ||
Object.keys(baseModuleObj.exports))
.forEach(function(methodName) {
if (baseModuleObj.exports[methodName] instanceof Function) {
baseModuleObj.exports[methodName] = implementApi(baseModuleObj.exports[methodName]);
}
});
}
}