From: Lucas Sanders Date: Sun, 30 Oct 2016 18:27:55 +0000 (-0400) Subject: Fix startOf/endOf/utcOffset and getters/setters for immutable API X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=3c9865292d6579beeb2601e8c6326c056ddd0862;p=thirdparty%2Fmoment.git Fix startOf/endOf/utcOffset and getters/setters for immutable API --- diff --git a/src/lib/create/from-anything.js b/src/lib/create/from-anything.js index e692679bc..72aad72bf 100644 --- a/src/lib/create/from-anything.js +++ b/src/lib/create/from-anything.js @@ -22,7 +22,7 @@ function createFromConfig (config) { var res = new Moment(checkOverflow(prepareConfig(config))); if (res._nextDay) { // Adding is smart enough around DST - res.add(1, 'd'); + res = res.add(1, 'd'); res._nextDay = undefined; } diff --git a/src/lib/moment/get-set.js b/src/lib/moment/get-set.js index 0d9fe5dfe..abdb8f4c0 100644 --- a/src/lib/moment/get-set.js +++ b/src/lib/moment/get-set.js @@ -43,7 +43,7 @@ export function stringSet (units, value) { units = normalizeObjectUnits(units); var prioritized = getPrioritizedUnits(units); for (var i = 0; i < prioritized.length; i++) { - this[prioritized[i].unit](units[prioritized[i].unit]); + prioritized[i].getSet.call(this, prioritized[i].value); } } else { units = normalizeUnits(units); diff --git a/src/lib/moment/start-end-of.js b/src/lib/moment/start-end-of.js index 02f982479..1a9908fdc 100644 --- a/src/lib/moment/start-end-of.js +++ b/src/lib/moment/start-end-of.js @@ -2,46 +2,48 @@ import { normalizeUnits } from '../units/aliases'; export function startOf (units) { units = normalizeUnits(units); + + var m = this; // the following switch intentionally omits break keywords // to utilize falling through the cases. switch (units) { case 'year': - this.month(0); + m = m.month(0); /* falls through */ case 'quarter': case 'month': - this.date(1); + m = m.date(1); /* falls through */ case 'week': case 'isoWeek': case 'day': case 'date': - this.hours(0); + m = m.hours(0); /* falls through */ case 'hour': - this.minutes(0); + m = m.minutes(0); /* falls through */ case 'minute': - this.seconds(0); + m = m.seconds(0); /* falls through */ case 'second': - this.milliseconds(0); + m = m.milliseconds(0); } // weeks are a special case if (units === 'week') { - this.weekday(0); + m = m.weekday(0); } if (units === 'isoWeek') { - this.isoWeekday(1); + m = m.isoWeekday(1); } // quarters are also special if (units === 'quarter') { - this.month(Math.floor(this.month() / 3) * 3); + m = m.month(Math.floor(m.month() / 3) * 3); } - return this; + return m; } export function endOf (units) { diff --git a/src/lib/units/day-of-month.js b/src/lib/units/day-of-month.js index 02532534e..4626977de 100644 --- a/src/lib/units/day-of-month.js +++ b/src/lib/units/day-of-month.js @@ -15,9 +15,6 @@ addFormatToken('D', ['DD', 2], 'Do', 'date'); addUnitAlias('date', 'D'); -// PRIOROITY -addUnitPriority('date', 9); - // PARSING addRegexToken('D', match1to2); @@ -37,3 +34,7 @@ addParseToken('Do', function (input, array) { // MOMENTS export var getSetDayOfMonth = makeGetSet('Date', true); + +// PRIORITY + +addUnitPriority('date', 9, getSetDayOfMonth); diff --git a/src/lib/units/day-of-week.js b/src/lib/units/day-of-week.js index 55027608d..81ef1bc4d 100644 --- a/src/lib/units/day-of-week.js +++ b/src/lib/units/day-of-week.js @@ -35,11 +35,6 @@ addUnitAlias('day', 'd'); addUnitAlias('weekday', 'e'); addUnitAlias('isoWeekday', 'E'); -// PRIORITY -addUnitPriority('day', 11); -addUnitPriority('weekday', 11); -addUnitPriority('isoWeekday', 11); - // PARSING addRegexToken('d', match1to2); @@ -362,3 +357,9 @@ function computeWeekdaysParse () { this._weekdaysShortStrictRegex = new RegExp('^(' + shortPieces.join('|') + ')', 'i'); this._weekdaysMinStrictRegex = new RegExp('^(' + minPieces.join('|') + ')', 'i'); } + +// PRIORITY + +addUnitPriority('day', 11, getSetDayOfWeek); +addUnitPriority('weekday', 11, getSetLocaleDayOfWeek); +addUnitPriority('isoWeekday', 11, getSetISODayOfWeek); diff --git a/src/lib/units/day-of-year.js b/src/lib/units/day-of-year.js index 6fe931c1e..1159602e9 100644 --- a/src/lib/units/day-of-year.js +++ b/src/lib/units/day-of-year.js @@ -15,9 +15,6 @@ addFormatToken('DDD', ['DDDD', 3], 'DDDo', 'dayOfYear'); addUnitAlias('dayOfYear', 'DDD'); -// PRIORITY -addUnitPriority('dayOfYear', 4); - // PARSING addRegexToken('DDD', match1to3); @@ -34,3 +31,7 @@ export function getSetDayOfYear (input) { var dayOfYear = Math.round((this.clone().startOf('day') - this.clone().startOf('year')) / 864e5) + 1; return input == null ? dayOfYear : this.add((input - dayOfYear), 'd'); } + +// PRIORITY + +addUnitPriority('dayOfYear', 4, getSetDayOfYear); diff --git a/src/lib/units/hour.js b/src/lib/units/hour.js index ef7586803..d4aed9c22 100644 --- a/src/lib/units/hour.js +++ b/src/lib/units/hour.js @@ -54,9 +54,6 @@ meridiem('A', false); addUnitAlias('hour', 'h'); -// PRIORITY -addUnitPriority('hour', 13); - // PARSING function matchMeridiem (isStrict, locale) { @@ -142,3 +139,7 @@ export function localeMeridiem (hours, minutes, isLower) { // a new timezone) makes sense. Adding/subtracting hours does not follow // this rule. export var getSetHour = makeGetSet('Hours', true); + +// PRIORITY + +addUnitPriority('hour', 13, getSetHour); diff --git a/src/lib/units/millisecond.js b/src/lib/units/millisecond.js index 27c951269..d37e1ca14 100644 --- a/src/lib/units/millisecond.js +++ b/src/lib/units/millisecond.js @@ -37,15 +37,10 @@ addFormatToken(0, ['SSSSSSSSS', 9], 0, function () { return this.millisecond() * 1000000; }); - // ALIASES addUnitAlias('millisecond', 'ms'); -// PRIORITY - -addUnitPriority('millisecond', 16); - // PARSING addRegexToken('S', match1to3, match1); @@ -67,3 +62,7 @@ for (token = 'S'; token.length <= 9; token += 'S') { // MOMENTS export var getSetMillisecond = makeGetSet('Milliseconds', false); + +// PRIORITY + +addUnitPriority('millisecond', 16, getSetMillisecond); diff --git a/src/lib/units/minute.js b/src/lib/units/minute.js index 9f760322e..08b0b6bc4 100644 --- a/src/lib/units/minute.js +++ b/src/lib/units/minute.js @@ -14,10 +14,6 @@ addFormatToken('m', ['mm', 2], 0, 'minute'); addUnitAlias('minute', 'm'); -// PRIORITY - -addUnitPriority('minute', 14); - // PARSING addRegexToken('m', match1to2); @@ -27,3 +23,7 @@ addParseToken(['m', 'mm'], MINUTE); // MOMENTS export var getSetMinute = makeGetSet('Minutes', false); + +// PRIORITY + +addUnitPriority('minute', 14, getSetMinute); diff --git a/src/lib/units/month.js b/src/lib/units/month.js index 28b4a1b98..a09ffca79 100644 --- a/src/lib/units/month.js +++ b/src/lib/units/month.js @@ -36,10 +36,6 @@ addFormatToken('MMMM', 0, 0, function (format) { addUnitAlias('month', 'M'); -// PRIORITY - -addUnitPriority('month', 8); - // PARSING addRegexToken('M', match1to2); @@ -281,3 +277,7 @@ function computeMonthsParse () { this._monthsStrictRegex = new RegExp('^(' + longPieces.join('|') + ')', 'i'); this._monthsShortStrictRegex = new RegExp('^(' + shortPieces.join('|') + ')', 'i'); } + +// PRIORITY + +addUnitPriority('month', 8, getSetMonth); diff --git a/src/lib/units/offset.js b/src/lib/units/offset.js index 752358f0d..d7f5bc20e 100644 --- a/src/lib/units/offset.js +++ b/src/lib/units/offset.js @@ -146,9 +146,7 @@ export function getSetZone (input, keepLocalTime) { input = -input; } - this.utcOffset(input, keepLocalTime); - - return this; + return this.utcOffset(input, keepLocalTime); } else { return -this.utcOffset(); } @@ -159,28 +157,30 @@ export function setOffsetToUTC (keepLocalTime) { } export function setOffsetToLocal (keepLocalTime) { - if (this._isUTC) { - this.utcOffset(0, keepLocalTime); - this._isUTC = false; + var ret = this; + if (ret._isUTC) { + ret = ret.utcOffset(0, keepLocalTime); + ret._isUTC = false; if (keepLocalTime) { - this.subtract(getDateOffset(this), 'm'); + ret = ret.subtract(getDateOffset(ret), 'm'); } } - return this; + return ret; } export function setOffsetToParsedOffset () { if (this._tzm != null) { - this.utcOffset(this._tzm, false, true); + return this.utcOffset(this._tzm, false, true); } else if (typeof this._i === 'string') { var tZone = offsetFromString(matchOffset, this._i); if (tZone != null) { - this.utcOffset(tZone); + return this.utcOffset(tZone); } else { - this.utcOffset(0, true); + return this.utcOffset(0, true); } + return this.utcOffset(offsetFromString(matchOffset, this._i)); } return this; } diff --git a/src/lib/units/priorities.js b/src/lib/units/priorities.js index 699017cb8..05c9dd982 100644 --- a/src/lib/units/priorities.js +++ b/src/lib/units/priorities.js @@ -1,13 +1,18 @@ var priorities = {}; -export function addUnitPriority(unit, priority) { - priorities[unit] = priority; +export function addUnitPriority(unit, priority, getterSetter) { + priorities[unit] = {priority: priority, getSet: getterSetter}; } export function getPrioritizedUnits(unitsObj) { var units = []; for (var u in unitsObj) { - units.push({unit: u, priority: priorities[u]}); + units.push({ + unit: u, + value: unitsObj[u], + priority: priorities[u].priority, + getSet: priorities[u].getSet + }); } units.sort(function (a, b) { return a.priority - b.priority; diff --git a/src/lib/units/quarter.js b/src/lib/units/quarter.js index a6d409a86..c2419c0c6 100644 --- a/src/lib/units/quarter.js +++ b/src/lib/units/quarter.js @@ -3,6 +3,7 @@ import { addUnitAlias } from './aliases'; import { addUnitPriority } from './priorities'; import { addRegexToken, match1 } from '../parse/regex'; import { addParseToken } from '../parse/token'; +import { getSetMonth } from './month'; import { MONTH } from './constants'; import toInt from '../utils/to-int'; @@ -14,10 +15,6 @@ addFormatToken('Q', 0, 'Qo', 'quarter'); addUnitAlias('quarter', 'Q'); -// PRIORITY - -addUnitPriority('quarter', 7); - // PARSING addRegexToken('Q', match1); @@ -28,5 +25,9 @@ addParseToken('Q', function (input, array) { // MOMENTS export function getSetQuarter (input) { - return input == null ? Math.ceil((this.month() + 1) / 3) : this.month((input - 1) * 3 + this.month() % 3); + return input == null ? Math.ceil((this.month() + 1) / 3) : getSetMonth.call(this, (input - 1) * 3 + this.month() % 3); } + +// PRIORITY + +addUnitPriority('quarter', 7, getSetQuarter); diff --git a/src/lib/units/second.js b/src/lib/units/second.js index 179371186..8f6952f1e 100644 --- a/src/lib/units/second.js +++ b/src/lib/units/second.js @@ -14,10 +14,6 @@ addFormatToken('s', ['ss', 2], 0, 'second'); addUnitAlias('second', 's'); -// PRIORITY - -addUnitPriority('second', 15); - // PARSING addRegexToken('s', match1to2); @@ -27,3 +23,7 @@ addParseToken(['s', 'ss'], SECOND); // MOMENTS export var getSetSecond = makeGetSet('Seconds', false); + +// PRIORITY + +addUnitPriority('second', 15, getSetSecond); diff --git a/src/lib/units/week-year.js b/src/lib/units/week-year.js index 7fa5425bc..538f1ca37 100644 --- a/src/lib/units/week-year.js +++ b/src/lib/units/week-year.js @@ -8,6 +8,9 @@ import toInt from '../utils/to-int'; import { hooks } from '../utils/hooks'; import { createLocal } from '../create/local'; import { createUTCDate } from '../create/date-from-array'; +import { getSetDayOfMonth } from './day-of-month'; +import { getSetMonth } from './month'; +import { getSetYear } from './year'; // FORMATTING @@ -33,11 +36,6 @@ addWeekYearFormatToken('GGGGG', 'isoWeekYear'); addUnitAlias('weekYear', 'gg'); addUnitAlias('isoWeekYear', 'GG'); -// PRIORITY - -addUnitPriority('weekYear', 1); -addUnitPriority('isoWeekYear', 1); - // PARSING @@ -100,8 +98,13 @@ function setWeekAll(weekYear, week, weekday, dow, doy) { var dayOfYearData = dayOfYearFromWeeks(weekYear, week, weekday, dow, doy), date = createUTCDate(dayOfYearData.year, 0, dayOfYearData.dayOfYear); - this.year(date.getUTCFullYear()); - this.month(date.getUTCMonth()); - this.date(date.getUTCDate()); + getSetYear.call(this, date.getUTCFullYear()); + getSetMonth.call(this, date.getUTCMonth()); + getSetDayOfMonth.call(this, date.getUTCDate()); return this; } + +// PRIORITY + +addUnitPriority('weekYear', 1, getSetWeekYear); +addUnitPriority('isoWeekYear', 1, getSetISOWeekYear); diff --git a/src/lib/units/week.js b/src/lib/units/week.js index da64ffef3..e63b3e109 100644 --- a/src/lib/units/week.js +++ b/src/lib/units/week.js @@ -17,11 +17,6 @@ addFormatToken('W', ['WW', 2], 'Wo', 'isoWeek'); addUnitAlias('week', 'w'); addUnitAlias('isoWeek', 'W'); -// PRIORITIES - -addUnitPriority('week', 5); -addUnitPriority('isoWeek', 5); - // PARSING addRegexToken('w', match1to2); @@ -65,3 +60,8 @@ export function getSetISOWeek (input) { var week = weekOfYear(this, 1, 4).week; return input == null ? week : this.add((input - week) * 7, 'd'); } + +// PRIORITIES + +addUnitPriority('week', 5, getSetWeek); +addUnitPriority('isoWeek', 5, getSetISOWeek); diff --git a/src/lib/units/year.js b/src/lib/units/year.js index a10e5b4be..47353a044 100644 --- a/src/lib/units/year.js +++ b/src/lib/units/year.js @@ -27,10 +27,6 @@ addFormatToken(0, ['YYYYYY', 6, true], 0, 'year'); addUnitAlias('year', 'y'); -// PRIORITIES - -addUnitPriority('year', 1); - // PARSING addRegexToken('Y', matchSigned); @@ -73,3 +69,7 @@ export var getSetYear = makeGetSet('FullYear', true); export function getIsLeapYear () { return isLeapYear(this.year()); } + +// PRIORITIES + +addUnitPriority('year', 1, getSetYear);