From: Tim Wood Date: Tue, 12 Jun 2012 18:22:23 +0000 (-0700) Subject: Adding in month and weekday callback functions and tests. X-Git-Tag: 1.7.0~19 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cff663569710a7e89eb5fecbd2c95a155dfd37c1;p=thirdparty%2Fmoment.git Adding in month and weekday callback functions and tests. #313 #325 #312 --- diff --git a/moment.js b/moment.js index 502aaf72b..b8471be48 100644 --- a/moment.js +++ b/moment.js @@ -285,7 +285,7 @@ // 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) { @@ -300,7 +300,9 @@ } 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; @@ -731,6 +733,19 @@ } }; + // 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("_"), @@ -784,19 +799,6 @@ } }); - // 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 diff --git a/test/moment/lang.js b/test/moment/lang.js index 364edc583..09afb5ed5 100644 --- a/test/moment/lang.js +++ b/test/moment/lang.js @@ -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(); } };