From ab0a68317120fc7222de2b3d86de86021f7e4c06 Mon Sep 17 00:00:00 2001 From: Pratik Shah Date: Thu, 12 Nov 2015 09:17:41 +0530 Subject: [PATCH 1/2] Add amStartOf and amEndOf filter --- angular-moment.js | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/angular-moment.js b/angular-moment.js index 7ed1199..db49e50 100644 --- a/angular-moment.js +++ b/angular-moment.js @@ -669,7 +669,49 @@ amAddFilter.$stateful = angularMomentConfig.statefulFilters; return amAddFilter; - }]); + }]) + + /** + * @ngdoc filter + * @name angularMoment.filter:amStartOf + * @module angularMoment + * @function + */ + .filter('amStartOf', ['moment', 'angularMomentConfig', function (moment, angularMomentConfig) { + function amStartOfFilter(value, type) { + + if (isUndefinedOrNull(value)) { + return ''; + } + + return moment(value).startOf(type); + } + + amStartOfFilter.$stateful = angularMomentConfig.statefulFilters; + + return amStartOfFilter; + }]) + + /** + * @ngdoc filter + * @name angularMoment.filter:amEndOf + * @module angularMoment + * @function + */ + .filter('amEndOf', ['moment', 'angularMomentConfig', function (moment, angularMomentConfig) { + function amEndOfFilter(value, type) { + + if (isUndefinedOrNull(value)) { + return ''; + } + + return moment(value).endOf(type); + } + + amEndOfFilter.$stateful = angularMomentConfig.statefulFilters; + + return amEndOfFilter; + }]); } if (typeof define === 'function' && define.amd) { From de074f770bcb1df4323aa288bedea1122cda0690 Mon Sep 17 00:00:00 2001 From: Pratik Shah Date: Thu, 12 Nov 2015 09:20:09 +0530 Subject: [PATCH 2/2] Add test case for amStartOf and amEndOf filter and Update README --- README.md | 26 ++++++++++++++++++++++++ tests.js | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/README.md b/README.md index bc445f3..c0ab825 100644 --- a/README.md +++ b/README.md @@ -212,6 +212,32 @@ Example: ``` +### amStartOf filter + +Mutates the original moment by setting it to the start of a unit(minute, hour, day..) of time. + +See [Moment.JS documentation](http://momentjs.com/docs/#/manipulating/start-of/) for a list of supported duration formats. + +Example: + +```html +{{ date | amStartOf:'month' | amLocal }} + +``` + +### amEndOf filter + +Mutates the original moment by setting it to the end of a unit(minute, hour, day..) of time. + +See [Moment.JS documentation](http://momentjs.com/docs/#/manipulating/end-of/) for a list of supported duration formats. + +Example: + +```html +{{ date | amEndOf:'month' | amLocal }} + +``` + ### Time zone support The `amDateFormat` and `amCalendar` filters can be configured to display dates aligned diff --git a/tests.js b/tests.js index f15001c..e7f1c1c 100644 --- a/tests.js +++ b/tests.js @@ -719,6 +719,65 @@ describe('module angularMoment', function () { }); + describe('amStartOf filter', function () { + + var amStartOf; + + beforeEach(function () { + amStartOf = $filter('amStartOf'); + }); + + it('should set date to start of the hour', function () { + var date = new Date(2000, 1, 0, 12, 12, 12); + expect(amStartOf(date, 'hour').toString()).toMatch(/^Mon Jan 31 2000 12:00:00/); + }); + + it('should set date to start of the day', function () { + var date = new Date(2000, 1, 0, 12, 12, 12); + expect(amStartOf(date, 'day').toString()).toMatch(/^Mon Jan 31 2000 00:00:00/); + }); + + it('should set date to start of the week', function () { + var date = new Date(2000, 1, 10, 12, 12, 12); + expect(amStartOf(date, 'week').toString()).toMatch(/^Sun Feb 06 2000 00:00:00/); + }); + + it('should set date to start of the year', function () { + var date = new Date(2000, 6, 6, 12, 12, 12); + expect(amStartOf(date, 'year').toString()).toMatch(/^Sat Jan 01 2000 00:00:00/); + }); + + }); + + describe('amEndOf filter', function () { + + var amEndOf; + + beforeEach(function () { + amEndOf = $filter('amEndOf'); + }); + + it('should set date to end of the hour', function () { + var date = new Date(2000, 0, 1, 12, 12, 12); + expect(amEndOf(date, 'hour').toString()).toMatch(/^Sat Jan 01 2000 12:59:59/); + }); + + it('should set date to end of the day', function () { + var date = new Date(2000, 0, 1, 12, 12, 12); + expect(amEndOf(date, 'day').toString()).toMatch(/^Sat Jan 01 2000 23:59:59/); + }); + + it('should set date to end of the week', function () { + var date = new Date(2000, 0, 10, 12, 12, 12); + expect(amEndOf(date, 'week').toString()).toMatch(/^Sat Jan 15 2000 23:59:59/); + }); + + it('should set date to end of the year', function () { + var date = new Date(2000, 6, 6, 12, 12, 12); + expect(amEndOf(date, 'year').toString()).toMatch(/^Sun Dec 31 2000 23:59:59/); + }); + + }); describe('amMoment service', function () { describe('#changeLocale', function () {