Skip to content

Instantly share code, notes, and snippets.

@etale
Last active March 2, 2018 04:25
Show Gist options
  • Save etale/56d57e7cdd4d43ed55fbdeb2775680d5 to your computer and use it in GitHub Desktop.
Save etale/56d57e7cdd4d43ed55fbdeb2775680d5 to your computer and use it in GitHub Desktop.
an extension for Number
(({ ownKeys, defineProperty }, { prototype }) => ownKeys(Math).forEach(property => defineProperty( prototype, property, (value => typeof value === 'function' ? value.length < 2 ? { get() { return value(this) } } : { value(a) { return value(this, a) } } : { value })(Math[property]) ) ))(Reflect, Number)
@etale
Copy link
Author

etale commented Mar 28, 2017

Reflect.ownKeys(Math).forEach((k) => (
  ((value) => (
    Reflect.defineProperty(Number.prototype, k,
      typeof value !== 'function' ? { value } :
      value.length < 2 ? { get() { return value(this) } } :
      { value(a) { return value(a) } }
    )
  ))(Math[k])
))

@etale
Copy link
Author

etale commented Jul 28, 2017

Reflect.set(Number.prototype, Symbol.iterator, function* () {
  let i = 0; while (i < this) yield i++
})

@etale
Copy link
Author

etale commented Mar 2, 2018

;(({ ownKeys, defineProperty }, { prototype }) =>
  ownKeys(Math).forEach(property =>
    defineProperty(
      prototype,
      property,
      (value =>
        typeof value === 'function'
          ? value.length < 2
            ? {
                get() {
                  return value(this)
                }
              }
            : {
                value(a) {
                  return value(this, a)
                }
              }
          : { value })(Math[property])
    )
  ))(Reflect, Number)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment