From: Iskren Chernev Date: Thu, 16 Jun 2016 06:34:09 +0000 (-0700) Subject: Replace hacks with harder to spot ones X-Git-Tag: 2.14.0~9^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=081a4827425c097fe1ad12cd3da229470509d8fd;p=thirdparty%2Fmoment.git Replace hacks with harder to spot ones --- diff --git a/src/lib/locale/base-config.js b/src/lib/locale/base-config.js index 9a6cb1102..cbb0b36b4 100644 --- a/src/lib/locale/base-config.js +++ b/src/lib/locale/base-config.js @@ -8,8 +8,6 @@ import { defaultRelativeTime } from './relative'; import { defaultLocaleMonths, defaultLocaleMonthsShort, - defaultMonthsRegex, - defaultMonthsShortRegex, } from '../units/month'; // week @@ -20,10 +18,6 @@ import { defaultLocaleWeekdays, defaultLocaleWeekdaysMin, defaultLocaleWeekdaysShort, - - defaultWeekdaysRegex, - defaultWeekdaysShortRegex, - defaultWeekdaysMinRegex, } from '../units/day-of-week'; // meridiem @@ -39,8 +33,6 @@ export var baseConfig = { months: defaultLocaleMonths, monthsShort: defaultLocaleMonthsShort, - monthsRegex: defaultMonthsRegex, - monthsShortRegex: defaultMonthsShortRegex, week: defaultLocaleWeek, @@ -48,9 +40,5 @@ export var baseConfig = { weekdaysMin: defaultLocaleWeekdaysMin, weekdaysShort: defaultLocaleWeekdaysShort, - weekdaysRegex: defaultWeekdaysRegex, - weekdaysShortRegex: defaultWeekdaysShortRegex, - weekdaysMinRegex: defaultWeekdaysMinRegex, - meridiemParse: defaultLocaleMeridiemParse }; diff --git a/src/lib/locale/locales.js b/src/lib/locale/locales.js index 9b3565cfd..abdafcea7 100644 --- a/src/lib/locale/locales.js +++ b/src/lib/locale/locales.js @@ -82,16 +82,8 @@ export function getSetGlobalLocale (key, values) { return globalLocale._abbr; } -function sanitizeLocaleConfig(config) { - if (!hasOwnProp(config, 'longDateFormat')) { - config.longDateFormat = {}; - } - return config; -} - export function defineLocale (name, config) { if (config !== null) { - config = sanitizeLocaleConfig(config); var parentConfig = baseConfig; config.abbr = name; if (locales[name] != null) { diff --git a/src/lib/locale/set.js b/src/lib/locale/set.js index 1fd138902..45a2f46e1 100644 --- a/src/lib/locale/set.js +++ b/src/lib/locale/set.js @@ -34,5 +34,13 @@ export function mergeConfigs(parentConfig, childConfig) { } } } + for (prop in parentConfig) { + if (hasOwnProp(parentConfig, prop) && + !hasOwnProp(childConfig, prop) && + isObject(parentConfig[prop])) { + // make sure changes to properties don't modify parent config + res[prop] = extend({}, res[prop]); + } + } return res; } diff --git a/src/lib/units/day-of-week.js b/src/lib/units/day-of-week.js index de582255a..04968c649 100644 --- a/src/lib/units/day-of-week.js +++ b/src/lib/units/day-of-week.js @@ -257,7 +257,7 @@ export function getSetISODayOfWeek (input) { } } -export var defaultWeekdaysRegex = matchWord; +var defaultWeekdaysRegex = matchWord; export function weekdaysRegex (isStrict) { if (this._weekdaysParseExact) { if (!hasOwnProp(this, '_weekdaysRegex')) { @@ -269,12 +269,15 @@ export function weekdaysRegex (isStrict) { return this._weekdaysRegex; } } else { + if (!hasOwnProp(this, '_weekdaysRegex')) { + this._weekdaysRegex = defaultWeekdaysRegex; + } return this._weekdaysStrictRegex && isStrict ? this._weekdaysStrictRegex : this._weekdaysRegex; } } -export var defaultWeekdaysShortRegex = matchWord; +var defaultWeekdaysShortRegex = matchWord; export function weekdaysShortRegex (isStrict) { if (this._weekdaysParseExact) { if (!hasOwnProp(this, '_weekdaysRegex')) { @@ -286,16 +289,18 @@ export function weekdaysShortRegex (isStrict) { return this._weekdaysShortRegex; } } else { + if (!hasOwnProp(this, '_weekdaysShortRegex')) { + this._weekdaysShortRegex = defaultWeekdaysShortRegex; + } return this._weekdaysShortStrictRegex && isStrict ? this._weekdaysShortStrictRegex : this._weekdaysShortRegex; } } -export var defaultWeekdaysMinRegex = matchWord; +var defaultWeekdaysMinRegex = matchWord; export function weekdaysMinRegex (isStrict) { if (this._weekdaysParseExact) { - if (!hasOwnProp(this, '_weekdaysRegex') || - this._weekdaysRegex === defaultWeekdaysRegex) { + if (!hasOwnProp(this, '_weekdaysRegex')) { computeWeekdaysParse.call(this); } if (isStrict) { @@ -304,6 +309,9 @@ export function weekdaysMinRegex (isStrict) { return this._weekdaysMinRegex; } } else { + if (!hasOwnProp(this, '_weekdaysMinRegex')) { + this._weekdaysMinRegex = defaultWeekdaysMinRegex; + } return this._weekdaysMinStrictRegex && isStrict ? this._weekdaysMinStrictRegex : this._weekdaysMinRegex; } @@ -350,3 +358,9 @@ function computeWeekdaysParse () { this._weekdaysShortStrictRegex = new RegExp('^(' + shortPieces.join('|') + ')', 'i'); this._weekdaysMinStrictRegex = new RegExp('^(' + minPieces.join('|') + ')', 'i'); } + +function setDefaultRegexes () { + this._weekdaysRegex = defaultWeekdaysRegex; + this._weekdaysShortRegex = defaultWeekdaysShortRegex; + this._weekdaysMinRegex = defaultWeekdaysMinRegex; +} diff --git a/src/lib/units/month.js b/src/lib/units/month.js index 0a3fd9565..57e52bde2 100644 --- a/src/lib/units/month.js +++ b/src/lib/units/month.js @@ -199,12 +199,10 @@ export function getDaysInMonth () { return daysInMonth(this.year(), this.month()); } -export var defaultMonthsRegex = matchWord; -export var defaultMonthsShortRegex = matchWord; +var defaultMonthsShortRegex = matchWord; export function monthsShortRegex (isStrict) { if (this._monthsParseExact) { - if (!hasOwnProp(this, '_monthsRegex') || - this._monthsRegex === defaultMonthsRegex) { + if (!hasOwnProp(this, '_monthsRegex')) { computeMonthsParse.call(this); } if (isStrict) { @@ -213,15 +211,18 @@ export function monthsShortRegex (isStrict) { return this._monthsShortRegex; } } else { + if (!hasOwnProp(this, '_monthsShortRegex')) { + this._monthsShortRegex = defaultMonthsShortRegex; + } return this._monthsShortStrictRegex && isStrict ? this._monthsShortStrictRegex : this._monthsShortRegex; } } +var defaultMonthsRegex = matchWord; export function monthsRegex (isStrict) { if (this._monthsParseExact) { - if (!hasOwnProp(this, '_monthsRegex') || - this._monthsRegex === defaultMonthsRegex) { + if (!hasOwnProp(this, '_monthsRegex')) { computeMonthsParse.call(this); } if (isStrict) { @@ -230,6 +231,9 @@ export function monthsRegex (isStrict) { return this._monthsRegex; } } else { + if (!hasOwnProp(this, '_monthsRegex')) { + this._monthsRegex = defaultMonthsRegex; + } return this._monthsStrictRegex && isStrict ? this._monthsStrictRegex : this._monthsRegex; } diff --git a/src/test/moment/locale_inheritance.js b/src/test/moment/locale_inheritance.js index 2fec56e28..f0cb21fdd 100644 --- a/src/test/moment/locale_inheritance.js +++ b/src/test/moment/locale_inheritance.js @@ -142,15 +142,15 @@ test('ordinal parse', function (assert) { assert.ok(moment.utc('2015-01-1y', 'YYYY-MM-Do', true).isValid(), 'ordinal parse uses child'); - // moment.defineLocale('base-ordinal-parse-2', { - // ordinalParse : /\d{1,2}x/ - // }); - // moment.defineLocale('child-ordinal-parse-2', { - // parentLocale: 'base-ordinal-parse-2', - // ordinalParse : null - // }); - - // assert.ok(moment.utc('2015-01-1', 'YYYY-MM-Do', true).isValid(), 'ordinal parse uses child (default)'); + moment.defineLocale('base-ordinal-parse-2', { + ordinalParse : /\d{1,2}x/ + }); + moment.defineLocale('child-ordinal-parse-2', { + parentLocale: 'base-ordinal-parse-2', + ordinalParse : /\d{1,2}/ + }); + + assert.ok(moment.utc('2015-01-1', 'YYYY-MM-Do', true).isValid(), 'ordinal parse uses child (default)'); }); test('months', function (assert) { diff --git a/src/test/moment/locale_update.js b/src/test/moment/locale_update.js index 4008de2c0..fd189aad1 100644 --- a/src/test/moment/locale_update.js +++ b/src/test/moment/locale_update.js @@ -142,15 +142,15 @@ test('ordinal parse', function (assert) { assert.ok(moment.utc('2015-01-1y', 'YYYY-MM-Do', true).isValid(), 'ordinal parse uses child'); - // moment.defineLocale('ordinal-parse-2', null); - // moment.defineLocale('ordinal-parse-2', { - // ordinalParse : /\d{1,2}x/ - // }); - // moment.updateLocale('ordinal-parse-2', { - // ordinalParse : null - // }); - - // assert.ok(moment.utc('2015-01-1', 'YYYY-MM-Do', true).isValid(), 'ordinal parse uses child (default)'); + moment.defineLocale('ordinal-parse-2', null); + moment.defineLocale('ordinal-parse-2', { + ordinalParse : /\d{1,2}x/ + }); + moment.updateLocale('ordinal-parse-2', { + ordinalParse : /\d{1,2}/ + }); + + assert.ok(moment.utc('2015-01-1', 'YYYY-MM-Do', true).isValid(), 'ordinal parse uses child (default)'); }); test('months', function (assert) {