]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Improve Duration function by normalizing the input object. 1116/head
authorXotic750 <xotic750@gmail.com>
Wed, 18 Sep 2013 00:00:52 +0000 (02:00 +0200)
committerXotic750 <xotic750@gmail.com>
Wed, 18 Sep 2013 00:24:16 +0000 (02:24 +0200)
Created a new function (normalizeObjectUnits), this is now also used by dateFromObject

Fixed spacing or jshint

Fixed missing argument

moment.js

index c6d7222bc261927162e85e2993960702397c9e1f..7fc1d278b4e51a28b83f25f752a8f70dd8af92de 100644 (file)
--- a/moment.js
+++ b/moment.js
 
     // 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;
         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;
 
     }
 
     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,