From: Lucas Sanders Date: Sun, 30 Oct 2016 18:27:57 +0000 (-0400) Subject: Construct immutable Durations X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ac0caac2172d34677b201a7c1023488e4ce71aaf;p=thirdparty%2Fmoment.git Construct immutable Durations --- diff --git a/src/lib/duration/constructor.js b/src/lib/duration/constructor.js index 578ba34f6..856375655 100644 --- a/src/lib/duration/constructor.js +++ b/src/lib/duration/constructor.js @@ -2,6 +2,7 @@ import { normalizeObjectUnits } from '../units/aliases'; import { getLocale } from '../locale/locales'; import { bubble } from './bubble'; import isDurationValid from './valid'; +import extend from '../utils/extend'; export function Duration (duration) { var normalizedInput = normalizeObjectUnits(duration), @@ -15,6 +16,11 @@ export function Duration (duration) { seconds = normalizedInput.second || 0, milliseconds = normalizedInput.millisecond || 0; + if (isDuration(duration)) { + copyDuration(this, duration); + return; + } + this._isValid = isDurationValid(normalizedInput); // representation for dateAddRemove @@ -26,7 +32,7 @@ export function Duration (duration) { // day when working around DST, we need to store them separately this._days = +days + weeks * 7; - // It is impossible translate months into days without knowing + // It is impossible to translate months into days without knowing // which months you are are talking about, so we have to store // it separately. this._months = +months + @@ -41,3 +47,12 @@ export function Duration (duration) { export function isDuration (obj) { return obj instanceof Duration; } + +function copyDuration(to, from) { + to._isValid = from._isValid; + to._milliseconds = from._milliseconds; + to._days = from._days; + to._months = from._months; + to._data = extend({}, from._data); + to._locale = from._locale; +} diff --git a/src/lib/duration/create.js b/src/lib/duration/create.js index 8f435015f..ef9707221 100644 --- a/src/lib/duration/create.js +++ b/src/lib/duration/create.js @@ -25,11 +25,7 @@ export function createDuration (input, key) { diffRes; if (isDuration(input)) { - duration = { - ms : input._milliseconds, - d : input._days, - M : input._months - }; + duration = input; } else if (isNumber(input)) { duration = {}; if (key) {