Created
November 28, 2020 19:45
-
-
Save SparK-Cruz/34d96397dd4f1e23e244a51a9f00c2f9 to your computer and use it in GitHub Desktop.
Allows you to chain calls for void methods in third party objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports.chain = function (subject) { | |
const proxy = new Proxy(subject, { | |
get(target, name, receiver) { | |
const member = Reflect.get(target, name, receiver); | |
if (typeof member === 'function') { | |
return function () { | |
const value = member.apply(subject, arguments); | |
if (typeof value !== 'undefined') | |
return value; | |
return proxy; | |
}; | |
} | |
return member; | |
} | |
}); | |
return proxy; | |
}; |
Author
SparK-Cruz
commented
Nov 28, 2020
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment