]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Adding in month and weekday callback functions and tests.
authorTim Wood <washwithcare@gmail.com>
Tue, 12 Jun 2012 18:22:23 +0000 (11:22 -0700)
committerTim Wood <washwithcare@gmail.com>
Tue, 12 Jun 2012 18:22:23 +0000 (11:22 -0700)
#313 #325 #312

moment.js
test/moment/lang.js

index 502aaf72b8814568f83dd4da2c40794dd5ddb8e2..b8471be488173efc19ab7d462c1fa8cedb272981 100644 (file)
--- a/moment.js
+++ b/moment.js
     // are provided, it will load the language file module.  As a convenience,
     // this function also returns the language values.
     function loadLang(key, values) {
-        var i,
+        var i, m,
             parse = [];
 
         if (!values && hasModule) {
         }
 
         for (i = 0; i < 12; i++) {
-            parse[i] = new RegExp('^' + values.months[i] + '|^' + values.monthsShort[i].replace('.', ''), 'i');
+            m = moment([2000, i]);
+            parse[i] = new RegExp('^' + (values.months[i] || values.months(m, '')) + 
+                '|^' + (values.monthsShort[i] || values.monthsShort(m, '')).replace('.', ''), 'i');
         }
         values.monthsParse = values.monthsParse || parse;
 
         }
     };
 
+    // returns language data
+    moment.langData = getLangDefinition;
+
+    // compare moment object
+    moment.isMoment = function (obj) {
+        return obj instanceof Moment;
+    };
+
+    // for typechecking Duration objects
+    moment.isDuration = function (obj) {
+        return obj instanceof Duration;
+    };
+
     // Set default language, other languages will inherit from English.
     moment.lang('en', {
         months : "January_February_March_April_May_June_July_August_September_October_November_December".split("_"),
         }
     });
 
-    // returns language data
-    moment.langData = getLangDefinition;
-
-    // compare moment object
-    moment.isMoment = function (obj) {
-        return obj instanceof Moment;
-    };
-
-    // for typechecking Duration objects
-    moment.isDuration = function (obj) {
-        return obj instanceof Duration;
-    };
-
 
     /************************************
         Moment Prototype
index 364edc583f8f5e09fef975f6a5dd06533b02731c..09afb5ed5f2028275e2794ea7fa6b4e363643603 100644 (file)
@@ -113,6 +113,34 @@ exports.lang = {
         test.equal(a.from(b), 'hace un día', 'preserve language of first moment');
         test.equal(b.from(a), 'in a day', 'do not preserve language of second moment');
 
+        test.done();
+    },
+
+    "month name callback function" : function(test) {
+        test.expect(3);
+
+        function fakeReplace(m, format) {
+            if (/test/.test(format)) {
+                return "test";
+            }
+            if (m.date() === 1) {
+                return "date";
+            }
+            return 'default';
+        }
+
+        moment.lang('made-up', {
+            months : fakeReplace,
+            monthsShort : fakeReplace,
+            weekdays : fakeReplace,
+            weekdaysShort : fakeReplace,
+            weekdaysMin : fakeReplace
+        });
+
+        test.equal(moment().format('[test] dd ddd dddd MMM MMMM'), 'test test test test test test', 'format month name function should be able to access the format string');
+        test.equal(moment([2011, 0, 1]).format('dd ddd dddd MMM MMMM'), 'date date date date date', 'format month name function should be able to access the moment object');
+        test.equal(moment([2011, 0, 2]).format('dd ddd dddd MMM MMMM'), 'default default default default default', 'format month name function should be able to access the moment object');
+
         test.done();
     }
 };