| 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 | 2x 2x 2x 5x 5x 4x 1x 2x 2x 1x |
function bindWith<S extends Object, T extends Object>(source: S, ...args: any[]): T {
const proto = Reflect.getPrototypeOf(source);
const bindMethod = (cur, key) => {
const f = source[key];
if (typeof (f) === 'function') {
return { ...cur, [key]: f.bind(source, ...args) };
}
return cur;
};
const methods = Object.keys(proto || []).reduce(
bindMethod,
{});
return {
...methods,
...Object.keys(source).reduce(
bindMethod,
{})
};
}
export default bindWith;
|