From: Lucas Sanders Date: Sun, 30 Oct 2016 18:27:55 +0000 (-0400) Subject: Never call `moment#clone` in our implementation X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=6f334bac923e02e56f0e6b9977a7da706b1db8c8;p=thirdparty%2Fmoment.git Never call `moment#clone` in our implementation --- diff --git a/benchmarks/clone.js b/benchmarks/clone.js index ac516bec2..0d0ee56af 100644 --- a/benchmarks/clone.js +++ b/benchmarks/clone.js @@ -5,6 +5,6 @@ var Benchmark = require('benchmark'), module.exports = { name: 'clone', onComplete: function(){}, - fn: function(){base.clone();}, + fn: function(){moment(base);}, async: true }; diff --git a/src/lib/duration/create.js b/src/lib/duration/create.js index 9b6d5a964..8f435015f 100644 --- a/src/lib/duration/create.js +++ b/src/lib/duration/create.js @@ -94,11 +94,11 @@ function positiveMomentsDifference(base, other) { res.months = other.month() - base.month() + (other.year() - base.year()) * 12; - if (base.clone().add(res.months, 'M').isAfter(other)) { + if (base.add(res.months, 'M').isAfter(other)) { --res.months; } - res.milliseconds = +other - +(base.clone().add(res.months, 'M')); + res.milliseconds = +other - +(base.add(res.months, 'M')); return res; } diff --git a/src/lib/moment/clone.js b/src/lib/moment/clone.js deleted file mode 100644 index d96b328b2..000000000 --- a/src/lib/moment/clone.js +++ /dev/null @@ -1,5 +0,0 @@ -import { Moment } from './constructor'; - -export function clone () { - return new Moment(this); -} diff --git a/src/lib/moment/compare.js b/src/lib/moment/compare.js index b26bac633..799573299 100644 --- a/src/lib/moment/compare.js +++ b/src/lib/moment/compare.js @@ -12,7 +12,7 @@ export function isAfter (input, units) { if (units === 'millisecond') { return this.valueOf() > localInput.valueOf(); } else { - return localInput.valueOf() < this.clone().startOf(units).valueOf(); + return localInput.valueOf() < this.startOf(units).valueOf(); } } @@ -25,7 +25,7 @@ export function isBefore (input, units) { if (units === 'millisecond') { return this.valueOf() < localInput.valueOf(); } else { - return this.clone().endOf(units).valueOf() < localInput.valueOf(); + return this.endOf(units).valueOf() < localInput.valueOf(); } } @@ -46,7 +46,7 @@ export function isSame (input, units) { return this.valueOf() === localInput.valueOf(); } else { inputMs = localInput.valueOf(); - return this.clone().startOf(units).valueOf() <= inputMs && inputMs <= this.clone().endOf(units).valueOf(); + return this.startOf(units).valueOf() <= inputMs && inputMs <= this.endOf(units).valueOf(); } } diff --git a/src/lib/moment/diff.js b/src/lib/moment/diff.js index 9f4390818..de75044cb 100644 --- a/src/lib/moment/diff.js +++ b/src/lib/moment/diff.js @@ -44,15 +44,15 @@ function monthDiff (a, b) { // difference in months var wholeMonthDiff = ((b.year() - a.year()) * 12) + (b.month() - a.month()), // b is in (anchor - 1 month, anchor + 1 month) - anchor = a.clone().add(wholeMonthDiff, 'months'), + anchor = a.add(wholeMonthDiff, 'months'), anchor2, adjust; if (b - anchor < 0) { - anchor2 = a.clone().add(wholeMonthDiff - 1, 'months'); + anchor2 = a.add(wholeMonthDiff - 1, 'months'); // linear across the month adjust = (b - anchor) / (anchor - anchor2); } else { - anchor2 = a.clone().add(wholeMonthDiff + 1, 'months'); + anchor2 = a.add(wholeMonthDiff + 1, 'months'); // linear across the month adjust = (b - anchor) / (anchor2 - anchor); } diff --git a/src/lib/moment/format.js b/src/lib/moment/format.js index d95042f20..40f3792d6 100644 --- a/src/lib/moment/format.js +++ b/src/lib/moment/format.js @@ -6,14 +6,14 @@ hooks.defaultFormat = 'YYYY-MM-DDTHH:mm:ssZ'; hooks.defaultFormatUtc = 'YYYY-MM-DDTHH:mm:ss[Z]'; export function toString () { - return this.clone().locale('en').format('ddd MMM DD YYYY HH:mm:ss [GMT]ZZ'); + return this.locale('en').format('ddd MMM DD YYYY HH:mm:ss [GMT]ZZ'); } export function toISOString() { if (!this.isValid()) { return null; } - var m = this.clone().utc(); + var m = this.utc(); if (m.year() < 0 || m.year() > 9999) { return formatMoment(m, 'YYYYYY-MM-DD[T]HH:mm:ss.SSS[Z]'); } diff --git a/src/lib/moment/prototype.js b/src/lib/moment/prototype.js index dab62143f..3cab9fc86 100644 --- a/src/lib/moment/prototype.js +++ b/src/lib/moment/prototype.js @@ -5,7 +5,6 @@ var proto = Moment.prototype; import { add, subtract } from './add-subtract'; import { calendar, getCalendarFormat } from './calendar'; -import { clone } from './clone'; import { isBefore, isBetween, isSame, isAfter, isSameOrAfter, isSameOrBefore } from './compare'; import { diff } from './diff'; import { format, toString, toISOString, inspect } from './format'; diff --git a/src/lib/units/day-of-year.js b/src/lib/units/day-of-year.js index 1159602e9..f988ca46b 100644 --- a/src/lib/units/day-of-year.js +++ b/src/lib/units/day-of-year.js @@ -28,7 +28,7 @@ addParseToken(['DDD', 'DDDD'], function (input, array, config) { // MOMENTS export function getSetDayOfYear (input) { - var dayOfYear = Math.round((this.clone().startOf('day') - this.clone().startOf('year')) / 864e5) + 1; + var dayOfYear = Math.round((this.startOf('day') - this.startOf('year')) / 864e5) + 1; return input == null ? dayOfYear : this.add((input - dayOfYear), 'd'); } diff --git a/src/lib/units/offset.js b/src/lib/units/offset.js index d7f5bc20e..b24fed207 100644 --- a/src/lib/units/offset.js +++ b/src/lib/units/offset.js @@ -1,7 +1,7 @@ import zeroFill from '../utils/zero-fill'; import { createDuration } from '../duration/create'; import { addSubtract } from '../moment/add-subtract'; -import { isMoment, copyConfig } from '../moment/constructor'; +import { Moment, isMoment, copyConfig } from '../moment/constructor'; import { addFormatToken } from '../format/format'; import { addRegexToken, matchOffset, matchShortOffset } from '../parse/regex'; import { addParseToken } from '../parse/token'; @@ -67,7 +67,7 @@ function offsetFromString(matcher, string) { export function cloneWithOffset(input, model) { var res, diff; if (model._isUTC) { - res = model.clone(); + res = new Moment(model); diff = (isMoment(input) || isDate(input) ? input.valueOf() : createLocal(input).valueOf()) - res.valueOf(); // Use low-level api, because this fn is low-level api. res._d.setTime(res._d.valueOf() + diff); @@ -196,8 +196,8 @@ export function hasAlignedHourOffset (input) { export function isDaylightSavingTime () { return ( - this.utcOffset() > this.clone().month(0).utcOffset() || - this.utcOffset() > this.clone().month(5).utcOffset() + this.utcOffset() > new Moment(this).month(0).utcOffset() || + this.utcOffset() > new Moment(this).month(5).utcOffset() ); }