From c68bb0a8206f0ea97f8bf31e8ad2f2e817a96486 Mon Sep 17 00:00:00 2001 From: Iskren Chernev Date: Fri, 29 Aug 2014 19:11:45 +0300 Subject: [PATCH] Fix moment().locale('ivalid-id') --- moment.js | 10 +++++++--- test/moment/locale.js | 12 ++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/moment.js b/moment.js index 868206aad..3b5d65adc 100644 --- a/moment.js +++ b/moment.js @@ -2448,10 +2448,15 @@ // instance. Otherwise, it will return the locale configuration // variables for this instance. locale : function (key) { + var newLocaleData; + if (key === undefined) { return this._locale._abbr; } else { - this._locale = moment.localeData(key); + newLocaleData = moment.localeData(key); + if (newLocaleData != null) { + this._locale = newLocaleData; + } return this; } }, @@ -2462,8 +2467,7 @@ if (key === undefined) { return this.localeData(); } else { - this._locale = moment.localeData(key); - return this; + return this.locale(key); } } ), diff --git a/test/moment/locale.js b/test/moment/locale.js index bf0bcdefb..57ecb4ae9 100644 --- a/test/moment/locale.js +++ b/test/moment/locale.js @@ -458,5 +458,17 @@ exports.locale = { test.equal(m.locale(), 'fr', 'instance locale reset to global locale'); test.done(); + }, + + 'moment().locale with missing key doesn\'t change locale' : function (test) { + test.equal(moment().locale('boo').localeData(), moment.localeData(), + 'preserve global locale in case of bad locale id'); + test.done(); + }, + + 'moment().lang with missing key doesn\'t change locale' : function (test) { + test.equal(moment().lang('boo').localeData(), moment.localeData(), + 'preserve global locale in case of bad locale id'); + test.done(); } }; -- 2.47.2