Helpers

every(interval, action)

Alias to JavaScript's setInterval() function, combined with monkeypatches to Number detailed below.

  • interval - interval to run action on
  • action - action to perform on interval

Returns a setInterval

Examples:

every((5).seconds(), function() {
  return console.log("Hello world! (and again in 5 seconds)");
});

after(interval, action)

Alias to JavaScript's setTimeout() function, combined with monkeypatches to Number detailed below.

  • interval - interval to run action after
  • action - action to perform after interval

Returns a setTimeout

Examples:

after((10).seconds(), function() {
  return console.log("Hello world!");
});

proxyFunctionsToObject(methods, target, base = this, force = false)

Proxies a list of methods from one object to another. It will not overwrite existing methods unless told to.

  • methods - array of functions to proxy
  • target - object to proxy the functions to
  • base - (optional) object that proxied functions will be declared on. Defaults to this
  • force - (optional) boolean - whether or not to force method assignment. Defaults to false

Examples:

var BaseClass, ProxyClass, klass;

ProxyClass = (function() {
  function ProxyClass() {}

  ProxyClass.prototype.reverse = function(string) {
    return string.split('').reverse().join('');
  };

  return ProxyClass;

})();

BaseClass = (function() {
  function BaseClass() {
    this.proxy = new ProxyClass;
    proxyFunctionsToObject(['reverse'], this.proxy);
  }

  return BaseClass;

})();

klass = new BaseClass;

klass.reverse("Hello, World!");

Number#seconds

A monkey-patch to Number that allows for using numbers to represent time in terms of seconds.

Note that while using this in JavaScript, you have to use it in one of these ways:

10..seconds()
// or
(10).seconds()

Otherwise, the JavaScript interpreter will get confused and think you're trying to represent a float.

In CoffeeScript you can use 10.seconds(), as the compiler will turn it into 10..seconds() for you.

Returns a Number representing the time in milliseconds

Examples:

(10).seconds();

Number#second

Aliases to Number#seconds. See that method for more information

Returns a Number representing the time in milliseconds

Examples:

(1).second();

Number#fromScale(start, end)

Monkey-patch on Number, converts a value from an old number scale (start, end) to (0..1) scale.

  • start - starting number of the scale
  • end - top number number of the scale

Returns a number representing the scaled value.

Examples:

(150).fromScale(0, 200);

(5).fromScale(0, 10);

Number#toScale(start, end)

Monkey-patch on Number, converts a value from (0..1) scale to a new (start, end) scale.

  • start - starting number of the scale
  • end - top number number of the scale

This can be chained together with Number#fromScale to quickly and handily convert numbers between scales. e.g

(100).fromScale(0, 200).toScale(0, 100);

Returns a number representing the scaled value.

Examples:

0.75.toScale(0, 200);

0.5.toScale(0, 10);