From: quietmint Date: Sat, 23 Mar 2013 00:37:24 +0000 (-0400) Subject: startOf and endOf accept abbreviated units X-Git-Tag: 2.1.0~52^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=11371b333f487a7bb8c59b76dd571044ca3dab79;p=thirdparty%2Fmoment.git startOf and endOf accept abbreviated units --- diff --git a/moment.js b/moment.js index 4bf5e8369..aaeaa482b 100644 --- a/moment.js +++ b/moment.js @@ -1190,33 +1190,40 @@ }, startOf: function (units) { - units = units.replace(/s$/, ''); + units = units.replace(/(.)s$/, "$1"); // the following switch intentionally omits break keywords // to utilize falling through the cases. switch (units) { case 'year': + case 'y': this.month(0); /* falls through */ case 'month': + case 'M': this.date(1); /* falls through */ case 'week': + case 'w': case 'day': + case 'd': this.hours(0); /* falls through */ case 'hour': + case 'h': this.minutes(0); /* falls through */ case 'minute': + case 'm': this.seconds(0); /* falls through */ case 'second': + case 's': this.milliseconds(0); /* falls through */ } // weeks are a special case - if (units === 'week') { + if (units === 'week' || units === 'w') { this.day(0); } @@ -1224,7 +1231,7 @@ }, endOf: function (units) { - return this.startOf(units).add(units.replace(/s?$/, 's'), 1).subtract('ms', 1); + return this.startOf(units).add(units.replace(/(.)s$/, "$1"), 1).subtract('ms', 1); }, isAfter: function (input, units) { diff --git a/test/moment/sod_eod.js b/test/moment/sod_eod.js index 8ff70d00d..21ddc442e 100644 --- a/test/moment/sod_eod.js +++ b/test/moment/sod_eod.js @@ -2,11 +2,13 @@ var moment = require("../../moment"); exports.end_start_of = { "start of year" : function(test) { - test.expect(8); + test.expect(9); var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('year'); var ms = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('years'); + var ma = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('y'); test.equal(+m, +ms, "Plural or singular should work"); + test.equal(+m, +ma, "Full or abbreviated should work"); test.equal(m.year(), 2011, "keep the year"); test.equal(m.month(), 0, "strip out the month"); test.equal(m.date(), 1, "strip out the day"); @@ -18,11 +20,13 @@ exports.end_start_of = { }, "end of year" : function(test) { - test.expect(8); + test.expect(9); var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('year'); var ms = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('years'); + var ma = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('y'); test.equal(+m, +ms, "Plural or singular should work"); + test.equal(+m, +ma, "Full or abbreviated should work"); test.equal(m.year(), 2011, "keep the year"); test.equal(m.month(), 11, "set the month"); test.equal(m.date(), 31, "set the day"); @@ -34,11 +38,13 @@ exports.end_start_of = { }, "start of month" : function(test) { - test.expect(8); + test.expect(9); var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('month'); var ms = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('months'); + var ma = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('M'); test.equal(+m, +ms, "Plural or singular should work"); + test.equal(+m, +ma, "Full or abbreviated should work"); test.equal(m.year(), 2011, "keep the year"); test.equal(m.month(), 1, "keep the month"); test.equal(m.date(), 1, "strip out the day"); @@ -50,11 +56,13 @@ exports.end_start_of = { }, "end of month" : function(test) { - test.expect(8); + test.expect(9); var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('month'); var ms = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('months'); + var ma = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('M'); test.equal(+m, +ms, "Plural or singular should work"); + test.equal(+m, +ma, "Full or abbreviated should work"); test.equal(m.year(), 2011, "keep the year"); test.equal(m.month(), 1, "keep the month"); test.equal(m.date(), 28, "set the day"); @@ -66,11 +74,13 @@ exports.end_start_of = { }, "start of week" : function(test) { - test.expect(9); + test.expect(10); var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('week'); var ms = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('weeks'); + var ma = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('w'); test.equal(+m, +ms, "Plural or singular should work"); + test.equal(+m, +ma, "Full or abbreviated should work"); test.equal(m.year(), 2011, "keep the year"); test.equal(m.month(), 0, "rolls back to January"); test.equal(m.day(), 0, "set day of week"); @@ -83,11 +93,13 @@ exports.end_start_of = { }, "end of week" : function(test) { - test.expect(9); + test.expect(10); var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('week'); var ms = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('weeks'); + var ma = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('weeks'); test.equal(+m, +ms, "Plural or singular should work"); + test.equal(+m, +ma, "Full or abbreviated should work"); test.equal(m.year(), 2011, "keep the year"); test.equal(m.month(), 1, "keep the month"); test.equal(m.day(), 6, "set the day of the week"); @@ -100,11 +112,13 @@ exports.end_start_of = { }, "start of day" : function(test) { - test.expect(8); + test.expect(9); var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('day'); var ms = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('days'); + var ma = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('d'); test.equal(+m, +ms, "Plural or singular should work"); + test.equal(+m, +ma, "Full or abbreviated should work"); test.equal(m.year(), 2011, "keep the year"); test.equal(m.month(), 1, "keep the month"); test.equal(m.date(), 2, "keep the day"); @@ -116,11 +130,13 @@ exports.end_start_of = { }, "end of day" : function(test) { - test.expect(8); + test.expect(9); var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('day'); var ms = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('days'); + var ma = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('d'); test.equal(+m, +ms, "Plural or singular should work"); + test.equal(+m, +ma, "Full or abbreviated should work"); test.equal(m.year(), 2011, "keep the year"); test.equal(m.month(), 1, "keep the month"); test.equal(m.date(), 2, "keep the day"); @@ -132,11 +148,13 @@ exports.end_start_of = { }, "start of hour" : function(test) { - test.expect(8); + test.expect(9); var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('hour'); var ms = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('hours'); + var ma = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('h'); test.equal(+m, +ms, "Plural or singular should work"); + test.equal(+m, +ma, "Full or abbreviated should work"); test.equal(m.year(), 2011, "keep the year"); test.equal(m.month(), 1, "keep the month"); test.equal(m.date(), 2, "keep the day"); @@ -148,11 +166,13 @@ exports.end_start_of = { }, "end of hour" : function(test) { - test.expect(8); + test.expect(9); var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('hour'); var ms = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('hours'); + var ma = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('h'); test.equal(+m, +ms, "Plural or singular should work"); + test.equal(+m, +ma, "Full or abbreviated should work"); test.equal(m.year(), 2011, "keep the year"); test.equal(m.month(), 1, "keep the month"); test.equal(m.date(), 2, "keep the day"); @@ -164,11 +184,13 @@ exports.end_start_of = { }, "start of minute" : function(test) { - test.expect(8); + test.expect(9); var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('minute'); var ms = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('minutes'); + var ma = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('m'); test.equal(+m, +ms, "Plural or singular should work"); + test.equal(+m, +ma, "Full or abbreviated should work"); test.equal(m.year(), 2011, "keep the year"); test.equal(m.month(), 1, "keep the month"); test.equal(m.date(), 2, "keep the day"); @@ -180,11 +202,13 @@ exports.end_start_of = { }, "end of minute" : function(test) { - test.expect(8); + test.expect(9); var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('minute'); var ms = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('minutes'); + var ma = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('m'); test.equal(+m, +ms, "Plural or singular should work"); + test.equal(+m, +ma, "Full or abbreviated should work"); test.equal(m.year(), 2011, "keep the year"); test.equal(m.month(), 1, "keep the month"); test.equal(m.date(), 2, "keep the day"); @@ -196,11 +220,13 @@ exports.end_start_of = { }, "start of second" : function(test) { - test.expect(8); + test.expect(9); var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('second'); var ms = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('seconds'); + var ma = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('s'); test.equal(+m, +ms, "Plural or singular should work"); + test.equal(+m, +ma, "Full or abbreviated should work"); test.equal(m.year(), 2011, "keep the year"); test.equal(m.month(), 1, "keep the month"); test.equal(m.date(), 2, "keep the day"); @@ -212,11 +238,13 @@ exports.end_start_of = { }, "end of second" : function(test) { - test.expect(8); + test.expect(9); var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('second'); var ms = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('seconds'); + var ma = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('s'); test.equal(+m, +ms, "Plural or singular should work"); + test.equal(+m, +ma, "Full or abbreviated should work"); test.equal(m.year(), 2011, "keep the year"); test.equal(m.month(), 1, "keep the month"); test.equal(m.date(), 2, "keep the day");