From 65352e0da25500701e1256e382e1f4dfa24d31c0 Mon Sep 17 00:00:00 2001 From: Lucas Sanders Date: Sun, 30 Oct 2016 14:27:56 -0400 Subject: [PATCH] Remove _bubble() from Duration prototype and make its API immutable The bubble function now returns a new data object instead of mutating the duration's existing data (which had always been empty when we called _bubble anyway). --- src/lib/duration/bubble.js | 13 ++++++------- src/lib/duration/constructor.js | 7 +++---- src/lib/duration/prototype.js | 1 - 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/lib/duration/bubble.js b/src/lib/duration/bubble.js index 0c4a336ec..f24ec5c5d 100644 --- a/src/lib/duration/bubble.js +++ b/src/lib/duration/bubble.js @@ -2,11 +2,11 @@ import absFloor from '../utils/abs-floor'; import absCeil from '../utils/abs-ceil'; import { createUTCDate } from '../create/date-from-array'; -export function bubble () { - var milliseconds = this._milliseconds; - var days = this._days; - var months = this._months; - var data = this._data; +export function bubble (duration) { + var milliseconds = duration._milliseconds; + var days = duration._days; + var months = duration._months; + var data = {}; var seconds, minutes, hours, years, monthsFromDays; // if we have a mix of positive and negative values, bubble down first @@ -45,8 +45,7 @@ export function bubble () { data.days = days; data.months = months; data.years = years; - - return this; + return data; } export function daysToMonths (days) { diff --git a/src/lib/duration/constructor.js b/src/lib/duration/constructor.js index 5c97ef108..578ba34f6 100644 --- a/src/lib/duration/constructor.js +++ b/src/lib/duration/constructor.js @@ -1,6 +1,7 @@ import { normalizeObjectUnits } from '../units/aliases'; import { getLocale } from '../locale/locales'; -import isDurationValid from './valid.js'; +import { bubble } from './bubble'; +import isDurationValid from './valid'; export function Duration (duration) { var normalizedInput = normalizeObjectUnits(duration), @@ -32,11 +33,9 @@ export function Duration (duration) { quarters * 3 + years * 12; - this._data = {}; + this._data = bubble(this); this._locale = getLocale(); - - this._bubble(); } export function isDuration (obj) { diff --git a/src/lib/duration/prototype.js b/src/lib/duration/prototype.js index 3fd516a56..7eaca4461 100644 --- a/src/lib/duration/prototype.js +++ b/src/lib/duration/prototype.js @@ -27,7 +27,6 @@ proto.asWeeks = asWeeks; proto.asMonths = asMonths; proto.asYears = asYears; proto.valueOf = valueOf; -proto._bubble = bubble; proto.get = get; proto.milliseconds = milliseconds; proto.seconds = seconds; -- 2.47.2