From: Tim Wood Date: Thu, 23 May 2013 19:17:24 +0000 (-0700) Subject: Adding moment#min and moment#max #755 X-Git-Tag: 2.1.0~27^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=33f1011ebb93743c2148ce4812a67a8498c6a78a;p=thirdparty%2Fmoment.git Adding moment#min and moment#max #755 --- diff --git a/moment.js b/moment.js index fd67ba564..1adf58d21 100644 --- a/moment.js +++ b/moment.js @@ -1360,6 +1360,24 @@ return +this.clone().startOf(units) === +moment(input).startOf(units); }, + min: function (other) { + if (typeof other === "string" && other.toLowerCase() === "now") { + other = moment(); + } else { + other = moment.apply(null, arguments); + } + return other < this ? this : other; + }, + + max: function (other) { + if (typeof other === "string" && other.toLowerCase() === "now") { + other = moment(); + } else { + other = moment.apply(null, arguments); + } + return other > this ? this : other; + }, + zone : function (input) { var offset = this._offset || 0; if (input != null) { diff --git a/test/moment/min_max.js b/test/moment/min_max.js new file mode 100644 index 000000000..9ede27ce5 --- /dev/null +++ b/test/moment/min_max.js @@ -0,0 +1,66 @@ +var moment = require("../../moment"); + +exports.min_max = { + setUp : function (cb) { + moment.lang('en'); + cb(); + }, + + tearDown : function (cb) { + moment.lang('en'); + cb(); + }, + + "min" : function (test) { + test.expect(10); + + var now = moment(), + future = now.clone().add(1, 'month'), + past = now.clone().subtract(1, 'month'); + + // we use Math.abs(a.diff(b)) < 2 to prevent issues where + // two moments are off by a millisecond. + + test.ok(Math.abs(past.min(now).diff(now)) < 2, "A past date with the minimum of now should be now"); + test.ok(Math.abs(past.min('now').diff(now)) < 2, "A past date with the minimum of 'now' should be now"); + test.ok(Math.abs(past.min().diff(now)) < 2, "A past date with the minimum of implied now should be now"); + test.ok(Math.abs(past.min(future).diff(future)) < 2, "A past date with the minimum of the future should be the future date"); + + test.ok(Math.abs(future.min(now).diff(future)) < 2, "A future date with the minimum of now should be the future"); + test.ok(Math.abs(future.min('now').diff(future)) < 2, "A future date with the minimum of 'now' should be the future"); + test.ok(Math.abs(future.min().diff(future)) < 2, "A future date with the minimum of implied now should be the future"); + test.ok(Math.abs(future.min(past).diff(future)) < 2, "A future date with the minimum of the past should be the future"); + + test.ok(Math.abs(now.min(past).diff(now)) < 2, "Now with the minimum of the past should be now"); + test.ok(Math.abs(now.min(future).diff(future)) < 2, "Now with the minimum of the future should be the future"); + + test.done(); + }, + + "max" : function (test) { + test.expect(10); + + var now = moment(), + future = now.clone().add(1, 'month'), + past = now.clone().subtract(1, 'month'); + + // we use Math.abs(a.diff(b)) < 2 to prevent issues where + // two moments are off by a millisecond. + + test.ok(Math.abs(past.max(now).diff(past)) < 2, "A past date with the maximum of now should be the past"); + test.ok(Math.abs(past.max('now').diff(past)) < 2, "A past date with the maximum of 'now' should be the past"); + test.ok(Math.abs(past.max().diff(past)) < 2, "A past date with the maximum of implied now should be the past"); + test.ok(Math.abs(past.max(future).diff(past)) < 2, "A past date with the maximum of the future should be the past"); + + test.ok(Math.abs(future.max(now).diff(now)) < 2, "A future date with the maximum of now should be now"); + test.ok(Math.abs(future.max('now').diff(now)) < 2, "A future date with the maximum of 'now' should be now"); + test.ok(Math.abs(future.max().diff(now)) < 2, "A future date with the maximum of implied now should be now"); + test.ok(Math.abs(future.max(past).diff(past)) < 2, "A future date with the maximum of the past should be the past"); + + test.ok(Math.abs(now.max(past).diff(past)) < 2, "Now with the maximum of the past should be the past"); + test.ok(Math.abs(now.max(future).diff(now)) < 2, "Now with the maximum of the future should be now"); + + test.done(); + } + +};