From 3866df1baec8600e0f861a24acf6bffd443cd75a Mon Sep 17 00:00:00 2001 From: Iskren Chernev Date: Tue, 6 May 2014 10:08:44 -0700 Subject: [PATCH] Extract argument handling logic in pickBy --- moment.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/moment.js b/moment.js index 567366a54..76282eca6 100644 --- a/moment.js +++ b/moment.js @@ -1641,14 +1641,22 @@ 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]; } } @@ -1658,21 +1666,13 @@ 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 -- 2.47.2