Skip to content

Commit 8021658

Browse files
authored
Merge pull request #323 from UiPath/fix/humanize-duration-v14.5
[v14.5] fix: humanize duration in different locales
2 parents 34ccc33 + 4d659cb commit 8021658

File tree

7 files changed

+59
-44
lines changed

7 files changed

+59
-44
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# v14.5.5 (2023-03-31)
2+
* **fix** humanize duration in different locales
3+
14
# v14.5.3 (2023-03-03)
25
* **grid** add test for tooltip on focus
36
* **grid** display title & description tooltip on focus

package-lock.json

Lines changed: 2 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-components",
3-
"version": "14.5.3",
3+
"version": "14.5.5",
44
"author": {
55
"name": "UiPath Inc",
66
"url": "https://uipath.com"
@@ -81,7 +81,6 @@
8181
"@angular/platform-browser-dynamic": "14.2.12",
8282
"@angular/router": "14.2.12",
8383
"clipboard": "2.0.8",
84-
"humanize-duration": "3.28.0",
8584
"lodash-es": "4.17.21",
8685
"luxon": "3.2.1",
8786
"moment": "2.29.4",
@@ -112,7 +111,6 @@
112111
"@types/chalk": "^2.2.0",
113112
"@types/clipboard": "2.0.7",
114113
"@types/faker": "4.1.5",
115-
"@types/humanize-duration": "3.27.1",
116114
"@types/jasmine": "3.3.12",
117115
"@types/jasmine_dom_matchers": "^1.4.4",
118116
"@types/jasminewd2": "2.0.6",

projects/angular/directives/ui-dateformat/src/ui-dateformat.directive.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ export const resolveTimezone = (options: IDateFormatOptions) => {
112112
* Optionally, you can opt-in to use Luxon instead of Moment.
113113
* Depends On:
114114
* - [luxon](https://www.npmjs.com/package/luxon)
115-
* - [humanize-duration](https://www.npmjs.com/package/humanize-duration)
116115
*
117116
* @export
118117
*/

projects/angular/directives/ui-secondformat/src/ui-secondformat.directive.luxon.spec.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Settings } from 'luxon';
12
import {
23
BehaviorSubject,
34
firstValueFrom,
@@ -13,9 +14,8 @@ import {
1314
waitForAsync,
1415
} from '@angular/core/testing';
1516
import { By } from '@angular/platform-browser';
16-
17-
import { Settings } from 'luxon';
1817
import { USE_LUXON } from '@uipath/angular/utilities';
18+
1919
import {
2020
ISecondFormatOptions,
2121
UiSecondFormatDirective,
@@ -161,4 +161,35 @@ describe('Directive: UiSecondFormat with luxon', () => {
161161
expect(enTooltip).toBe(jaTooltip);
162162
});
163163
});
164+
165+
describe('humanize in different locales', () => {
166+
[
167+
{
168+
code: 'es-mx',
169+
unit: ' segundos',
170+
},
171+
{
172+
code: 'pt-br',
173+
unit: ' segundos',
174+
},
175+
{
176+
code: 'zh-cn',
177+
unit: '秒钟',
178+
},
179+
].forEach(locale => {
180+
it(`should humanize in ${locale.code}`, async () => {
181+
Settings.defaultLocale = locale.code;
182+
183+
component.seconds = 40;
184+
fixture.detectChanges();
185+
186+
const text = fixture.debugElement.query(By.directive(UiSecondFormatDirective));
187+
188+
(options.redraw$ as BehaviorSubject<void>).next();
189+
190+
fixture.detectChanges();
191+
expect(text.nativeElement.innerText).toBe(`40${locale.unit}`);
192+
});
193+
});
194+
});
164195
});

projects/angular/directives/ui-secondformat/src/ui-secondformat.directive.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import humanizeDuration from 'humanize-duration';
2-
import { Duration } from 'luxon';
1+
import {
2+
Duration,
3+
DurationObjectUnits,
4+
} from 'luxon';
35
import moment from 'moment';
46
import {
57
BehaviorSubject,
@@ -55,7 +57,6 @@ export const UI_SECONDFORMAT_OPTIONS = new InjectionToken<Observable<void>>('UiS
5557
* Optionally, you can opt-in to use Luxon instead of Moment.
5658
* Depends On:
5759
* - [luxon](https://www.npmjs.com/package/luxon)
58-
* - [humanize-duration](https://www.npmjs.com/package/humanize-duration)
5960
*
6061
* @export
6162
*/
@@ -88,6 +89,8 @@ export class UiSecondFormatDirective {
8889

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

92+
private _units: (keyof DurationObjectUnits)[] = ['years', 'months', 'days', 'hours', 'minutes', 'seconds', 'milliseconds'];
93+
9194
/**
9295
* @ignore
9396
*/
@@ -134,13 +137,15 @@ export class UiSecondFormatDirective {
134137
return '';
135138
}
136139

137-
return moment.isDuration(duration)
138-
? duration.humanize()
139-
: humanizeDuration(duration.toMillis(), {
140-
language: duration.locale,
141-
// Max number of units is set to 1 to mimic what moment does
142-
largest: 1,
143-
});
140+
if (moment.isDuration(duration)) {
141+
return duration.humanize();
142+
}
143+
144+
const rescaledDuration = duration.rescale();
145+
146+
const largestUnit = this._getDurationLargestUnit(rescaledDuration);
147+
148+
return Duration.fromObject({ [largestUnit]: rescaledDuration[largestUnit] }).toHuman();
144149
};
145150

146151
private _mapDurationToTooltip = (duration: Duration | moment.Duration | null) => {
@@ -152,4 +157,8 @@ export class UiSecondFormatDirective {
152157
? duration.toISOString()
153158
: duration.shiftTo('hours', 'minutes', 'seconds').toISO();
154159
};
160+
161+
private _getDurationLargestUnit(duration: Duration) {
162+
return this._units.find(unit => !!duration[unit]) ?? 'seconds';
163+
}
155164
}

projects/angular/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@uipath/angular",
3-
"version": "14.5.3",
3+
"version": "14.5.5",
44
"license": "MIT",
55
"author": {
66
"name": "UiPath Inc",
@@ -39,7 +39,6 @@
3939
"@angular/platform-browser-dynamic": ">=14.1.0",
4040
"@angular/router": ">=14.1.0",
4141
"clipboard": "^2.0.8",
42-
"humanize-duration": "^3.28.0",
4342
"lodash-es": "^4.17.21",
4443
"luxon": "^3.2.1",
4544
"moment": "^2.29.1",

0 commit comments

Comments
 (0)