From 8a289b8eb3c451b4cce9213864ee46ab37c00aef Mon Sep 17 00:00:00 2001 From: Iskren Chernev Date: Thu, 1 May 2014 10:23:13 -0700 Subject: [PATCH] Implement moment.min and moment.max --- moment.js | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/moment.js b/moment.js index 0c6da7323..acf57da09 100644 --- a/moment.js +++ b/moment.js @@ -1641,6 +1641,40 @@ config._d = new Date(config._i); }); + function _pickBy(key, moments) { + var res, i; + if (!moments.length) { + return moment(); + } + res = moments[0]; + for (i = 1; i < moments.length; ++i) { + if (moments[i][key](res)) { + res = moments[i]; + } + } + return res; + } + + 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); + } + }, + + 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); + } + }, + // creating with utc moment.utc = function (input, format, lang, strict) { var c; @@ -2069,7 +2103,7 @@ } ), - max: deprecated( + max: deprecate( "moment().max is deprecated, use moment.max instead", function (other) { other = moment.apply(null, arguments); -- 2.47.3