From: Tim Wood Date: Thu, 5 Jul 2012 17:46:31 +0000 (-0700) Subject: Backwards compatibility with changing moment.months and other lang properties X-Git-Tag: 1.7.0~11 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c018776284d0bcd9b3a4e67160336ff7b85915ae;p=thirdparty%2Fmoment.git Backwards compatibility with changing moment.months and other lang properties #332 #334 --- diff --git a/moment.js b/moment.js index f7d472a23..c959ab31d 100644 --- a/moment.js +++ b/moment.js @@ -322,9 +322,9 @@ function getLangDefinition(m) { var langKey = (typeof m === 'string') && m || m && m._lang || - currentLanguage; + null; - return languages[langKey] || loadLang(langKey); + return langKey ? (languages[langKey] || loadLang(langKey)) : moment; } @@ -729,6 +729,7 @@ for (i = 0; i < langConfigProperties.length; i++) { moment[langConfigProperties[i]] = languages[key][langConfigProperties[i]]; } + moment.monthsParse = languages[key].monthsParse; currentLanguage = key; } }; diff --git a/test/moment/lang.js b/test/moment/lang.js index 09afb5ed5..b4823f197 100644 --- a/test/moment/lang.js +++ b/test/moment/lang.js @@ -141,6 +141,57 @@ exports.lang = { 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(); + }, + + // the following tests should be removed after the 2.0.0 release as they will be deprecated + "lang accessors on the global object should exist < 2.0.0" : function(test) { + moment.lang('en'); + + var a = 'months|monthsShort|monthsParse|weekdays|weekdaysShort|weekdaysMin|longDateFormat|calendar|relativeTime|ordinal|meridiem'.split('|'); + var i; + + test.expect(a.length); + + for (i = 0; i < a.length; i++) { + test.ok(moment[a[i]], "moment." + a[i] + " should exist"); + } + + test.done(); + }, + + // the following tests should be removed after the 2.0.0 release as they will be deprecated + "lang accessors on the global object should change < 2.0.0" : function(test) { + moment.lang('en'); + + var a = 'months|monthsShort|weekdays|weekdaysShort|weekdaysMin|longDateFormat|calendar|relativeTime|ordinal'.split('|'); + var i; + var en = {}; + + test.expect(a.length); + + for (i = 0; i < a.length; i++) { + en[a[i]] = moment[a[i]]; + } + + moment.lang('fr'); + + for (i = 0; i < a.length; i++) { + test.notDeepEqual(en[a[i]], moment[a[i]], "the " + a[i] + " lang data should change on the global object"); + } + + test.done(); + }, + + "manip lang accessors on the global object < 2.0.0" : function(test) { + test.expect(1); + moment.lang('en'); + + moment.months = ["test"]; + test.equal(moment([2011, 0]).format('MMMM'), "test", "Should be able to manipulate the objects on the global object"); + + moment.lang('en'); + test.done(); } };