forked from sindresorhus/pify
/
index.js
107 lines (86 loc) · 2.22 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
'use strict';
const processFn = (fn, options) => function (...args) {
const P = options.promiseModule;
return new P((resolve, reject) => {
if (options.multiArgs) {
args.push((...result) => {
if (options.errorFirst) {
if (result[0]) {
reject(result);
} else {
result.shift();
resolve(result);
}
} else {
resolve(result);
}
});
} else if (options.errorFirst) {
args.push((error, result) => {
if (error) {
reject(error);
} else {
resolve(result);
}
});
} else {
args.push(resolve);
}
Reflect.apply(fn, this, args);
});
};
const filterCache = new WeakMap();
module.exports = (input, options) => {
options = Object.assign({
exclude: [/.+(Sync|Stream)$/],
errorFirst: true,
promiseModule: Promise
}, options);
const objType = typeof input;
if (!(input !== null && (objType === 'object' || objType === 'function'))) {
throw new TypeError(`Expected \`input\` to be a \`Function\` or \`Object\`, got \`${input === null ? 'null' : objType}\``);
}
const filter = (target, key) => {
let cached = filterCache.get(target);
if (!cached) {
cached = {};
filterCache.set(target, cached);
}
if (key in cached) {
return cached[key];
}
const match = pattern => (typeof pattern === 'string' || typeof key === 'symbol') ? key === pattern : pattern.test(key);
const shouldFilter = options.include ? options.include.some(match) : !options.exclude.some(match);
cached[key] = shouldFilter;
return shouldFilter;
};
const cache = new WeakMap();
const handler = {
apply(target, thisArg, args) {
const cached = cache.get(target);
if (cached) {
return Reflect.apply(cached, thisArg, args);
}
const pified = options.excludeMain ? target : processFn(target, options);
cache.set(target, pified);
return Reflect.apply(pified, thisArg, args);
},
get(target, key) {
const prop = target[key];
if (!filter(target, key)) {
return prop;
}
const cached = cache.get(prop);
if (cached) {
return cached;
}
if (typeof prop === 'function') {
const pified = processFn(prop, options);
cache.set(prop, pified);
return pified;
}
return prop;
}
};
return new Proxy(input, handler);
};