From: Lucas Sanders Date: Sun, 19 Mar 2017 04:12:50 +0000 (-0400) Subject: Immutable internal implementations of Moment#locale and Duration#locale X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=13e159c13ced91b73204bdceafb32ee453dbdc57;p=thirdparty%2Fmoment.git Immutable internal implementations of Moment#locale and Duration#locale --- diff --git a/src/lib/duration/locale.js b/src/lib/duration/locale.js new file mode 100644 index 000000000..58788b7c8 --- /dev/null +++ b/src/lib/duration/locale.js @@ -0,0 +1,32 @@ +import { Duration } from './constructor'; +import { getLocale } from '../locale/locales'; + +// If passed a locale key, it will return a cloned instance that is set +// to the specified locale. Otherwise, it will return the name of the +// locale that is set on this instance. +export function locale (key) { + var clone, newLocaleData; + + if (key === undefined) { + return this._locale._abbr; + } else { + clone = new Duration(this); + newLocaleData = getLocale(key); + if (newLocaleData != null) { + clone._locale = newLocaleData; + } + return clone; + } +} + +export function lang (key) { + if (key === undefined) { + return this.localeData(); + } else { + return this.locale(key); + } +} + +export function localeData () { + return this._locale; +} diff --git a/src/lib/duration/prototype.js b/src/lib/duration/prototype.js index ff6c70ae7..7b0c5469e 100644 --- a/src/lib/duration/prototype.js +++ b/src/lib/duration/prototype.js @@ -1,8 +1,4 @@ import { Duration } from './constructor'; -import wrap from '../utils/wrap'; - -var proto = Duration.prototype; - import { abs } from './abs'; import { add, subtract } from './add-subtract'; import { as, asMilliseconds, asSeconds, asMinutes, asHours, asDays, asWeeks, asMonths, asYears, valueOf } from './as'; @@ -10,9 +6,10 @@ import { bubble } from './bubble'; import { get, milliseconds, seconds, minutes, hours, days, months, years, weeks } from './get'; import { humanize } from './humanize'; import { toISOString } from './iso-string'; -import { lang, locale, localeData } from '../moment/locale'; +import { lang, locale, localeData } from './locale'; import { isValid } from './valid'; +var proto = Duration.prototype; proto.isValid = isValid; proto.abs = abs; proto.add = add; @@ -40,11 +37,11 @@ proto.humanize = humanize; proto.toISOString = toISOString; proto.toString = toISOString; proto.toJSON = toISOString; -proto.locale = wrap(Duration, locale, true); +proto.locale = locale; proto.localeData = localeData; // Deprecations import { deprecate } from '../utils/deprecate'; proto.toIsoString = deprecate('toIsoString() is deprecated. Please use toISOString() instead (notice the capitals)', toISOString); -proto.lang = deprecate('duration.lang() is deprecated. Use locale() or localeData() instead.', wrap(Duration, lang)); +proto.lang = deprecate('duration.lang() is deprecated. Use locale() or localeData() instead.', lang); diff --git a/src/lib/moment/locale.js b/src/lib/moment/locale.js index 27d8ab036..cb8f46679 100644 --- a/src/lib/moment/locale.js +++ b/src/lib/moment/locale.js @@ -1,20 +1,21 @@ +import { Moment } from './constructor'; import { getLocale } from '../locale/locales'; -import { deprecate } from '../utils/deprecate'; -// If passed a locale key, it will set the locale for this -// instance. Otherwise, it will return the locale configuration -// variables for this instance. +// If passed a locale key, it will return a cloned instance that is set +// to the specified locale. Otherwise, it will return the name of the +// locale that is set on this instance. export function locale (key) { - var newLocaleData; + var clone, newLocaleData; if (key === undefined) { return this._locale._abbr; } else { + clone = new Moment(this); newLocaleData = getLocale(key); if (newLocaleData != null) { - this._locale = newLocaleData; + clone._locale = newLocaleData; } - return this; + return clone; } } diff --git a/src/lib/moment/prototype.js b/src/lib/moment/prototype.js index 4a94345a0..1a494f407 100644 --- a/src/lib/moment/prototype.js +++ b/src/lib/moment/prototype.js @@ -1,8 +1,4 @@ import { Moment } from './constructor'; -import wrap from '../utils/wrap'; - -var proto = Moment.prototype; - import { add, subtract } from './add-subtract'; import { calendar, getCalendarFormat } from './calendar'; import { isBefore, isBetween, isSame, isAfter, isSameOrAfter, isSameOrBefore } from './compare'; @@ -17,7 +13,9 @@ import { startOf, endOf } from './start-end-of'; import { valueOf, toDate, toArray, toObject, toJSON, unix } from './to-type'; import { isValid, parsingFlags, invalidAt } from './valid'; import { creationData } from './creation-data'; +import wrap from '../utils/wrap'; +var proto = Moment.prototype; proto.add = wrap(Moment, add); proto.calendar = calendar; proto.diff = diff; @@ -36,7 +34,7 @@ proto.isSame = isSame; proto.isSameOrAfter = isSameOrAfter; proto.isSameOrBefore = isSameOrBefore; proto.isValid = isValid; -proto.locale = wrap(Moment, locale); +proto.locale = locale; proto.localeData = localeData; proto.parsingFlags = parsingFlags; proto.set = wrap(Moment, stringSet); @@ -147,7 +145,7 @@ proto.dates = deprecate( wrap(Moment, getSetDayOfMonth, true)); proto.lang = deprecate( 'moment().lang() is deprecated. Instead, use moment().localeData() to get the language configuration. Use moment().locale() to change languages.', - wrap(Moment, lang)); + lang); proto.max = deprecate( 'moment().max() is deprecated. Use moment.min() instead (notice lack of parentheses).', prototypeMax);