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 #146 from Cobrowser/master
Browse files Browse the repository at this point in the history
Support from date in time ago filter
  • Loading branch information
urish committed Jul 28, 2015
2 parents f17e607 + 3b9acb5 commit a0880ae
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
11 changes: 9 additions & 2 deletions angular-moment.js
Original file line number Diff line number Diff line change
Expand Up @@ -551,17 +551,24 @@
* @function
*/
.filter('amTimeAgo', ['moment', 'amMoment', 'angularMomentConfig', function (moment, amMoment, angularMomentConfig) {
function amTimeAgoFilter(value, preprocess, suffix) {
function amTimeAgoFilter(value, preprocess, suffix, from) {
var date, dateFrom;

if (typeof value === 'undefined' || value === null) {
return '';
}

value = amMoment.preprocessDate(value, preprocess);
var date = moment(value);
date = moment(value);
if (!date.isValid()) {
return '';
}

dateFrom = moment(from);
if (typeof from !== 'undefined' && dateFrom.isValid()) {
return amMoment.applyTimezone(date).from(dateFrom, suffix);
}

return amMoment.applyTimezone(date).fromNow(suffix);
}

Expand Down
7 changes: 7 additions & 0 deletions tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,13 @@ describe('module angularMoment', function () {
expect(amTimeAgo(date, null, true)).toBe('a few seconds');
});

it('should support started date as fourth parameter', function () {
var date = new Date(2015, 7, 14),
from = new Date(2015, 7, 15);
expect(amTimeAgo(date, null, null, from)).toBe('a day ago');
expect(amTimeAgo(date, null, true, from)).toBe('a day');
});

it('should gracefully handle undefined values', function () {
expect(amTimeAgo()).toBe('');
});
Expand Down

0 comments on commit a0880ae

Please sign in to comment.