Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

Commit

Permalink
Merge pull request #203 from joshsoftware/filters
Browse files Browse the repository at this point in the history
Support for startOf, endOf #184
  • Loading branch information
urish committed Nov 12, 2015
2 parents f2bb4b6 + de074f7 commit 6e8cd1e
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 1 deletion.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<span>{{ date | amStartOf:'month' | amLocal }}</span>

```

### 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
<span>{{ date | amEndOf:'month' | amLocal }}</span>

```

### Time zone support

The `amDateFormat` and `amCalendar` filters can be configured to display dates aligned
Expand Down
44 changes: 43 additions & 1 deletion angular-moment.js
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,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) {
Expand Down
59 changes: 59 additions & 0 deletions tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,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 () {
Expand Down

0 comments on commit 6e8cd1e

Please sign in to comment.