Convenience functions for milliseconds and microseconds

This commit is contained in:
deadprogram 2015-08-31 19:24:23 -07:00
parent bea67b2e5e
commit 012578f012
2 changed files with 50 additions and 0 deletions

View File

@ -44,6 +44,38 @@ module.exports.install = function() {
*/
Number.prototype.second = Number.prototype.seconds;
/**
* Passthru to get time in milliseconds
*
* @example
* (200).milliseconds(); //=> 200
* @return {Number} time in milliseconds
*/
Number.prototype.milliseconds = function() {
return this;
};
/**
* Alias for Number.prototype.milliseconds
*
* @see Number.prototype.milliseconds
* @example
* (100).ms(); //=> 100
* @return {Number} time in milliseconds
*/
Number.prototype.ms = Number.prototype.milliseconds;
/**
* Divides a number by 1000 to get time in microseconds
*
* @example
* (2000).microseconds(); //=> 2
* @return {Number} time in milliseconds
*/
Number.prototype.microseconds = function() {
return this / 1000;
};
/**
* Converts a number from a current scale to a 0 - 1 scale.
*

View File

@ -59,6 +59,24 @@ describe("monkey-patches", function() {
});
});
describe("#milliseconds", function() {
it("allows for expressing time in milliseconds", function() {
expect((5).milliseconds()).to.be.eql(5);
});
});
describe("#ms", function() {
it("allows for expressing time in milliseconds", function() {
expect((5).ms()).to.be.eql(5);
});
});
describe("#microseconds", function() {
it("allows for expressing time in microseconds", function() {
expect((5000).microseconds()).to.be.eql(5);
});
});
describe("#fromScale", function() {
it("converts a value from one scale to 0-1 scale", function() {
expect((5).fromScale(0, 10)).to.be.eql(0.5);