Created
December 16, 2021 10:36
-
-
Save Ivannnnn/54fef420edcbeecc5ff3eea71585a0f3 to your computer and use it in GitHub Desktop.
Chainable
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
| function chainable(obj) { | |
| const proxy = new Proxy(obj, { | |
| get(target, prop) { | |
| return typeof target[prop] === 'function' | |
| ? (...args) => target[prop](...args) || proxy | |
| : target[prop] | |
| }, | |
| }) | |
| return proxy | |
| } | |
| const builder = chainable({ | |
| query: {}, | |
| with(a) { | |
| this.query.with = a | |
| }, | |
| where(a) { | |
| this.query.where = a | |
| }, | |
| get() { | |
| return this.query | |
| }, | |
| }) | |
| console.log(builder.with('tasks').where({ projectId: 'abc' }).get()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment