Skip to content

Commit

Permalink
Fix moment#3883 handle creating moment with child locale before paren…
Browse files Browse the repository at this point in the history
…t is defined. Added warning and test
  • Loading branch information
cmyers committed Nov 18, 2017
1 parent 22afa81 commit dd94bd5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/lib/create/from-anything.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { getLocale } from '../locale/locales';
import { hooks } from '../utils/hooks';
import checkOverflow from './check-overflow';
import { isValid } from './valid';
import { deprecateSimple } from '../utils/deprecate';

import { configFromStringAndArray } from './from-string-and-array';
import { configFromStringAndFormat } from './from-string-and-format';
Expand All @@ -35,6 +36,11 @@ export function prepareConfig (config) {

config._locale = config._locale || getLocale(config._l);

if (!config._locale) {
deprecateSimple('localeUndefined', 'defined locale can not be used until parentLocale is defined. Using default global.');
config._locale = getLocale();
}

if (input === null || (format === undefined && input === '')) {
return createInvalid({nullInput: true});
}
Expand Down
7 changes: 6 additions & 1 deletion src/test/moment/locale_inheritance.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,13 @@ test('define child locale before parent', function (assert) {

moment.defineLocale('months-x', {
parentLocale: 'base-months-x',
months : 'First_Second_Third_Fourth_Fifth_Sixth_Seventh_Eighth_Ninth_Tenth_Eleventh_Twelveth '.split('_')
months: 'First_Second_Third_Fourth_Fifth_Sixth_Seventh_Eighth_Ninth_Tenth_Eleventh_Twelveth '.split('_')
});

assert.throws(function () {
moment('08:00:00 01/First/00', 'HH:mm:ss DD/MMM/YY', 'months-x');
}, Error, 'should throw error if child used before parent is defined');

assert.equal(moment.locale(), 'en', 'failed to set a locale requiring missing parent');
moment.defineLocale('base-months-x', {
months : 'One_Two_Three_Four_Five_Six_Seven_Eight_Nine_Ten_Eleven_Twelve'.split('_')
Expand Down

0 comments on commit dd94bd5

Please sign in to comment.