Skip to content

Commit 522fc63

Browse files
Andrei CaleniucAndrei Caleniuc
authored andcommitted
fix: humanize duration in different locales
1 parent a35282f commit 522fc63

File tree

5 files changed

+46
-34
lines changed

5 files changed

+46
-34
lines changed

package-lock.json

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

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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
"object-hash": "2.2.0",
@@ -110,7 +109,6 @@
110109
"@types/chalk": "^2.2.0",
111110
"@types/clipboard": "2.0.7",
112111
"@types/faker": "4.1.5",
113-
"@types/humanize-duration": "3.27.1",
114112
"@types/jasmine": "3.3.12",
115113
"@types/jasmine_dom_matchers": "^1.4.4",
116114
"@types/jasminewd2": "2.0.6",

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,35 @@ describe('Directive: UiSecondFormat', () => {
156156
expect(enTooltip).toBe(jaTooltip);
157157
});
158158
});
159+
160+
describe('humanize in different locales', () => {
161+
[
162+
{
163+
code: 'es-mx',
164+
unit: ' segundos',
165+
},
166+
{
167+
code: 'pt-br',
168+
unit: ' segundos',
169+
},
170+
{
171+
code: 'zh-cn',
172+
unit: '秒钟',
173+
},
174+
].forEach(locale => {
175+
it(`should humanize in ${locale.code}`, async () => {
176+
Settings.defaultLocale = locale.code;
177+
178+
component.seconds = 40;
179+
fixture.detectChanges();
180+
181+
const text = fixture.debugElement.query(By.directive(UiSecondFormatDirective));
182+
183+
(options.redraw$ as BehaviorSubject<void>).next();
184+
185+
fixture.detectChanges();
186+
expect(text.nativeElement.innerText).toBe(`40${locale.unit}`);
187+
});
188+
});
189+
});
159190
});

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

Lines changed: 15 additions & 7 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 {
46
BehaviorSubject,
57
merge,
@@ -77,6 +79,8 @@ export class UiSecondFormatDirective {
7779

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

82+
private _units: (keyof DurationObjectUnits)[] = ['years', 'months', 'days', 'hours', 'minutes', 'seconds', 'milliseconds'];
83+
8084
/**
8185
* @ignore
8286
*/
@@ -118,11 +122,11 @@ export class UiSecondFormatDirective {
118122
return '';
119123
}
120124

121-
return humanizeDuration(duration.toMillis(), {
122-
language: duration.locale,
123-
// Max number of units is set to 1 to mimic what moment does
124-
largest: 1,
125-
});
125+
const rescaledDuration = duration.rescale();
126+
127+
const largestUnit = this._getDurationLargestUnit(rescaledDuration);
128+
129+
return Duration.fromObject({ [largestUnit]: rescaledDuration[largestUnit] }).toHuman();
126130
};
127131

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

133137
return duration.shiftTo('hours', 'minutes', 'seconds').toISO();
134138
};
139+
140+
private _getDurationLargestUnit(duration: Duration) {
141+
return this._units.find(unit => !!duration[unit]) ?? 'seconds';
142+
}
135143
}

projects/angular/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
"object-hash": "^2.2.0",

0 commit comments

Comments
 (0)