+import { Moment } from './constructor';
import { get, set } from './get-set';
import { setMonth } from '../units/month';
import { createDuration } from '../duration/create';
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);
export var add = createAdder(1, 'add');
export var subtract = createAdder(-1, 'subtract');
-
+import { Moment } from './constructor';
import { normalizeUnits, normalizeObjectUnits } from '../units/aliases';
import { getPrioritizedUnits } from '../units/priorities';
import { hooks } from '../utils/hooks';
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);
}
export function set (mom, unit, value) {
if (mom.isValid()) {
+ mom = new Moment(mom);
mom._d['set' + (mom._isUTC ? 'UTC' : '') + unit](value);
}
+ return mom;
}
// MOMENTS
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;
}
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;
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;
// 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;
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 {
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;
});
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);
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);
+import { Moment } from '../moment/constructor';
import { get } from '../moment/get-set';
import hasOwnProp from '../utils/has-own-prop';
import { addFormatToken } from '../format/format';
}
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');
}
// _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);
}
}
// 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
+import { Moment } from '../moment/constructor';
import { addFormatToken } from '../format/format';
import { addUnitAlias } from './aliases';
import { addUnitPriority } from './priorities';
// MOMENTS
export function getSetWeekYear (input) {
- return getSetWeekYearHelper.call(this,
+ return getSetWeekYearHelper(this,
input,
this.week(),
this.weekday(),
}
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 () {
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
+++ /dev/null
-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);
- };
-}
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');
});