From 522fc63e1529ca752c0787011bed30daaa4145ad Mon Sep 17 00:00:00 2001 From: Andrei Caleniuc Date: Fri, 31 Mar 2023 16:54:09 +0300 Subject: [PATCH 1/2] fix: humanize duration in different locales --- package-lock.json | 24 -------------- package.json | 2 -- .../src/ui-secondformat.directive.spec.ts | 31 +++++++++++++++++++ .../src/ui-secondformat.directive.ts | 22 ++++++++----- projects/angular/package.json | 1 - 5 files changed, 46 insertions(+), 34 deletions(-) diff --git a/package-lock.json b/package-lock.json index 26ef8395d..00617adf8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -20,7 +20,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", @@ -49,7 +48,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", @@ -5497,12 +5495,6 @@ "@types/node": "*" } }, - "node_modules/@types/humanize-duration": { - "version": "3.27.1", - "resolved": "https://registry.npmjs.org/@types/humanize-duration/-/humanize-duration-3.27.1.tgz", - "integrity": "sha512-K3e+NZlpCKd6Bd/EIdqjFJRFHbrq5TzPPLwREk5Iv/YoIjQrs6ljdAUCo+Lb2xFlGNOjGSE0dqsVD19cZL137w==", - "dev": true - }, "node_modules/@types/jasmine": { "version": "3.3.12", "resolved": "https://registry.npmjs.org/@types/jasmine/-/jasmine-3.3.12.tgz", @@ -14404,11 +14396,6 @@ "node": ">=10.17.0" } }, - "node_modules/humanize-duration": { - "version": "3.28.0", - "resolved": "https://registry.npmjs.org/humanize-duration/-/humanize-duration-3.28.0.tgz", - "integrity": "sha512-jMAxraOOmHuPbffLVDKkEKi/NeG8dMqP8lGRd6Tbf7JgAeG33jjgPWDbXXU7ypCI0o+oNKJFgbSB9FKVdWNI2A==" - }, "node_modules/humanize-ms": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/humanize-ms/-/humanize-ms-1.2.1.tgz", @@ -29617,12 +29604,6 @@ "@types/node": "*" } }, - "@types/humanize-duration": { - "version": "3.27.1", - "resolved": "https://registry.npmjs.org/@types/humanize-duration/-/humanize-duration-3.27.1.tgz", - "integrity": "sha512-K3e+NZlpCKd6Bd/EIdqjFJRFHbrq5TzPPLwREk5Iv/YoIjQrs6ljdAUCo+Lb2xFlGNOjGSE0dqsVD19cZL137w==", - "dev": true - }, "@types/jasmine": { "version": "3.3.12", "resolved": "https://registry.npmjs.org/@types/jasmine/-/jasmine-3.3.12.tgz", @@ -36432,11 +36413,6 @@ "integrity": "sha512-B4FFZ6q/T2jhhksgkbEW3HBvWIfDW85snkQgawt07S7J5QXTk6BkNV+0yAeZrM5QpMAdYlocGoljn0sJ/WQkFw==", "dev": true }, - "humanize-duration": { - "version": "3.28.0", - "resolved": "https://registry.npmjs.org/humanize-duration/-/humanize-duration-3.28.0.tgz", - "integrity": "sha512-jMAxraOOmHuPbffLVDKkEKi/NeG8dMqP8lGRd6Tbf7JgAeG33jjgPWDbXXU7ypCI0o+oNKJFgbSB9FKVdWNI2A==" - }, "humanize-ms": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/humanize-ms/-/humanize-ms-1.2.1.tgz", diff --git a/package.json b/package.json index 7e6e95499..694eee150 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/projects/angular/directives/ui-secondformat/src/ui-secondformat.directive.spec.ts b/projects/angular/directives/ui-secondformat/src/ui-secondformat.directive.spec.ts index 32bf919c9..acc881005 100644 --- a/projects/angular/directives/ui-secondformat/src/ui-secondformat.directive.spec.ts +++ b/projects/angular/directives/ui-secondformat/src/ui-secondformat.directive.spec.ts @@ -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).next(); + + fixture.detectChanges(); + expect(text.nativeElement.innerText).toBe(`40${locale.unit}`); + }); + }); + }); }); diff --git a/projects/angular/directives/ui-secondformat/src/ui-secondformat.directive.ts b/projects/angular/directives/ui-secondformat/src/ui-secondformat.directive.ts index 67bcd4546..ab2882f3a 100644 --- a/projects/angular/directives/ui-secondformat/src/ui-secondformat.directive.ts +++ b/projects/angular/directives/ui-secondformat/src/ui-secondformat.directive.ts @@ -1,5 +1,7 @@ -import humanizeDuration from 'humanize-duration'; -import { Duration } from 'luxon'; +import { + Duration, + DurationObjectUnits, +} from 'luxon'; import { BehaviorSubject, merge, @@ -77,6 +79,8 @@ export class UiSecondFormatDirective { private _seconds$ = new BehaviorSubject(null); + private _units: (keyof DurationObjectUnits)[] = ['years', 'months', 'days', 'hours', 'minutes', 'seconds', 'milliseconds']; + /** * @ignore */ @@ -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) => { @@ -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'; + } } diff --git a/projects/angular/package.json b/projects/angular/package.json index 182a134f0..557b77e11 100644 --- a/projects/angular/package.json +++ b/projects/angular/package.json @@ -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", From 07aaaec9990da5e95658b5d1df14ee836561d626 Mon Sep 17 00:00:00 2001 From: Andrei Caleniuc Date: Fri, 31 Mar 2023 16:54:35 +0300 Subject: [PATCH 2/2] chore: bump version to v14.7.2 --- CHANGELOG.md | 4 ++++ package-lock.json | 4 ++-- package.json | 2 +- projects/angular/package.json | 2 +- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 958e89b35..26dcb0a80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/package-lock.json b/package-lock.json index 00617adf8..24d7174d2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "angular-components", - "version": "14.7.1", + "version": "14.7.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "angular-components", - "version": "14.7.1", + "version": "14.7.2", "license": "MIT", "dependencies": { "@angular/animations": "14.2.12", diff --git a/package.json b/package.json index 694eee150..226f8ca16 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "angular-components", - "version": "14.7.1", + "version": "14.7.2", "author": { "name": "UiPath Inc", "url": "https://uipath.com" diff --git a/projects/angular/package.json b/projects/angular/package.json index 557b77e11..30a6fae30 100644 --- a/projects/angular/package.json +++ b/projects/angular/package.json @@ -1,6 +1,6 @@ { "name": "@uipath/angular", - "version": "14.7.1", + "version": "14.7.2", "license": "MIT", "author": { "name": "UiPath Inc",