Skip to content

Commit

Permalink
Merge pull request #324 from UiPath/fix/humanize-duration
Browse files Browse the repository at this point in the history
fix: humanize duration in different locales
  • Loading branch information
caleniuc authored Mar 31, 2023
2 parents a35282f + 07aaaec commit 37f13ee
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 38 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# v14.7.2 (2023-03-31)
* **fix** humanize duration in different locales
* **deps** bump webpack and @angular-devkit/build-angular

# v14.7.1 (2023-03-28)
* **tree-select** add option to expand on select

Expand Down
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.7.1",
"version": "14.7.2",
"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",
"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';
}
}
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.7.1",
"version": "14.7.2",
"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",
"object-hash": "^2.2.0",
Expand Down

0 comments on commit 37f13ee

Please sign in to comment.