Last active
July 26, 2022 14:08
-
-
Save SparK-Cruz/3d820b1e61e7be6680b14f4c988be952 to your computer and use it in GitHub Desktop.
Lib for chaining any method by using the ._. operator
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.unchain = function unchain(subject) { | |
const proxy = new Proxy(subject, { | |
get(target, name, receiver) { | |
if (name === '_') { | |
return new Proxy(subject, { | |
get(target, name, receiver) { | |
const member = Reflect.get(target, name, receiver); | |
if (typeof member === 'function') { | |
return function () { | |
member.apply(subject, arguments); | |
return proxy; | |
}; | |
} | |
return member; | |
} | |
}); | |
} | |
const member = Reflect.get(target, name, receiver); | |
if (typeof member === 'function') { | |
return function () { | |
const value = member.apply(subject, arguments); | |
if (typeof value === 'object') | |
return unchain(value); | |
return value; | |
}; | |
} | |
if (typeof member === 'object') { | |
return unchain(member); | |
} | |
return member; | |
} | |
}); | |
return proxy; | |
}; |
Author
SparK-Cruz
commented
Sep 27, 2021
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment