diff --git a/lib/utils/monkey-patches.js b/lib/utils/monkey-patches.js index 90a2cd2..69bf24b 100644 --- a/lib/utils/monkey-patches.js +++ b/lib/utils/monkey-patches.js @@ -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. * diff --git a/spec/lib/utils/monkey-patches.spec.js b/spec/lib/utils/monkey-patches.spec.js index 10db257..57db358 100644 --- a/spec/lib/utils/monkey-patches.spec.js +++ b/spec/lib/utils/monkey-patches.spec.js @@ -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);