]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Added the asYear, asMonth, etc. getters to Durations 265/head
authorRocky Meza <rocky@fusionbox.com>
Sun, 15 Apr 2012 09:20:35 +0000 (03:20 -0600)
committerRocky Meza <rocky@fusionbox.com>
Sun, 15 Apr 2012 09:20:35 +0000 (03:20 -0600)
moment.js
test/moment/duration.js

index 1be02ecbecb9ab52a4abbf5b5d114a1c8e9ccecc..f936894e7aa5b5ce47b5018442ba6a9a3f200c07 100644 (file)
--- a/moment.js
+++ b/moment.js
         timezoneParseRegex = /([\+\-]|\d\d)/gi,
         VERSION = "1.5.0",
         shortcuts = 'Month|Date|Hours|Minutes|Seconds|Milliseconds'.split('|'),
-        durationGetters = 'years|months|days|hours|minutes|seconds|milliseconds'.split('|');
+        durationGetters = 'years|months|days|hours|minutes|seconds|milliseconds'.split('|'),
+        unitMillisecondFactors = {
+            'Milliseconds' : 1,
+            'Seconds' : 1e3,
+            'Minutes' : 6e4,
+            'Hours' : 36e5,
+            'Days' : 864e5,
+            'Weeks' : 6048e5,
+            'Months' : 2592e6,
+            'Years' : 31536e6
+        };
 
     // Moment prototype object
     function Moment(date, isUTC) {
         };
     }
 
+    function makeDurationAsGetter(name, factor) {
+        moment.duration.fn['as' + name] = function () {
+            return +this / factor;
+        };
+    }
+
     for (i = 0; i < durationGetters.length; i++) {
         makeDurationGetter(durationGetters[i]);
     }
 
+    for (i in unitMillisecondFactors) {
+        if (unitMillisecondFactors.hasOwnProperty(i)) {
+            makeDurationAsGetter(i, unitMillisecondFactors[i]);
+        }
+    }
+
     // CommonJS module is defined
     if (hasModule) {
         module.exports = moment;
index 46971280b095ef09deed8d5b111ba4f27d041085..37ac4546b5f597d12ecbc794f19d85ef941a2d4d 100644 (file)
@@ -14,13 +14,13 @@ exports.duration = {
         });
 
         test.expect(8);
-        test.equal(d.years(), 2, "years");
-        test.equal(d.months(), 3, "months");
-        test.equal(d.weeks(), 2, "weeks");
-        test.equal(d.days(), 15, "days"); // two weeks + 1 day
-        test.equal(d.hours(), 8, "hours");
-        test.equal(d.minutes(), 9, "minutes");
-        test.equal(d.seconds(), 20, "seconds");
+        test.equal(d.years(),        2,  "years");
+        test.equal(d.months(),       3,  "months");
+        test.equal(d.weeks(),        2,  "weeks");
+        test.equal(d.days(),         15, "days"); // two weeks + 1 day
+        test.equal(d.hours(),        8,  "hours");
+        test.equal(d.minutes(),      9,  "minutes");
+        test.equal(d.seconds(),      20, "seconds");
         test.equal(d.milliseconds(), 12, "milliseconds");
         test.done();
     },
@@ -178,6 +178,30 @@ exports.duration = {
         test.done();
     },
 
+    "asGetters" : function(test) {
+        var d = moment.duration({
+            years: 2,
+            months: 3,
+            weeks: 2,
+            days: 1,
+            hours: 8,
+            minutes: 9,
+            seconds: 20,
+            milliseconds: 12
+        });
+
+        test.expect(8);
+        test.equal(Math.round(d.asYears() * 100) / 100,   2.26,        "years");
+        test.equal(Math.round(d.asMonths() * 100) / 100,  27.51,       "months");
+        test.equal(Math.round(d.asWeeks() * 100) / 100,   117.91,      "weeks");
+        test.equal(Math.round(d.asDays() * 100) / 100,    825.34,      "days");
+        test.equal(Math.round(d.asHours() * 100) / 100,   19808.16,    "hours");
+        test.equal(Math.round(d.asMinutes() * 100) / 100, 1188489.33,  "minutes");
+        test.equal(Math.round(d.asSeconds() * 100) / 100, 71309360.01, "seconds");
+        test.equal(d.asMilliseconds(),                    71309360012, "milliseconds");
+        test.done();
+    },
+
     "isDuration" : function(test) {
         test.expect(3);
         test.ok(moment.isDuration(moment.duration(12345678)), "correctly says true");