Skip to content

Commit

Permalink
fix: humanize duration in different locales
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei Caleniuc authored and Andrei Caleniuc committed Mar 31, 2023
1 parent a35282f commit 522fc63
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 34 deletions.
24 changes: 0 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
"@angular/platform-browser-dynamic": "14.2.12",
"@angular/router": "14.2.12",
"clipboard": "2.0.8",
"humanize-duration": "3.28.0",
"lodash-es": "4.17.21",
"luxon": "3.2.1",
"object-hash": "2.2.0",
Expand Down Expand Up @@ -110,7 +109,6 @@
"@types/chalk": "^2.2.0",
"@types/clipboard": "2.0.7",
"@types/faker": "4.1.5",
"@types/humanize-duration": "3.27.1",
"@types/jasmine": "3.3.12",
"@types/jasmine_dom_matchers": "^1.4.4",
"@types/jasminewd2": "2.0.6",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,35 @@ describe('Directive: UiSecondFormat', () => {
expect(enTooltip).toBe(jaTooltip);
});
});

describe('humanize in different locales', () => {
[
{
code: 'es-mx',
unit: ' segundos',
},
{
code: 'pt-br',
unit: ' segundos',
},
{
code: 'zh-cn',
unit: '秒钟',
},
].forEach(locale => {
it(`should humanize in ${locale.code}`, async () => {
Settings.defaultLocale = locale.code;

component.seconds = 40;
fixture.detectChanges();

const text = fixture.debugElement.query(By.directive(UiSecondFormatDirective));

(options.redraw$ as BehaviorSubject<void>).next();

fixture.detectChanges();
expect(text.nativeElement.innerText).toBe(`40${locale.unit}`);
});
});
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import humanizeDuration from 'humanize-duration';
import { Duration } from 'luxon';
import {
Duration,
DurationObjectUnits,
} from 'luxon';
import {
BehaviorSubject,
merge,
Expand Down Expand Up @@ -77,6 +79,8 @@ export class UiSecondFormatDirective {

private _seconds$ = new BehaviorSubject<number | null>(null);

private _units: (keyof DurationObjectUnits)[] = ['years', 'months', 'days', 'hours', 'minutes', 'seconds', 'milliseconds'];

/**
* @ignore
*/
Expand Down Expand Up @@ -118,11 +122,11 @@ export class UiSecondFormatDirective {
return '';
}

return humanizeDuration(duration.toMillis(), {
language: duration.locale,
// Max number of units is set to 1 to mimic what moment does
largest: 1,
});
const rescaledDuration = duration.rescale();

const largestUnit = this._getDurationLargestUnit(rescaledDuration);

return Duration.fromObject({ [largestUnit]: rescaledDuration[largestUnit] }).toHuman();
};

private _mapDurationToTooltip = (duration: Duration | null) => {
Expand All @@ -132,4 +136,8 @@ export class UiSecondFormatDirective {

return duration.shiftTo('hours', 'minutes', 'seconds').toISO();
};

private _getDurationLargestUnit(duration: Duration) {
return this._units.find(unit => !!duration[unit]) ?? 'seconds';
}
}
1 change: 0 additions & 1 deletion projects/angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"@angular/platform-browser-dynamic": ">=14.1.0",
"@angular/router": ">=14.1.0",
"clipboard": "^2.0.8",
"humanize-duration": "^3.28.0",
"lodash-es": "^4.17.21",
"luxon": "^3.2.1",
"object-hash": "^2.2.0",
Expand Down

0 comments on commit 522fc63

Please sign in to comment.