Last active
July 20, 2018 03:24
-
-
Save falconmick/3e93d762f4a32909dec53b0020f4f8f4 to your computer and use it in GitHub Desktop.
This file contains 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
const bindFactory = injectable => (...binds) => injectable(Object.assign({}, ...(binds || [{}]))); | |
const service = args => ({myArgs: () => args}); | |
const bindableService = bindFactory(service); | |
console.log(bindableService().myArgs()); | |
const withSomeArgs = bindableService.bind(null, {a: 'apple;'}); | |
console.log(withSomeArgs().myArgs()); | |
const withMoreArgs = withSomeArgs.bind(null, {b: 'success'}); | |
console.log(withMoreArgs().myArgs()); | |
console.log(withMoreArgs({c: 'finally add more here too'}).myArgs()); | |
// fluent binding factory implemented as a class | |
class Elbinderator { | |
constructor(injectable) { | |
this.bindInProgress = bindFactory(injectable); | |
this.bind = this.bind.bind(this); | |
this.build = this.build.bind(this); | |
} | |
bind(...args) { | |
this.bindInProgress = this.bindInProgress.bind(null, ...args); | |
return this; | |
} | |
build(...args) { | |
return this.bindInProgress(...args); | |
} | |
} | |
// fluent binding factory implemented as a function | |
const elBindorator = injectable => { | |
const self = this; | |
let bindInProgress = bindFactory(injectable); | |
self.return = { | |
bind: (...args) => { | |
bindInProgress = bindInProgress.bind(null, ...args); | |
return self.return; | |
}, | |
build: (...args) => { | |
return bindInProgress.bind(null, ...args)(); | |
} | |
}; | |
return self.return; | |
}; | |
const testElBindorator = new Elbinderator(service); | |
// const testElBindorator = elBindorator(service); | |
// showing mutliple uses of the elbindinator, you can pass multplie or single objects in at a time and it will assign em all together | |
const myBindedSerice = testElBindorator | |
.bind({a: 'apple;'}) | |
.bind({b: 'bapples'}, {bb: 'bbaple'}) | |
.build({c: 'capples'}, {cc: 'ccapples'}); | |
console.log(myBindedSerice.myArgs()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment