]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Finish removing wrap() in favor of immutable internals
authorLucas Sanders <butterflyhug@google.com>
Tue, 21 Mar 2017 04:22:07 +0000 (00:22 -0400)
committerLucas Sanders <butterflyhug@google.com>
Wed, 22 Mar 2017 11:00:20 +0000 (07:00 -0400)
src/lib/moment/add-subtract.js
src/lib/moment/get-set.js
src/lib/moment/prototype.js
src/lib/units/month.js
src/lib/units/offset.js
src/lib/units/quarter.js
src/lib/units/week-year.js
src/lib/utils/wrap.js [deleted file]
src/test/moment/mutable.js

index 6f67e4168e493e701f7852a6216a49d8ebffb4c1..7b682d53a45c914ab1a2acb89321d0c1288fc74b 100644 (file)
@@ -1,3 +1,4 @@
+import { Moment } from './constructor';
 import { get, set } from './get-set';
 import { setMonth } from '../units/month';
 import { createDuration } from '../duration/create';
@@ -36,13 +37,14 @@ export function addSubtract (mom, duration, isAdding, updateOffset) {
     updateOffset = updateOffset == null ? true : updateOffset;
 
     if (milliseconds) {
+        mom = new Moment(mom);
         mom._d.setTime(mom._d.valueOf() + milliseconds * isAdding);
     }
     if (days) {
-        set(mom, 'Date', get(mom, 'Date') + days * isAdding);
+        mom = set(mom, 'Date', get(mom, 'Date') + days * isAdding);
     }
     if (months) {
-        setMonth(mom, get(mom, 'Month') + months * isAdding);
+        mom = setMonth(mom, get(mom, 'Month') + months * isAdding);
     }
     if (updateOffset) {
         mom = hooks.updateOffset(mom, days || months);
@@ -52,4 +54,3 @@ export function addSubtract (mom, duration, isAdding, updateOffset) {
 
 export var add      = createAdder(1, 'add');
 export var subtract = createAdder(-1, 'subtract');
-
index e23c401c04481625e909ee3212e5124da3d3d865..54f3f49320ee55f5ddec93ed097950fe5d5b0da3 100644 (file)
@@ -1,3 +1,4 @@
+import { Moment } from './constructor';
 import { normalizeUnits, normalizeObjectUnits } from '../units/aliases';
 import { getPrioritizedUnits } from '../units/priorities';
 import { hooks } from '../utils/hooks';
@@ -7,8 +8,8 @@ import isFunction from '../utils/is-function';
 export function makeGetSet (unit, keepTime) {
     return function (value) {
         if (value != null) {
-            set(this, unit, value);
-            return hooks.updateOffset(this, keepTime);
+            var mom = set(this, unit, value);
+            return hooks.updateOffset(mom, keepTime);
         } else {
             return get(this, unit);
         }
@@ -22,8 +23,10 @@ export function get (mom, unit) {
 
 export function set (mom, unit, value) {
     if (mom.isValid()) {
+        mom = new Moment(mom);
         mom._d['set' + (mom._isUTC ? 'UTC' : '') + unit](value);
     }
+    return mom;
 }
 
 // MOMENTS
@@ -38,17 +41,18 @@ export function stringGet (units) {
 
 
 export function stringSet (units, value) {
+    var mom = this;
     if (typeof units === 'object') {
         units = normalizeObjectUnits(units);
         var prioritized = getPrioritizedUnits(units);
         for (var i = 0; i < prioritized.length; i++) {
-            prioritized[i].getSet.call(this, prioritized[i].value);
+            mom = prioritized[i].getSet.call(mom, prioritized[i].value);
         }
     } else {
         units = normalizeUnits(units);
-        if (isFunction(this[units])) {
-            return this[units](value);
+        if (isFunction(mom[units])) {
+            return mom[units](value);
         }
     }
-    return this;
+    return mom;
 }
index 1a494f4077782673632f3025aba8c36a2d4696d7..4cb866bf077957db408eb46516cc9a8a5d9c99a6 100644 (file)
@@ -13,13 +13,12 @@ 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.add            = add;
 proto.calendar       = calendar;
 proto.diff           = diff;
-proto.endOf          = wrap(Moment, endOf);
+proto.endOf          = endOf;
 proto.format         = format;
 proto.from           = from;
 proto.fromNow        = fromNow;
@@ -37,9 +36,9 @@ proto.isValid        = isValid;
 proto.locale         = locale;
 proto.localeData     = localeData;
 proto.parsingFlags   = parsingFlags;
-proto.set            = wrap(Moment, stringSet);
-proto.startOf        = wrap(Moment, startOf);
-proto.subtract       = wrap(Moment, subtract);
+proto.set            = stringSet;
+proto.startOf        = startOf;
+proto.subtract       = subtract;
 proto.toArray        = toArray;
 proto.toObject       = toObject;
 proto.toDate         = toDate;
@@ -53,27 +52,27 @@ proto.creationData   = creationData;
 
 // Year
 import { getSetYear, getIsLeapYear } from '../units/year';
-proto.year       = wrap(Moment, getSetYear, true);
+proto.year       = getSetYear;
 proto.isLeapYear = getIsLeapYear;
 
 // Week Year
 import { getSetWeekYear, getSetISOWeekYear, getWeeksInYear, getISOWeeksInYear } from '../units/week-year';
-proto.weekYear    = wrap(Moment, getSetWeekYear, true);
-proto.isoWeekYear = wrap(Moment, getSetISOWeekYear, true);
+proto.weekYear    = getSetWeekYear;
+proto.isoWeekYear = getSetISOWeekYear;
 
 // Quarter
 import { getSetQuarter } from '../units/quarter';
-proto.quarter = proto.quarters = wrap(Moment, getSetQuarter, true);
+proto.quarter = proto.quarters = getSetQuarter;
 
 // Month
 import { getSetMonth, getDaysInMonth } from '../units/month';
-proto.month       = wrap(Moment, getSetMonth, true);
+proto.month       = getSetMonth;
 proto.daysInMonth = getDaysInMonth;
 
 // Week
 import { getSetWeek, getSetISOWeek } from '../units/week';
-proto.week    = proto.weeks    = wrap(Moment, getSetWeek, true);
-proto.isoWeek = proto.isoWeeks = wrap(Moment, getSetISOWeek, true);
+proto.week    = proto.weeks    = getSetWeek;
+proto.isoWeek = proto.isoWeeks = getSetISOWeek;
 proto.weeksInYear              = getWeeksInYear;
 proto.isoWeeksInYear           = getISOWeeksInYear;
 
@@ -81,27 +80,27 @@ proto.isoWeeksInYear           = getISOWeeksInYear;
 import { getSetDayOfMonth } from '../units/day-of-month';
 import { getSetDayOfWeek, getSetISODayOfWeek, getSetLocaleDayOfWeek } from '../units/day-of-week';
 import { getSetDayOfYear } from '../units/day-of-year';
-proto.date             = wrap(Moment, getSetDayOfMonth, true);
-proto.day = proto.days = wrap(Moment, getSetDayOfWeek, true);
-proto.weekday          = wrap(Moment, getSetLocaleDayOfWeek, true);
-proto.isoWeekday       = wrap(Moment, getSetISODayOfWeek, true);
-proto.dayOfYear        = wrap(Moment, getSetDayOfYear, true);
+proto.date             = getSetDayOfMonth;
+proto.day = proto.days = getSetDayOfWeek;
+proto.weekday          = getSetLocaleDayOfWeek;
+proto.isoWeekday       = getSetISODayOfWeek;
+proto.dayOfYear        = getSetDayOfYear;
 
 // Hour
 import { getSetHour } from '../units/hour';
-proto.hour = proto.hours = wrap(Moment, getSetHour, true);
+proto.hour = proto.hours = getSetHour;
 
 // Minute
 import { getSetMinute } from '../units/minute';
-proto.minute = proto.minutes = wrap(Moment, getSetMinute, true);
+proto.minute = proto.minutes = getSetMinute;
 
 // Second
 import { getSetSecond } from '../units/second';
-proto.second = proto.seconds = wrap(Moment, getSetSecond, true);
+proto.second = proto.seconds = getSetSecond;
 
 // Millisecond
 import { getSetMillisecond } from '../units/millisecond';
-proto.millisecond = proto.milliseconds = wrap(Moment, getSetMillisecond, true);
+proto.millisecond = proto.milliseconds = getSetMillisecond;
 
 // Offset
 import {
@@ -117,10 +116,10 @@ import {
     isUtcOffset,
     isUtc
 } from '../units/offset';
-proto.utcOffset            = wrap(Moment, getSetOffset, true);
-proto.utc                  = wrap(Moment, setOffsetToUTC);
-proto.local                = wrap(Moment, setOffsetToLocal);
-proto.parseZone            = wrap(Moment, setOffsetToParsedOffset);
+proto.utcOffset            = getSetOffset;
+proto.utc                  = setOffsetToUTC;
+proto.local                = setOffsetToLocal;
+proto.parseZone            = setOffsetToParsedOffset;
 proto.hasAlignedHourOffset = hasAlignedHourOffset;
 proto.isDST                = isDaylightSavingTime;
 proto.isLocal              = isLocal;
@@ -142,7 +141,7 @@ proto.clone = deprecate(
     });
 proto.dates = deprecate(
     'dates accessor is deprecated. Use date instead.',
-    wrap(Moment, getSetDayOfMonth, true));
+    getSetDayOfMonth);
 proto.lang = deprecate(
     'moment().lang() is deprecated. Instead, use moment().localeData() to get the language configuration. Use moment().locale() to change languages.',
     lang);
@@ -154,13 +153,13 @@ proto.min = deprecate(
     prototypeMin);
 proto.months = deprecate(
     'months accessor is deprecated. Use month instead',
-    wrap(Moment, getSetMonth, true));
+    getSetMonth);
 proto.years = deprecate(
     'years accessor is deprecated. Use year instead',
-    wrap(Moment, getSetYear, true));
+    getSetYear);
 proto.zone = deprecate(
     'moment().zone is deprecated, use moment().utcOffset instead. http://momentjs.com/guides/#/warnings/zone/',
-    wrap(Moment, getSetZone, true));
+    getSetZone);
 proto.isDSTShifted = deprecate(
     'isDSTShifted is deprecated. See http://momentjs.com/guides/#/warnings/dst-shifted/ for more information',
     isDaylightSavingTimeShifted);
index e0fd9e46d7d78ad033f8e82004e338cdf6839a2d..a08ee1961929d06388b4f148daacaa7938cdcb9d 100644 (file)
@@ -1,3 +1,4 @@
+import { Moment } from '../moment/constructor';
 import { get } from '../moment/get-set';
 import hasOwnProp from '../utils/has-own-prop';
 import { addFormatToken } from '../format/format';
@@ -186,14 +187,15 @@ export function setMonth (mom, value) {
     }
 
     dayOfMonth = Math.min(mom.date(), daysInMonth(mom.year(), value));
+    mom = new Moment(mom);
     mom._d['set' + (mom._isUTC ? 'UTC' : '') + 'Month'](value, dayOfMonth);
     return mom;
 }
 
 export function getSetMonth (value) {
     if (value != null) {
-        setMonth(this, value);
-        return hooks.updateOffset(this, true);
+        var mom = setMonth(this, value);
+        return hooks.updateOffset(mom, true);
     } else {
         return get(this, 'Month');
     }
index 3b6b543df8c2c87e5d6088737ba300f53e552a04..e29f2cae4add61fbd029e640acba3bc1c3c95401 100644 (file)
@@ -108,41 +108,42 @@ hooks.updateOffset = function (m) {
 // _changeInProgress == true case, then we have to adjust, because
 // there is no such time in the given timezone.
 export function getSetOffset (input, keepLocalTime, keepMinutes) {
-    var offset = this._offset || 0,
+    var mom = this,
+        offset = mom._offset || 0,
         localAdjust;
-    if (!this.isValid()) {
-        return input != null ? this : NaN;
+    if (!mom.isValid()) {
+        return input != null ? mom : NaN;
     }
     if (input != null) {
-        var ret = this;
         if (typeof input === 'string') {
             input = offsetFromString(matchShortOffset, input);
             if (input === null) {
-                return this;
+                return mom;
             }
         } else if (Math.abs(input) < 16 && !keepMinutes) {
             input = input * 60;
         }
-        if (!ret._isUTC && keepLocalTime) {
-            localAdjust = getDateOffset(ret);
+        if (!mom._isUTC && keepLocalTime) {
+            localAdjust = getDateOffset(mom);
         }
-        ret._offset = input;
-        ret._isUTC = true;
+        mom = new Moment(mom);
+        mom._offset = input;
+        mom._isUTC = true;
         if (localAdjust != null) {
-            ret = ret.add(localAdjust, 'm');
+            mom = mom.add(localAdjust, 'm');
         }
         if (offset !== input) {
-            if (!keepLocalTime || ret._changeInProgress) {
-                ret = addSubtract(ret, createDuration(input - offset, 'm'), 1, false);
-            } else if (!ret._changeInProgress) {
-                ret._changeInProgress = true;
-                ret = hooks.updateOffset(ret, true);
-                ret._changeInProgress = null;
+            if (!keepLocalTime || mom._changeInProgress) {
+                mom = addSubtract(mom, createDuration(input - offset, 'm'), 1, false);
+            } else if (!mom._changeInProgress) {
+                mom._changeInProgress = true;
+                mom = hooks.updateOffset(mom, true);
+                mom._changeInProgress = null;
             }
         }
-        return ret;
+        return mom;
     } else {
-        return this._isUTC ? offset : getDateOffset(this);
+        return mom._isUTC ? offset : getDateOffset(mom);
     }
 }
 
index c2419c0c66a6f2f437b7b4c25502d3c3c6ba8db9..94654c1a9750e599dec43a13a7fc7e1fabb32a32 100644 (file)
@@ -25,7 +25,10 @@ addParseToken('Q', function (input, array) {
 // MOMENTS
 
 export function getSetQuarter (input) {
-    return input == null ? Math.ceil((this.month() + 1) / 3) : getSetMonth.call(this, (input - 1) * 3 + this.month() % 3);
+    if (input == null) {
+        return Math.ceil((this.month() + 1) / 3);
+    }
+    return getSetMonth.call(this, (input - 1) * 3 + this.month() % 3);
 }
 
 // PRIORITY
index 538f1ca37ec573c85d80d164313490953f05c812..42fad5f9858bff52e666f673c7b39c89deee85eb 100644 (file)
@@ -1,3 +1,4 @@
+import { Moment } from '../moment/constructor';
 import { addFormatToken } from '../format/format';
 import { addUnitAlias } from './aliases';
 import { addUnitPriority } from './priorities';
@@ -59,7 +60,7 @@ addWeekParseToken(['gg', 'GG'], function (input, week, config, token) {
 // MOMENTS
 
 export function getSetWeekYear (input) {
-    return getSetWeekYearHelper.call(this,
+    return getSetWeekYearHelper(this,
             input,
             this.week(),
             this.weekday(),
@@ -68,8 +69,8 @@ export function getSetWeekYear (input) {
 }
 
 export function getSetISOWeekYear (input) {
-    return getSetWeekYearHelper.call(this,
-            input, this.isoWeek(), this.isoWeekday(), 1, 4);
+    return getSetWeekYearHelper(
+        this, input, this.isoWeek(), this.isoWeekday(), 1, 4);
 }
 
 export function getISOWeeksInYear () {
@@ -81,27 +82,27 @@ export function getWeeksInYear () {
     return weeksInYear(this.year(), weekInfo.dow, weekInfo.doy);
 }
 
-function getSetWeekYearHelper(input, week, weekday, dow, doy) {
+function getSetWeekYearHelper(mom, input, week, weekday, dow, doy) {
     var weeksTarget;
     if (input == null) {
-        return weekOfYear(this, dow, doy).year;
+        return weekOfYear(mom, dow, doy).year;
     } else {
         weeksTarget = weeksInYear(input, dow, doy);
         if (week > weeksTarget) {
             week = weeksTarget;
         }
-        return setWeekAll.call(this, input, week, weekday, dow, doy);
+        return setWeekAll(mom, input, week, weekday, dow, doy);
     }
 }
 
-function setWeekAll(weekYear, week, weekday, dow, doy) {
+function setWeekAll(mom, weekYear, week, weekday, dow, doy) {
     var dayOfYearData = dayOfYearFromWeeks(weekYear, week, weekday, dow, doy),
         date = createUTCDate(dayOfYearData.year, 0, dayOfYearData.dayOfYear);
 
-    getSetYear.call(this, date.getUTCFullYear());
-    getSetMonth.call(this, date.getUTCMonth());
-    getSetDayOfMonth.call(this, date.getUTCDate());
-    return this;
+    mom = new Moment(mom);
+    mom._d['set' + (mom._isUTC ? 'UTC' : '') + 'FullYear'](
+        date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate());
+    return mom;
 }
 
 // PRIORITY
diff --git a/src/lib/utils/wrap.js b/src/lib/utils/wrap.js
deleted file mode 100644 (file)
index d9da670..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-export default function wrap(Type, fn, dontCloneWithNoArgs) {
-    return function () {
-        var m;
-        if (dontCloneWithNoArgs && !arguments.length) {
-            m = this;
-        } else {
-            m = new Type(this);
-        }
-        return fn.apply(m, arguments);
-    };
-}
index 0c9e2d793c6023e7f1bd96b3c30e6434e7049531..627e7ce6f19b13b74b8b64cbc0f35e729b8c1cea 100644 (file)
@@ -4,19 +4,24 @@ import moment from '../../moment';
 module('mutable');
 
 test('manipulation methods', function (assert) {
-    var m = moment();
+    var m = moment([2017, 2, 21, 0, 6, 54]);
 
     assert.notEqual(m, moment(m), 'constructor should return a new moment');
     assert.notEqual(m, m.year(2011), 'year() should return a new moment');
     assert.notEqual(m, m.month(1), 'month() should return a new moment');
+    assert.notEqual(m, m.date(9), 'day() should return a new moment');
+    assert.notEqual(m, m.day(3), 'day() should return a new moment');
     assert.notEqual(m, m.hours(7), 'hours() should return a new moment');
     assert.notEqual(m, m.minutes(33), 'minutes() should return a new moment');
     assert.notEqual(m, m.seconds(44), 'seconds() should return a new moment');
     assert.notEqual(m, m.milliseconds(55), 'milliseconds() should return a new moment');
-    assert.notEqual(m, m.day(2), 'day() should return a new moment');
     assert.notEqual(m, m.startOf('week'), 'startOf() should return a new moment');
+    assert.notEqual(m, m.endOf('week'), 'endOf() should return a new moment');
     assert.notEqual(m, m.add(1, 'days'), 'add() should return a new moment');
     assert.notEqual(m, m.subtract(2, 'years'), 'subtract() should return a new moment');
-    assert.notEqual(m, m.local(), 'local() should return a new moment');
     assert.notEqual(m, m.utc(), 'utc() should return a new moment');
+    assert.notEqual(m, m.utcOffset(360), 'utcOffset() should return a new moment');
+
+    var utc = m.utc();
+    assert.notEqual(utc, utc.local(), 'local() should return a new moment');
 });