]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
adding as() and get() to durations 701/head
authorIsaac Cambron <icambron@gmail.com>
Sun, 31 Mar 2013 19:45:19 +0000 (15:45 -0400)
committerIsaac Cambron <icambron@gmail.com>
Sun, 31 Mar 2013 19:45:19 +0000 (15:45 -0400)
moment.js
test/moment/duration.js

index 4be1d2e6ed630dbd9e0ed6493a42449901aab056..ac684736035df2efd9497d9276c20ddaf5c15674 100644 (file)
--- a/moment.js
+++ b/moment.js
             return this.lang().postformat(output);
         },
 
+        get : function (units) {
+            return this[units.toLowerCase()]();
+        },
+
+        as : function (units) {
+            var loweredUnits = units.toLowerCase();
+            return this["as" + loweredUnits.charAt(0).toUpperCase() + loweredUnits.slice(1)]();
+        },
+
         lang : moment.fn.lang
     };
 
index 37ac4546b5f597d12ecbc794f19d85ef941a2d4d..f8666e8084ac004e91909003eebfb0eadd3ef7e8 100644 (file)
@@ -65,6 +65,19 @@ exports.duration = {
         test.done();
     },
 
+    "generic getter" : function(test) {
+        test.expect(8);
+        test.equal(moment.duration({y: 1}).get("years"),         1, "years = y");
+        test.equal(moment.duration({M: 2}).get("months"),        2, "months = M");
+        test.equal(moment.duration({w: 3}).get("weeks"),         3, "weeks = w");
+        test.equal(moment.duration({d: 4}).get("days"),          4, "days = d");
+        test.equal(moment.duration({h: 5}).get("hours"),         5, "hours = h");
+        test.equal(moment.duration({m: 6}).get("minutes"),       6, "minutes = m");
+        test.equal(moment.duration({s: 7}).get("seconds"),       7, "seconds = s");
+        test.equal(moment.duration({ms: 8}).get("milliseconds"), 8, "milliseconds = ms");
+        test.done();
+    },
+
     "instantiation from another duration" : function(test) {
         var simple = moment.duration(1234),
             complicated = moment.duration({
@@ -202,6 +215,30 @@ exports.duration = {
         test.done();
     },
 
+    "generic as getter" : 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.as("years") * 100) / 100,   2.26,        "years");
+        test.equal(Math.round(d.as("months") * 100) / 100,  27.51,       "months");
+        test.equal(Math.round(d.as("weeks") * 100) / 100,   117.91,      "weeks");
+        test.equal(Math.round(d.as("days") * 100) / 100,    825.34,      "days");
+        test.equal(Math.round(d.as("hours") * 100) / 100,   19808.16,    "hours");
+        test.equal(Math.round(d.as("minutes") * 100) / 100, 1188489.33,  "minutes");
+        test.equal(Math.round(d.as("seconds") * 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");