From: Xotic750 Date: Wed, 18 Sep 2013 00:00:52 +0000 (+0200) Subject: Improve Duration function by normalizing the input object. X-Git-Tag: 2.3.0~24^2 X-Git-Url: http://git.ipfire.org/gitweb/?a=commitdiff_plain;h=refs%2Fpull%2F1116%2Fhead;p=thirdparty%2Fmoment.git Improve Duration function by normalizing the input object. Created a new function (normalizeObjectUnits), this is now also used by dateFromObject Fixed spacing or jshint Fixed missing argument --- diff --git a/moment.js b/moment.js index c6d7222bc..7fc1d278b 100644 --- a/moment.js +++ b/moment.js @@ -249,14 +249,15 @@ // Duration Constructor function Duration(duration) { - var years = duration.years || duration.year || duration.y || 0, - months = duration.months || duration.month || duration.M || 0, - weeks = duration.weeks || duration.week || duration.w || 0, - days = duration.days || duration.day || duration.d || 0, - hours = duration.hours || duration.hour || duration.h || 0, - minutes = duration.minutes || duration.minute || duration.m || 0, - seconds = duration.seconds || duration.second || duration.s || 0, - milliseconds = duration.milliseconds || duration.millisecond || duration.ms || 0; + var normalizedInput = normalizeObjectUnits(duration), + years = normalizedInput.year || 0, + months = normalizedInput.month || 0, + weeks = normalizedInput.week || 0, + days = normalizedInput.day || 0, + hours = normalizedInput.hour || 0, + minutes = normalizedInput.minute || 0, + seconds = normalizedInput.second || 0, + milliseconds = normalizedInput.millisecond || 0; // store reference to input for deterministic cloning this._input = duration; @@ -373,6 +374,24 @@ return units ? unitAliases[units] || units.toLowerCase().replace(/(.)s$/, '$1') : units; } + function normalizeObjectUnits(inputObject) { + var normalizedInput = {}, + normalizedProp, + prop, + index; + + for (prop in inputObject) { + if (inputObject.hasOwnProperty(prop)) { + normalizedProp = normalizeUnits(prop); + if (normalizedProp) { + normalizedInput[normalizedProp] = inputObject[prop]; + } + } + } + + return normalizedInput; + } + function makeList(field) { var count, setter; @@ -911,24 +930,13 @@ } function dateFromObject(config) { - var normalizedInput = {}, - normalizedProp, - prop, - index; + var normalizedInput; if (config._d) { return; } - for (prop in config._i) { - if (config._i.hasOwnProperty(prop)) { - normalizedProp = normalizeUnits(prop); - if (normalizedProp) { - normalizedInput[normalizedProp] = config._i[prop]; - } - } - } - + normalizedInput = normalizeObjectUnits(config._i); config._a = [ normalizedInput.year, normalizedInput.month,