-
-
Notifications
You must be signed in to change notification settings - Fork 149
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 nf value-converter
- Loading branch information
Showing
9 changed files
with
152 additions
and
10 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
40 changes: 40 additions & 0 deletions
40
packages/__tests__/i18n/nf/number-format-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,40 @@ | ||
import { I18nService, NumberFormatValueConverter } from '@aurelia/i18n'; | ||
import { EventAggregator } from '@aurelia/kernel'; | ||
import { assert, MockSignaler } from '@aurelia/testing'; | ||
import i18next from 'i18next'; | ||
|
||
describe.only('nf', function () { | ||
async function setup() { | ||
const i18n = new I18nService({ i18next }, {}, new EventAggregator(), new MockSignaler()); | ||
await i18n['task'].wait(); | ||
const sut = new NumberFormatValueConverter(i18n); | ||
return { i18n, sut }; | ||
} | ||
|
||
for (const value of [undefined, null, 'chaos', new Date(), true]) { | ||
it(`returns the value itself if the value is not a number, 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(); | ||
assert.equal(sut.toView(123456789.12), '123,456,789.12'); | ||
}); | ||
|
||
it('formats a given number as per given formatting options', async function () { | ||
const { sut } = await setup(); | ||
assert.equal(sut.toView(123456789.12, { style: 'currency', currency: 'EUR' }), '€123,456,789.12'); | ||
}); | ||
|
||
it('formats a given number as per given locale', async function () { | ||
const { sut } = await setup(); | ||
assert.equal(sut.toView(123456789.12, undefined, 'de'), '123.456.789,12'); | ||
}); | ||
|
||
it('formats a given number as per given locale and formating options', async function () { | ||
const { sut } = await setup(); | ||
assert.equal(sut.toView(123456789.12, { style: 'currency', currency: 'EUR' }, 'de'), '123.456.789,12\u00A0€'); | ||
}); | ||
}); |
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 './number-format-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,18 @@ | ||
import { valueConverter } from '@aurelia/runtime'; | ||
import { I18N, I18N_SIGNAL, I18nService } from '../i18n'; | ||
|
||
@valueConverter('nf') | ||
export class NumberFormatValueConverter { | ||
public readonly signals: string[] = [I18N_SIGNAL]; | ||
|
||
constructor(@I18N private readonly i18n: I18nService) { } | ||
|
||
public toView(value: unknown, options?: Intl.NumberFormatOptions, locale?: string) { | ||
|
||
if (typeof value !== 'number') { | ||
return value; | ||
} | ||
|
||
return this.i18n.nf(value, options, locale); | ||
} | ||
} |