]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Extract argument handling logic in pickBy 1637/head
authorIskren Chernev <iskren.chernev@gmail.com>
Tue, 6 May 2014 17:08:44 +0000 (10:08 -0700)
committerIskren Chernev <iskren.chernev@gmail.com>
Tue, 6 May 2014 17:08:44 +0000 (10:08 -0700)
moment.js

index 567366a544d327c2852e6ad96b5b538e97850bb9..76282eca6f329b2dfc19a2ffb80a2a7836285422 100644 (file)
--- a/moment.js
+++ b/moment.js
         config._d = new Date(config._i);
     });
 
-    function _pickBy(key, moments) {
+    // Pick a moment m from moments so that m[fn](other) is true for all
+    // other. This relies on the function fn to be transitive.
+    //
+    // moments should either be an array of moment objects or an array, whose
+    // first element is an array of moment objects.
+    function pickBy(fn, moments) {
         var res, i;
+        if (moments.length === 1 && isArray(moments[0])) {
+            moments = moments[0];
+        }
         if (!moments.length) {
             return moment();
         }
         res = moments[0];
         for (i = 1; i < moments.length; ++i) {
-            if (moments[i][key](res)) {
+            if (moments[i][fn](res)) {
                 res = moments[i];
             }
         }
     moment.min = function () {
         var args = [].slice.call(arguments, 0);
 
-        if (args.length === 1 && isArray(args[0])) {
-            return _pickBy('isBefore', args[0]);
-        } else {
-            return _pickBy('isBefore', args);
-        }
+        return pickBy('isBefore', args);
     };
 
     moment.max = function () {
         var args = [].slice.call(arguments, 0);
 
-        if (args.length === 1 && isArray(args[0])) {
-            return _pickBy('isAfter', args[0]);
-        } else {
-            return _pickBy('isAfter', args);
-        }
+        return pickBy('isAfter', args);
     };
 
     // creating with utc