Skip to content

Instantly share code, notes, and snippets.

@forrest-rival
Last active April 3, 2018 17:15
Show Gist options
  • Save forrest-rival/ee4fb06542b7750676052d3010b860ce to your computer and use it in GitHub Desktop.
Save forrest-rival/ee4fb06542b7750676052d3010b860ce to your computer and use it in GitHub Desktop.
jsla-given
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()
}
}
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