From: Lucas Sanders Date: Tue, 21 Mar 2017 04:22:07 +0000 (-0400) Subject: Finish removing wrap() in favor of immutable internals X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cb7dcf06ea76c32f3137511de311fa6af10a812d;p=thirdparty%2Fmoment.git Finish removing wrap() in favor of immutable internals --- diff --git a/src/lib/moment/add-subtract.js b/src/lib/moment/add-subtract.js index 6f67e4168..7b682d53a 100644 --- a/src/lib/moment/add-subtract.js +++ b/src/lib/moment/add-subtract.js @@ -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'); - diff --git a/src/lib/moment/get-set.js b/src/lib/moment/get-set.js index e23c401c0..54f3f4932 100644 --- a/src/lib/moment/get-set.js +++ b/src/lib/moment/get-set.js @@ -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; } diff --git a/src/lib/moment/prototype.js b/src/lib/moment/prototype.js index 1a494f407..4cb866bf0 100644 --- a/src/lib/moment/prototype.js +++ b/src/lib/moment/prototype.js @@ -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); diff --git a/src/lib/units/month.js b/src/lib/units/month.js index e0fd9e46d..a08ee1961 100644 --- a/src/lib/units/month.js +++ b/src/lib/units/month.js @@ -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'); } diff --git a/src/lib/units/offset.js b/src/lib/units/offset.js index 3b6b543df..e29f2cae4 100644 --- a/src/lib/units/offset.js +++ b/src/lib/units/offset.js @@ -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); } } diff --git a/src/lib/units/quarter.js b/src/lib/units/quarter.js index c2419c0c6..94654c1a9 100644 --- a/src/lib/units/quarter.js +++ b/src/lib/units/quarter.js @@ -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 diff --git a/src/lib/units/week-year.js b/src/lib/units/week-year.js index 538f1ca37..42fad5f98 100644 --- a/src/lib/units/week-year.js +++ b/src/lib/units/week-year.js @@ -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 index d9da670ba..000000000 --- a/src/lib/utils/wrap.js +++ /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); - }; -} diff --git a/src/test/moment/mutable.js b/src/test/moment/mutable.js index 0c9e2d793..627e7ce6f 100644 --- a/src/test/moment/mutable.js +++ b/src/test/moment/mutable.js @@ -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'); });