]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
internal normalization of unit strings
authorIsaac Cambron <icambron@gmail.com>
Sun, 7 Apr 2013 03:42:03 +0000 (23:42 -0400)
committerIsaac Cambron <icambron@gmail.com>
Sun, 7 Apr 2013 03:42:03 +0000 (23:42 -0400)
moment.js
test/moment/duration.js

index 8ded4d0446036faa1b71f0fe6752ff7a32d91b51..bbda1c1dc6c05ec9e3d35cdd85b7b65bad52a0a7 100644 (file)
--- a/moment.js
+++ b/moment.js
             'Years' : 31536e6
         },
 
+        unitAliases = {
+            ms : "millisecond",
+            s : "second",
+            m : "minute",
+            h : "hour",
+            d : "day",
+            w : "week",
+            M : "month",
+            y : "year"
+        },
+
         // format function strings
         formatFunctions = {},
 
     }
     formatTokenFunctions.DDDD = padToken(formatTokenFunctions.DDD, 3);
 
-
     /************************************
         Constructors
     ************************************/
         return diffs + lengthDiff;
     }
 
+    function normalizeUnits(units) {
+        return unitAliases[units] || units.toLowerCase().replace(/(.)s$/, "$1");
+    }
 
     /************************************
         Languages
         },
 
         startOf: function (units) {
-            units = units.replace(/(.)s$/, "$1");
+            units = normalizeUnits(units);
             // the following switch intentionally omits break keywords
             // to utilize falling through the cases.
             switch (units) {
             case 'year':
-            case 'y':
                 this.month(0);
                 /* falls through */
             case 'month':
-            case 'M':
                 this.date(1);
                 /* falls through */
             case 'week':
-            case 'w':
             case 'day':
-            case 'd':
                 this.hours(0);
                 /* falls through */
             case 'hour':
-            case 'h':
                 this.minutes(0);
                 /* falls through */
             case 'minute':
-            case 'm':
                 this.seconds(0);
                 /* falls through */
             case 'second':
-            case 's': 
                 this.milliseconds(0);
                 /* falls through */
             }
 
             // weeks are a special case
-            if (units === 'week' || units === 'w') {
+            if (units === 'week') {
                 this.day(0);
             }
 
         },
 
         get : function (units) {
-            return this[units.toLowerCase()]();
+            units = normalizeUnits(units);
+            return this[units.toLowerCase() + "s"]();
         },
 
         as : function (units) {
-            var loweredUnits = units.toLowerCase();
-            return this["as" + loweredUnits.charAt(0).toUpperCase() + loweredUnits.slice(1)]();
+            units = normalizeUnits(units);
+            return this["as" + units.charAt(0).toUpperCase() + units.slice(1) + "s"]();
         },
 
         lang : moment.fn.lang
index b66b4b83f30ea6c5b236d9a2a3542a98e75727d1..d55fd355ce2f8031d02f30f03af70dc37b53a7f0 100644 (file)
@@ -66,15 +66,31 @@ exports.duration = {
     },
 
     "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.expect(24);
