Extract monkey-patches

This commit is contained in:
Andrew Stewart 2015-02-20 10:52:01 -08:00
parent aa3332445e
commit b17542061b
2 changed files with 95 additions and 92 deletions

View File

@ -8,97 +8,8 @@
"use strict";
var _ = require("./lodash");
var addCoreExtensions = function addCoreExtensions() {
var max = Math.max,
min = Math.min;
// 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
Number.prototype.seconds = function() {
return this * 1000;
};
// Public: Alias for Number::seconds, see comments for that method
//
// Examples
//
// 1.second()
// //=> 1000
//
// Returns an integer representing time in milliseconds
Number.prototype.second = function() {
return this.seconds(this);
};
// Public: Convert value from old scale (start, end) to (0..1) scale
//
// start - low point of scale to convert value from
// end - high point of scale to convert value from
//
// Examples
//
// (5).fromScale(0, 10)
// //=> 0.5
//
// Returns an integer representing 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;
};
// Public: Convert value from (0..1) scale to new (start, end) scale
//
// start - low point of scale to convert value to
// end - high point of scale to convert value to
//
// Examples
//
// (0.5).toScale(0, 10)
// //=> 5
//
// Returns an integer representing 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;
};
};
var _ = require("./lodash"),
monkeyPatches = require("./utils/monkey-patches");
var Utils = module.exports = {
// Public: Alias to setInterval, combined with Number monkeypatches below to
@ -327,7 +238,7 @@ var Utils = module.exports = {
global.after = this.after;
global.constantly = this.constantly;
addCoreExtensions();
monkeyPatches.install();
return this;
}

View File

@ -0,0 +1,92 @@
"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;
};
};