From: Tim Wood Date: Tue, 15 Jan 2013 19:45:08 +0000 (-0800) Subject: Allowing you to set values on a language after it has been loaded #580 X-Git-Tag: 2.0.0~22 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b68c4e631aadee14d30c93c414f33f28eb8cd8b2;p=thirdparty%2Fmoment.git Allowing you to set values on a language after it has been loaded #580 --- diff --git a/moment.js b/moment.js index 414fafdd3..70d6772a5 100644 --- a/moment.js +++ b/moment.js @@ -191,16 +191,8 @@ Constructors ************************************/ - function Language(config) { - var prop, i; - for (i in config) { - prop = config[i]; - if (typeof prop === 'function') { - this[i] = prop; - } else { - this['_' + i] = prop; - } - } + function Language() { + } // Moment prototype object @@ -338,6 +330,18 @@ Language.prototype = { + set : function (config) { + var prop, i; + for (i in config) { + prop = config[i]; + if (typeof prop === 'function') { + this[i] = prop; + } else { + this['_' + i] = prop; + } + } + }, + _months : "January_February_March_April_May_June_July_August_September_October_November_December".split("_"), months : function (m) { return this._months[m.month()]; @@ -470,7 +474,10 @@ // this function also returns the language values. function loadLang(key, values) { values.abbr = key; - languages[key] = new Language(values); + if (!languages[key]) { + languages[key] = new Language(); + } + languages[key].set(values); return languages[key]; } diff --git a/test/moment/lang.js b/test/moment/lang.js index a5d7328fd..8f8fb9513 100644 --- a/test/moment/lang.js +++ b/test/moment/lang.js @@ -180,56 +180,21 @@ exports.lang = { 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"); - } + "changing parts of a language config" : function (test) { + test.expect(2); - test.done(); - }, + moment.lang('partial-lang', { + months : 'a b c d e f g h i j k l'.split(' ') + }); - "manip lang accessors on the global object < 2.0.0" : function (test) { - test.expect(1); - moment.lang('en'); + test.equal(moment([2011, 0, 1]).format('MMMM'), 'a', 'should be able to set language values when creating the language'); - moment.months = ["test"]; - test.equal(moment([2011, 0]).format('MMMM'), "test", "Should be able to manipulate the objects on the global object"); + moment.lang('partial-lang', { + monthsShort : 'A B C D E F G H I J K L'.split(' ') + }); - moment.lang('en'); + test.equal(moment([2011, 0, 1]).format('MMMM MMM'), 'a A', 'should be able to set language values after creating the language'); test.done(); } -*/ };