+        test.equal(moment.duration(1, "years").get("years"),                1, "years");
+        test.equal(moment.duration(1, "years").get("year"),                 1, "years = year");
+        test.equal(moment.duration(1, "years").get("y"),                    1, "years = y");
+        test.equal(moment.duration(2, "months").get("months"),              2, "months");
+        test.equal(moment.duration(2, "months").get("month"),               2, "months = month");
+        test.equal(moment.duration(2, "months").get("M"),                   2, "months = M");
+        test.equal(moment.duration(3, "weeks").get("weeks"),                3, "weeks");
+        test.equal(moment.duration(3, "weeks").get("week"),                 3, "weeks = week");
+        test.equal(moment.duration(3, "weeks").get("w"),                    3, "weeks = w");
+        test.equal(moment.duration(4, "days").get("days"),                  4, "days");
+        test.equal(moment.duration(4, "days").get("day"),                   4, "days = day");
+        test.equal(moment.duration(4, "days").get("d"),                     4, "days = d");
+        test.equal(moment.duration(5, "hours").get("hours"),                5, "hours");
+        test.equal(moment.duration(5, "hours").get("hour"),                 5, "hours = hour");
+        test.equal(moment.duration(5, "hours").get("h"),                    5, "hours = h");
+        test.equal(moment.duration(6, "minutes").get("minutes"),            6, "minutes");
+        test.equal(moment.duration(6, "minutes").get("minute"),             6, "minutes = minute");
+        test.equal(moment.duration(6, "minutes").get("m"),                  6, "minutes = m");
+        test.equal(moment.duration(7, "seconds").get("seconds"),            7, "seconds");
+        test.equal(moment.duration(7, "seconds").get("second"),             7, "seconds = second");
+        test.equal(moment.duration(7, "seconds").get("s"),                  7, "seconds = s");
+        test.equal(moment.duration(8, "milliseconds").get("milliseconds"),  8, "milliseconds");
+        test.equal(moment.duration(8, "milliseconds").get("millisecond"),   8, "milliseconds = millisecond");
+        test.equal(moment.duration(8, "milliseconds").get("ms"),            8, "milliseconds = ms");
         test.done();
     },
 
@@ -304,15 +320,31 @@ exports.duration = {
             milliseconds: 12
         });
 
-        test.expect(8);
+        test.expect(24);
         test.equal(d.as("years").toFixed(2),          "2.29", "years");
-        test.equal(d.as("months").toFixed(2),        "27.51", "months");
-        test.equal(d.as("weeks").toFixed(2),        "119.33", "weeks");
-        test.equal(d.as("days").toFixed(2),         "835.34", "days");
-        test.equal(d.as("hours").toFixed(2),      "20048.16", "hours");
-        test.equal(d.as("minutes").toFixed(2),  "1202889.33", "minutes");
-        test.equal(d.as("seconds").toFixed(2), "72173360.01", "seconds");
-        test.equal(d.as("milliseconds"),         72173360012, "milliseconds");
+        test.equal(d.as("year").toFixed(2),           "2.29", "years = year");
+        test.equal(d.as("y").toFixed(2),              "2.29", "years = y");
+        test.equal(d.as("months").toFixed(2),         "27.51", "months");
+        test.equal(d.as("month").toFixed(2),          "27.51", "months = month");
+        test.equal(d.as("M").toFixed(2),              "27.51", "months = M");
+        test.equal(d.as("weeks").toFixed(2),          "119.33", "weeks");
+        test.equal(d.as("week").toFixed(2),           "119.33", "weeks = week");
+        test.equal(d.as("w").toFixed(2),              "119.33", "weeks = w");
+        test.equal(d.as("days").toFixed(2),           "835.34", "days");
+        test.equal(d.as("day").toFixed(2),            "835.34", "days = day");
+        test.equal(d.as("d").toFixed(2),              "835.34", "days = d");
+        test.equal(d.as("hours").toFixed(2),          "20048.16", "hours");
+        test.equal(d.as("hour").toFixed(2),           "20048.16", "hours = hour");
+        test.equal(d.as("h").toFixed(2),              "20048.16", "hours = h");
+        test.equal(d.as("minutes").toFixed(2),        "1202889.33", "minutes");
+        test.equal(d.as("minute").toFixed(2),         "1202889.33", "minutes = minute");
+        test.equal(d.as("m").toFixed(2),              "1202889.33", "minutes = m");
+        test.equal(d.as("seconds").toFixed(2),        "72173360.01", "seconds");
+        test.equal(d.as("second").toFixed(2),         "72173360.01", "seconds = second");
+        test.equal(d.as("s").toFixed(2),              "72173360.01", "seconds = s");
+        test.equal(d.as("milliseconds"),              72173360012, "milliseconds");
+        test.equal(d.as("millisecond"),               72173360012, "milliseconds = millisecond");
+        test.equal(d.as("ms"),                        72173360012, "milliseconds = ms");
         test.done();
     },