Skip to content

Commit

Permalink
Plural or singular case on endOf and startOf.
Browse files Browse the repository at this point in the history
  • Loading branch information
timrwood committed Jun 11, 2012
1 parent 4069225 commit d6a43f0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
4 changes: 2 additions & 2 deletions moment.js
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@
var output = this.clone();
// the following switch intentionally omits break keywords
// to utilize falling through the cases.
switch (val) {
switch (val.replace(/s$/, '')) {
case 'year':
output.month(0);
/* falls through */
Expand All @@ -897,7 +897,7 @@
},

endOf: function (val) {
return this.startOf(val).add(val + 's', 1).subtract('milliseconds', 1);
return this.startOf(val).add(val.replace(/s?$/, 's'), 1).subtract('ms', 1);
},

sod: function () {
Expand Down
48 changes: 36 additions & 12 deletions test/moment/sod_eod.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ exports.eod_sod = {
},

"start of year" : function(test) {
test.expect(7);
test.expect(8);

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');
test.equal(+m, +ms, "Plural or singular 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");
Expand All @@ -53,9 +55,11 @@ exports.eod_sod = {
},

"end of year" : function(test) {
test.expect(7);
test.expect(8);

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');
test.equal(+m, +ms, "Plural or singular 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");
Expand All @@ -67,9 +71,11 @@ exports.eod_sod = {
},

"start of month" : function(test) {
test.expect(7);
test.expect(8);

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');
test.equal(+m, +ms, "Plural or singular 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");
Expand All @@ -81,9 +87,11 @@ exports.eod_sod = {
},

"end of month" : function(test) {
test.expect(7);
test.expect(8);

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');
test.equal(+m, +ms, "Plural or singular 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");
Expand All @@ -95,9 +103,11 @@ exports.eod_sod = {
},

"start of day" : function(test) {
test.expect(7);
test.expect(8);

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');
test.equal(+m, +ms, "Plural or singular 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");
Expand All @@ -109,9 +119,11 @@ exports.eod_sod = {
},

"end of day" : function(test) {
test.expect(7);
test.expect(8);

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');
test.equal(+m, +ms, "Plural or singular 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");
Expand All @@ -123,9 +135,11 @@ exports.eod_sod = {
},

"start of hour" : function(test) {
test.expect(7);
test.expect(8);

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');
test.equal(+m, +ms, "Plural or singular 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");
Expand All @@ -137,9 +151,11 @@ exports.eod_sod = {
},

"end of hour" : function(test) {
test.expect(7);
test.expect(8);

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');
test.equal(+m, +ms, "Plural or singular 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");
Expand All @@ -151,9 +167,11 @@ exports.eod_sod = {
},

"start of minute" : function(test) {
test.expect(7);
test.expect(8);

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');
test.equal(+m, +ms, "Plural or singular 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");
Expand All @@ -165,9 +183,11 @@ exports.eod_sod = {
},

"end of minute" : function(test) {
test.expect(7);
test.expect(8);

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');
test.equal(+m, +ms, "Plural or singular 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");
Expand All @@ -179,9 +199,11 @@ exports.eod_sod = {
},

"start of second" : function(test) {
test.expect(7);
test.expect(8);

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');
test.equal(+m, +ms, "Plural or singular 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");
Expand All @@ -193,9 +215,11 @@ exports.eod_sod = {
},

"end of second" : function(test) {
test.expect(7);
test.expect(8);

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');
test.equal(+m, +ms, "Plural or singular 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");
Expand Down

0 comments on commit d6a43f0

Please sign in to comment.