Skip to content
This repository has been archived by the owner on Jul 9, 2024. It is now read-only.

Commit

Permalink
feat(format): 2396 '365 days' rounds up to a year
Browse files Browse the repository at this point in the history
Closes #2396
  • Loading branch information
Terry Lin authored and Terry Lin committed May 12, 2016
1 parent 0e441d0 commit 37992e9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
28 changes: 13 additions & 15 deletions app/scripts/components/ui/string/string.filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,19 @@ module ngApp.components.ui.string {

class AgeDisplay {
constructor(gettextCatalog: any) {
return function(ageInDays: number) {
if (!ageInDays) {
return '--';
}
if (ageInDays < 365.25) {
var daysText = gettextCatalog.getPlural(ageInDays, "day", "days");
return ageInDays + " " + daysText;
} else {
var ageInYears = Math.floor(ageInDays / 365.25);
var remainderDays = Math.ceil(ageInDays % 365.25);
var yearsText = gettextCatalog.getPlural(ageInYears, "year", "years");
var daysText = gettextCatalog.getPlural(remainderDays, "day", "days");
return ageInYears + " " + yearsText + (remainderDays ? " " + remainderDays + " " + daysText : "");
}
};
const oneYear = 365.25;
const leapThenPair = (years: number, days: number): number[] => (days === 365) ? [years + 1, 0] : [years, days];
const timeString = (number: number, singular: string, plural: string): string =>
number ? ('' + number + ' ' + gettextCatalog.getPlural(number, singular, plural || singular + 's')) : '';
// if ES6 is ever used, use `...` instead.
const _timeString = _.spread(timeString);

return (ageInDays: number): string => ageInDays ?
_.zip(leapThenPair(Math.floor(ageInDays / oneYear), Math.ceil(ageInDays % oneYear)), ['year', 'day'])
.filter(p => p[0] > 0)
.map(p => _timeString(p))
.join(' ') :
'--';
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ describe("AgeDisplay Filter:", function() {
expect($filter("ageDisplay")(age)).to.equal(age + " days");
age = 366;
expect($filter("ageDisplay")(age)).to.equal("1 year 1 day");
age = 729;
expect($filter("ageDisplay")(age)).to.equal("1 year 364 days");
age = 730;
expect($filter("ageDisplay")(age)).to.equal("1 year 365 days");
expect($filter("ageDisplay")(age)).to.equal("2 years");
age = 730.5;
expect($filter("ageDisplay")(age)).to.equal("2 years");
age = 731
age = 731;
expect($filter("ageDisplay")(age)).to.equal("2 years 1 day");

}));

});

0 comments on commit 37992e9

Please sign in to comment.