-
-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(i18n): signalable rt value converter
- Loading branch information
Showing
9 changed files
with
149 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
packages/__tests__/i18n/rt/relative-time-value-converter.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { I18nService, RelativeTimeValueConverter } from '@aurelia/i18n'; | ||
import { EventAggregator } from '@aurelia/kernel'; | ||
import { assert, MockSignaler } from '@aurelia/testing'; | ||
import i18next from 'i18next'; | ||
|
||
describe.only('rt', function () { | ||
async function setup() { | ||
const i18n = new I18nService({ i18next }, {}, new EventAggregator(), new MockSignaler()); | ||
await i18n['task'].wait(); | ||
const sut = new RelativeTimeValueConverter(i18n); | ||
return { i18n, sut }; | ||
} | ||
|
||
for (const value of [undefined, null, 'chaos', 123, true]) { | ||
it(`returns the value itself if the value is not a Date, for example: ${value}`, async function () { | ||
const { sut } = await setup(); | ||
assert.equal(sut.toView(value), value); | ||
}); | ||
} | ||
|
||
it('formats number by default as per current locale and default formatting options', async function () { | ||
const { sut } = await setup(); | ||
const date = new Date(); | ||
date.setHours(date.getHours() - 2); | ||
assert.equal(sut.toView(date), '2 hours ago'); | ||
}); | ||
|
||
it('formats a given number as per given and locale', async function () { | ||
const { sut } = await setup(); | ||
const date = new Date(); | ||
date.setHours(date.getHours() - 2); | ||
assert.equal(sut.toView(date, undefined, 'de'), 'vor 2 Stunden'); | ||
}); | ||
|
||
it('formats a given number as per given formatting options and locale', async function () { | ||
const { sut } = await setup(); | ||
const date = new Date(); | ||
date.setHours(date.getHours() - 2); | ||
assert.equal(sut.toView(date, { style: 'short' }, 'de'), 'vor 2 Std.'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './relative-time-value-converter'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { valueConverter } from '@aurelia/runtime'; | ||
import { I18N, I18N_SIGNAL, I18nService } from '../i18n'; | ||
|
||
export const RT_SIGNAL: string = 'aurelia-relativetime-signal'; | ||
|
||
@valueConverter('rt') | ||
export class RelativeTimeValueConverter { | ||
public readonly signals: string[] = [I18N_SIGNAL, RT_SIGNAL]; | ||
|
||
constructor(@I18N private readonly i18n: I18nService) { } | ||
|
||
public toView(value: unknown, options?: Intl.RelativeTimeFormatOptions, locale?: string) { | ||
|
||
if (!(value instanceof Date)) { | ||
return value; | ||
} | ||
|
||
return this.i18n.rt(value, options, locale); | ||
} | ||
} |