cylon/lib/utils/monkey-patches.js

93 lines
2.1 KiB
JavaScript

"use strict";
var max = Math.max,
min = Math.min;
module.exports.install = function() {
// Public: Monkey-patches Number to have Rails-like //seconds() function.
// Warning, due to the way the Javascript parser works, applying functions on
// numbers is kind of weird. See examples for details.
//
// Examples
//
// 2.seconds()
// //=> SyntaxError: Unexpected token ILLEGAL
//
// 10..seconds()
// //=> 10000
//
// (5).seconds()
// //=> 5000
// // This is the preferred way to represent numbers when calling these
// // methods on them
//
// Returns an integer representing time in milliseconds
/**
* Multiplies a number by 1000 to get time in milliseconds
*
* @example
* (2).seconds(); //=> 2000
* @return {Number} time in milliseconds
*/
Number.prototype.seconds = function() {
return this * 1000;
};
/**
* Alias for Number.prototype.seconds
*
* @see Number.prototype.seconds
* @example
* (1).second(); //=> 1000
* @return {Number} time in milliseconds
*/
Number.prototype.second = Number.prototype.seconds;
/**
* Converts a number from a current scale to a 0 - 1 scale.
*
* @param {Number} start low point of scale to convert from
* @param {Number} end high point of scale to convert from
* @example
* (5).fromScale(0, 10) //=> 0.5
* @return {Number} the scaled value
*/
Number.prototype.fromScale = function(start, end) {
var val = (this - min(start, end)) / (max(start, end) - min(start, end));
if (val > 1) {
return 1;
}
if (val < 0){
return 0;
}
return val;
};
/**
* Converts a number from a 0 - 1 scale to the specified scale.
*
* @param {Number} start low point of scale to convert to
* @param {Number} end high point of scale to convert to
* @example
* (0.5).toScale(0, 10) //=> 5
* @return {Number} the scaled value
*/
Number.prototype.toScale = function(start, end) {
var i = this * (max(start, end) - min(start, end)) + min(start, end);
if (i < start) {
return start;
}
if (i > end) {
return end;
}
return i;
};
};