Skip to content

Commit

Permalink
feat(age): add filter 'age' to calculate the age based on the birthdate
Browse files Browse the repository at this point in the history
Merge branch 'rodriguesgm-master' into develop
  • Loading branch information
the-darc committed Mar 6, 2016
2 parents 884884e + ff8ad82 commit 0884d52
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ m.filter('percentage', ['$filter', function($filter) {
thousandsDelimiter = $locale.NUMBER_FORMATS.GROUP_SEP,
currencySym = '';

if(currency === true) {
if (currency === true) {
currencySym = $locale.NUMBER_FORMATS.CURRENCY_SYM + ' ';
} else if (currency) {
currencySym = currency;
Expand All @@ -66,4 +66,27 @@ m.filter('percentage', ['$filter', function($filter) {
return function(input) {
return BrM.nfeAccessKey(input);
};
}]);
}])
.filter('age', function() {
/**
* @param value birthdate can be a date object or a time in milliseconds
* return the age based on the birthdate or undefined if value is invalid.
*/
return function calculateAge(value) {
if (!value) {
return undefined;
}
var isDateInstance = (value instanceof Date);
var isValidType = isDateInstance || !isNaN(parseInt(value));
if (!isValidType) {
return undefined;
}
var birthdate = isDateInstance ? value : new Date(value);
if (birthdate > new Date()) {
return undefined;
}
var ageDifMs = Date.now() - birthdate.getTime();
var ageDate = new Date(ageDifMs); // miliseconds from epoch
return Math.abs(ageDate.getUTCFullYear() - 1970);
}
});
32 changes: 32 additions & 0 deletions src/filters.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,36 @@ describe('br-filters', function() {
.toBe('3514 0111 7242 5800 0157 5500 1000 6882 1916 3038 6000');
});
});
describe('age', function() {
it('should be undefined if null, undefined or empty', function() {
expect(testFilter('age')(null)).toBe(undefined);
expect(testFilter('age')(undefined)).toBe(undefined);
expect(testFilter('age')('')).toBe(undefined);
});

it('should be undefined if not date or time in milliseconds', function() {
expect(testFilter('age')('not a date')).toBe(undefined);
expect(testFilter('age')(true)).toBe(undefined);
});

it('should be undefined for future birthdate', function() {
var futureYear = new Date();
futureYear.setFullYear(futureYear.getFullYear() + 1);
expect(testFilter('age')(futureYear)).toBe(undefined);
var futureMonth = new Date();
futureMonth.setMonth(futureMonth.getMonth() + 1);
expect(testFilter('age')(futureMonth)).toBe(undefined);
var futureDay = new Date();
futureDay.setDate(futureDay.getDate() + 1);
expect(testFilter('age')(futureDay)).toBe(undefined);
var futureMinute = new Date();
futureMinute.setMinutes(futureMinute.getMinutes() + 1);
expect(testFilter('age')(futureMinute)).toBe(undefined);
});

it('should format date as 27', function() {
expect(testFilter('age')(new Date(1988, 10, 07))).toBe(27);
expect(testFilter('age')(597463200000)).toBe(27);
});
})
});

0 comments on commit 0884d52

Please sign in to comment.