From 012578f012a6e23efd023384909ad37750048c9d Mon Sep 17 00:00:00 2001 From: deadprogram Date: Mon, 31 Aug 2015 19:24:23 -0700 Subject: [PATCH] Convenience functions for milliseconds and microseconds --- lib/utils/monkey-patches.js | 32 +++++++++++++++++++++++++++ spec/lib/utils/monkey-patches.spec.js | 18 +++++++++++++++ 2 files changed, 50 insertions(+) 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);