]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Immutable internal implementations of Moment#locale and Duration#locale
authorLucas Sanders <butterflyhug@google.com>
Sun, 19 Mar 2017 04:12:50 +0000 (00:12 -0400)
committerLucas Sanders <butterflyhug@google.com>
Wed, 22 Mar 2017 11:00:20 +0000 (07:00 -0400)
src/lib/duration/locale.js [new file with mode: 0644]
src/lib/duration/prototype.js
src/lib/moment/locale.js
src/lib/moment/prototype.js

diff --git a/src/lib/duration/locale.js b/src/lib/duration/locale.js
new file mode 100644 (file)
index 0000000..58788b7
--- /dev/null
@@ -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;
+}
index ff6c70ae7e7b6ab667e25e60b4045eb3ad335caf..7b0c5469e8c5f602d015e29b25a7c5bf6432035c 100644 (file)
@@ -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);
index 27d8ab036fa0a7a1fc470f3196930d82cfce02ff..cb8f466796ccc23fbe5901583b205fca2baac5de 100644 (file)
@@ -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;
     }
 }
 
index 4a94345a08dd775ea4f7fba8bf6af276ba145ba1..1a494f4077782673632f3025aba8c36a2d4696d7 100644 (file)
@@ -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);