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 34ccc33 commit 9fb1db1
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 44 deletions.
28 changes: 2 additions & 26 deletions package-lock.json

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

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-components",
"version": "14.5.3",
"version": "14.5.4",
"author": {
"name": "UiPath Inc",
"url": "https://uipath.com"
Expand Down 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",
"moment": "2.29.4",
Expand Down Expand Up @@ -112,7 +111,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 @@ -112,7 +112,6 @@ export const resolveTimezone = (options: IDateFormatOptions) => {
* Optionally, you can opt-in to use Luxon instead of Moment.
* Depends On:
* - [luxon](https://www.npmjs.com/package/luxon)
* - [humanize-duration](https://www.npmjs.com/package/humanize-duration)
*
* @export
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Settings } from 'luxon';
import {
BehaviorSubject,
firstValueFrom,
Expand All @@ -13,9 +14,8 @@ import {
waitForAsync,
} from '@angular/core/testing';
import { By } from '@angular/platform-browser';

import { Settings } from 'luxon';
import { USE_LUXON } from '@uipath/angular/utilities';

import {
ISecondFormatOptions,
UiSecondFormatDirective,
Expand Down Expand Up @@ -161,4 +161,35 @@ describe('Directive: UiSecondFormat with luxon', () => {
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 moment from 'moment';
import {
BehaviorSubject,
Expand Down Expand Up @@ -55,7 +57,6 @@ export const UI_SECONDFORMAT_OPTIONS = new InjectionToken<Observable<void>>('UiS
* Optionally, you can opt-in to use Luxon instead of Moment.
* Depends On:
* - [luxon](https://www.npmjs.com/package/luxon)
* - [humanize-duration](https://www.npmjs.com/package/humanize-duration)
*
* @export
*/
Expand Down Expand Up @@ -88,6 +89,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 @@ -134,13 +137,15 @@ export class UiSecondFormatDirective {
return '';
}

return moment.isDuration(duration)
? duration.humanize()
: humanizeDuration(duration.toMillis(), {
language: duration.locale,
// Max number of units is set to 1 to mimic what moment does
largest: 1,
});
if (moment.isDuration(duration)) {
return duration.humanize();
}

const rescaledDuration = duration.rescale();

const largestUnit = this._getDurationLargestUnit(rescaledDuration);

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

private _mapDurationToTooltip = (duration: Duration | moment.Duration | null) => {
Expand All @@ -152,4 +157,8 @@ export class UiSecondFormatDirective {
? duration.toISOString()
: duration.shiftTo('hours', 'minutes', 'seconds').toISO();
};

private _getDurationLargestUnit(duration: Duration) {
return this._units.find(unit => !!duration[unit]) ?? 'seconds';
}
}
3 changes: 1 addition & 2 deletions projects/angular/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@uipath/angular",
"version": "14.5.3",
"version": "14.5.4",
"license": "MIT",
"author": {
"name": "UiPath Inc",
Expand Down 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",
"moment": "^2.29.1",
Expand Down

0 comments on commit 9fb1db1

Please sign in to comment.