]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Never call `moment#clone` in our implementation
authorLucas Sanders <butterflyhug@google.com>
Sun, 30 Oct 2016 18:27:55 +0000 (14:27 -0400)
committerLucas Sanders <butterflyhug@google.com>
Wed, 22 Mar 2017 10:12:27 +0000 (06:12 -0400)
benchmarks/clone.js
src/lib/duration/create.js
src/lib/moment/clone.js [deleted file]
src/lib/moment/compare.js
src/lib/moment/diff.js
src/lib/moment/format.js
src/lib/moment/prototype.js
src/lib/units/day-of-year.js
src/lib/units/offset.js

index ac516bec2d5285e9c01bbe0f03e325d4d69e99f0..0d0ee56af82446343105fbd26353d8444f98c41c 100644 (file)
@@ -5,6 +5,6 @@ var Benchmark = require('benchmark'),
 module.exports = {
   name: 'clone',
   onComplete: function(){},
-  fn: function(){base.clone();},
+  fn: function(){moment(base);},
   async: true
 };
index 9b6d5a964fcdf3bf5303522b11ff9518502735eb..8f435015f41893b7bb549315d00ddd17cea69f90 100644 (file)
@@ -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 (file)
index d96b328..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-import { Moment } from './constructor';
-
-export function clone () {
-    return new Moment(this);
-}
index b26bac633c4c697aa9e556603fbdac4c00be6e36..799573299649dc42ae09d93b919ed2177930c876 100644 (file)
@@ -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();
     }
 }
 
index 9f4390818980b1d4fb00d67905fc02fe1f6df809..de75044cb7462574cc11e08d7fcceaba1ba71f6e 100644 (file)
@@ -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);
     }
index d95042f20bf5bf634b1aedb8a1b4b149ccb7c7ac..40f3792d635db72025e075c8da218e7fe9d4b4fb 100644 (file)
@@ -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]');
     }
index dab62143f687bda762e04de4aa651103c64da238..3cab9fc86ba1824541def6f85050419f15418812 100644 (file)
@@ -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';
index 1159602e93acd805297a970d69c00c1b8491f419..f988ca46b04346fcc4efd6657797124b24e9809e 100644 (file)
@@ -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');
 }
 
index d7f5bc20e66fda03afb2b37bf2f6ab836352c6bf..b24fed2070a52d24f2df835caf46f9e1d6ef8fd7 100644 (file)
@@ -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()
     );
 }