Last active
April 3, 2018 17:15
-
-
Save forrest-rival/ee4fb06542b7750676052d3010b860ce to your computer and use it in GitHub Desktop.
jsla-given
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
| export default function given (val) { | |
| return Given(val) | |
| } | |
| class Given { | |
| constructor (value) { | |
| this._value = value | |
| } | |
| add (num) { | |
| this._value += num | |
| return this | |
| } | |
| subtract (num) { | |
| this._value -= num | |
| return this | |
| } | |
| multiply (num) { | |
| this._value *= num | |
| return this | |
| } | |
| divide (num) { | |
| this._value /= num | |
| return this | |
| } | |
| value () { | |
| return this._value | |
| } | |
| toString () { | |
| return `${this._value}` | |
| } | |
| valueOf () { | |
| return this.value() | |
| } | |
| } |
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
| export default function given (value) { | |
| const factory = { | |
| add: chainable(add), | |
| subtract: chainable(subtract), | |
| multiply: chainable(multiply), | |
| divide: chainable(divide), | |
| value: () => value | |
| } | |
| return factory | |
| function chainable(op) { | |
| return num => (value = op(value, num), factory) | |
| } | |
| } | |
| const add = (x, y) => x + y | |
| const subtract = (x, y) => x - y | |
| const multiply = (x, y) => x * y | |
| const divide = (x, y) => x / y |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment