Skip to content

A tiny script which makes methods of the `Math` object available to numbers by adding properties to `Number.prototype`

License

Notifications You must be signed in to change notification settings

chadkirby/moremathmethods

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

moremathmethods

npm install moremathmethods

mathmethods is a tiny script which makes methods of the Math object available to numbers by adding properties to Number.prototype. This makes it possible to write certain expressions in a more expressive (Ruby-like) manner.

For each Math method, a property is added to Number.prototype. Accessing one of these properties invokes the appropriate method with the number as the first argument. The majority of the Math methods take exactly one argument, so parentheses are not required.

dollars = balance.floor                   // dollars = Math.floor(balance)

width = $nav.offset().left.abs            // width = Math.abs($nav.offset().left)

Infinity.atan.log.sqrt                    // Math.sqrt(Math.log(Math.atan(Infinity)))

A few Math methods – atan2, max, min, and pow – take more than one argument. When a property corresponding to one of these methods is accessed, a callable is returned which accepts the remaining arguments.

x.pow(y)                                  // Math.pow(x, y)

fee = 0..max(rate * hours - advance)      // fee = Math.max(0, rate * hours - advance)

kim.wage = 10..min(ian.wage, jan.wage)    // kim.wage = Math.min(10, ian.wage, jan.wage)

random

Math.random takes zero arguments. For consistency with the other methods, Number.prototype.random produces a number between 0 and this. 100..random, for example, produces a number between 0 and 100.

1..random                                 // Math.random()

// simluate a die roll
6..random.floor + 1                       // Math.floor(6 * Math.random()) + 1

squared and cubed

Shorthands for the equivalent .pows.

area = width.squared                      // area = Math.pow(width, 2)

volume = x.cubed                          // volume = Math.pow(x, 3)

fact

Gives the number's factorial. Throws a RangeError if the number is negative; a TypeError if the number is not an integer.

5..fact                                   // 5 * 4 * 3 * 2 * 1

Running the test suite

make setup
make test

Internet Explorer

There's a bug in IE9 which breaks getters for number primitives which are integers. To accommodate IE9 one must work with Number objects exclusively.

dollars = new Number(balance).floor       // dollars = Math.floor(balance)

This introduces unacceptable noise. One can quieten it to a certain extent by defining a function which returns a Number object whose value is equivalent to that of the provided argument.

function _(n) { return new Number(n) }

dollars = _(balance).floor                // dollars = Math.floor(balance)

width = _($nav.offset().left).abs         // width = Math.abs($nav.offset().left)

Infinity.atan.log.sqrt                    // Math.sqrt(Math.log(Math.atan(Infinity)))

_(x).pow(y)                               // Math.pow(x, y)

fee = _(0).max(rate * hours - advance)    // fee = Math.max(0, rate * hours - advance)

kim.wage = _(10).min(ian.wage, jan.wage)  // kim.wage = Math.min(10, ian.wage, jan.wage)

_(1).random                               // Math.random()

_(6).random.floor + 1                     // Math.floor(6 * Math.random()) + 1

area = _(width).squared                   // area = Math.pow(width, 2)

volume = _(x).cubed                       // volume = Math.pow(x, 3)

A better workaround would be extremely gratefully received!

About

A tiny script which makes methods of the `Math` object available to numbers by adding properties to `Number.prototype`

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • CoffeeScript 100.0%