-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3dd0d32
commit b6b5aec
Showing
59 changed files
with
1,351 additions
and
631 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
3 changes: 3 additions & 0 deletions
3
projects/angular-ecmascript-intl/src/lib/currency/intl-currency-pipe-default-options.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,3 @@ | ||
import {InjectionToken} from "@angular/core"; | ||
|
||
export const INTL_CURRENCY_PIPE_DEFAULT_OPTIONS = new InjectionToken<Partial<Intl.NumberFormatOptions>>('IntlCurrencyPipeDefaultOptions'); |
158 changes: 158 additions & 0 deletions
158
projects/angular-ecmascript-intl/src/lib/currency/intl-currency.pipe.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,158 @@ | ||
import {IntlCurrencyPipe} from './intl-currency.pipe'; | ||
import {TestBed} from "@angular/core/testing"; | ||
import {INTL_LOCALES} from "../locale"; | ||
import {INTL_CURRENCY_PIPE_DEFAULT_OPTIONS} from "./intl-currency-pipe-default-options"; | ||
|
||
describe('IntlCurrencyPipe', () => { | ||
let testUnit: IntlCurrencyPipe; | ||
|
||
describe('parsing', () => { | ||
beforeEach(() => { | ||
testUnit = new IntlCurrencyPipe('en-US'); | ||
}); | ||
|
||
it('should create an instance', () => { | ||
expect(testUnit).toBeTruthy(); | ||
}); | ||
|
||
it('should handle null values', () => { | ||
expect(testUnit.transform(null, 'USD')).toBeNull(); | ||
}); | ||
|
||
it('should handle undefined values', () => { | ||
expect(testUnit.transform(undefined, 'USD')).toBeNull(); | ||
}); | ||
|
||
it('should handle empty strings', () => { | ||
expect(testUnit.transform('', 'USD')).toBeNull(); | ||
}); | ||
|
||
it('should transform numbers', () => { | ||
expect(testUnit.transform(1024.224, 'USD')).toEqual('$1,024.22'); | ||
}); | ||
|
||
it('should transform strings', () => { | ||
expect(testUnit.transform('1024.224', 'USD')).toEqual('$1,024.22'); | ||
}); | ||
|
||
it('should handle invalid strings', () => { | ||
expect(() => testUnit.transform('invalid number', 'USD')).toThrow(); | ||
}); | ||
|
||
it('should handle missing Intl.NumberFormat browser API', () => { | ||
// @ts-expect-error | ||
spyOn(Intl, 'NumberFormat').and.returnValue(undefined); | ||
const consoleError = spyOn(console, 'error'); | ||
expect(testUnit.transform('1', 'USD')).toBeNull(); | ||
|
||
expect(consoleError).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
|
||
describe('internationalization', () => { | ||
it('should respect the set locale', () => { | ||
TestBed.configureTestingModule({ | ||
providers: [ | ||
IntlCurrencyPipe, | ||
{ | ||
provide: INTL_LOCALES, | ||
useValue: 'de-DE', | ||
}, | ||
], | ||
}); | ||
testUnit = TestBed.inject(IntlCurrencyPipe); | ||
|
||
expect(testUnit.transform(1024.2249, 'EUR')).toEqual('1.024,22\xa0€'); | ||
}); | ||
|
||
it('should fall back to the browser default locale', () => { | ||
TestBed.configureTestingModule({providers: [IntlCurrencyPipe]}); | ||
|
||
const result1 = TestBed.inject(IntlCurrencyPipe).transform(1024.2249, 'EUR'); | ||
const result2 = new IntlCurrencyPipe(navigator.language).transform(1024.2249, 'EUR'); | ||
|
||
expect(result1).toEqual(result2); | ||
}); | ||
}); | ||
|
||
describe('options', () => { | ||
it('should respect the setting from default config', () => { | ||
TestBed.configureTestingModule({ | ||
providers: [ | ||
IntlCurrencyPipe, | ||
{ | ||
provide: INTL_LOCALES, | ||
useValue: 'en-US', | ||
}, | ||
{ | ||
provide: INTL_CURRENCY_PIPE_DEFAULT_OPTIONS, | ||
useValue: { | ||
signDisplay: 'always', | ||
}, | ||
}, | ||
], | ||
}); | ||
testUnit = TestBed.inject(IntlCurrencyPipe); | ||
|
||
expect(testUnit.transform(1, 'USD')).toEqual('+$1.00'); | ||
|
||
}); | ||
|
||
it('should give the user options a higher priority', () => { | ||
TestBed.configureTestingModule({ | ||
providers: [ | ||
IntlCurrencyPipe, | ||
{ | ||
provide: INTL_LOCALES, | ||
useValue: 'en-US', | ||
}, | ||
{ | ||
provide: INTL_CURRENCY_PIPE_DEFAULT_OPTIONS, | ||
useValue: { | ||
signDisplay: 'exceptZero', | ||
}, | ||
}, | ||
], | ||
}); | ||
testUnit = TestBed.inject(IntlCurrencyPipe); | ||
|
||
expect(testUnit.transform(1, 'USD', {signDisplay: 'never'})).toEqual('$1.00'); | ||
}); | ||
}); | ||
|
||
it('should respect locale option', () => { | ||
TestBed.configureTestingModule({ | ||
providers: [ | ||
IntlCurrencyPipe, | ||
{ | ||
provide: INTL_LOCALES, | ||
useValue: 'en-US', | ||
}, | ||
], | ||
}); | ||
testUnit = TestBed.inject(IntlCurrencyPipe); | ||
|
||
expect(testUnit.transform(1024, 'USD', {locale: 'de-DE'})).toEqual('1.024,00\xa0$'); | ||
}); | ||
|
||
it('should not override the style option', () => { | ||
TestBed.configureTestingModule({ | ||
providers: [ | ||
IntlCurrencyPipe, | ||
{ | ||
provide: INTL_LOCALES, | ||
useValue: 'en-US', | ||
}, | ||
{ | ||
provide: INTL_CURRENCY_PIPE_DEFAULT_OPTIONS, | ||
useValue: { | ||
style: 'percent', | ||
}, | ||
}, | ||
], | ||
}); | ||
testUnit = TestBed.inject(IntlCurrencyPipe); | ||
|
||
expect(testUnit.transform(1, 'USD', {style: 'percent'})).toEqual('$1.00'); | ||
}); | ||
}); |
39 changes: 39 additions & 0 deletions
39
projects/angular-ecmascript-intl/src/lib/currency/intl-currency.pipe.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,39 @@ | ||
import {Inject, Optional, Pipe, PipeTransform} from '@angular/core'; | ||
import {IntlPipeOptions} from "../intl-pipe-options"; | ||
import {INTL_LOCALES} from "../locale"; | ||
import {INTL_CURRENCY_PIPE_DEFAULT_OPTIONS} from "./intl-currency-pipe-default-options"; | ||
import {getNumericValue} from "../utils/number-utils"; | ||
|
||
export type IntlCurrencyPipeOptions = Partial<Intl.NumberFormatOptions> & IntlPipeOptions; | ||
|
||
@Pipe({ | ||
name: 'intlCurrency', | ||
standalone: true, | ||
}) | ||
export class IntlCurrencyPipe implements PipeTransform { | ||
|
||
constructor(@Optional() @Inject(INTL_LOCALES) readonly locale?: string | string[] | null, | ||
@Optional() @Inject(INTL_CURRENCY_PIPE_DEFAULT_OPTIONS) readonly defaultOptions?: Partial<Intl.NumberFormatOptions> | null) { | ||
} | ||
|
||
transform(value: number | string | null | undefined, currency: string, options?: IntlCurrencyPipeOptions): string | null { | ||
if (typeof value !== 'number' && !value) { | ||
return null; | ||
} | ||
|
||
const numericValue = getNumericValue(value); | ||
|
||
const {locale, ...intlOptions} = options ?? {}; | ||
|
||
try { | ||
return new Intl.NumberFormat( | ||
locale ?? this.locale ?? undefined, | ||
{...this.defaultOptions, ...intlOptions, currency, style: 'currency'}, | ||
).format(numericValue); | ||
} catch (e) { | ||
console.error('Error while transforming the currency', e); | ||
return null; | ||
} | ||
} | ||
|
||
} |
5 changes: 2 additions & 3 deletions
5
projects/angular-ecmascript-intl/src/lib/date/intl-date-pipe-default-options.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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
import { InjectionToken } from "@angular/core"; | ||
import { IntlDatePipeOptions } from "./intl-date.pipe"; | ||
import {InjectionToken} from "@angular/core"; | ||
|
||
export const INTL_DATE_PIPE_DEFAULT_OPTIONS = new InjectionToken<IntlDatePipeOptions>('IntlDatePipeDefaultOptions'); | ||
export const INTL_DATE_PIPE_DEFAULT_OPTIONS = new InjectionToken<Partial<Intl.DateTimeFormatOptions>>('IntlDatePipeDefaultOptions'); |
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
3 changes: 3 additions & 0 deletions
3
projects/angular-ecmascript-intl/src/lib/decimal/intl-decimal-pipe-default-options.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,3 @@ | ||
import {InjectionToken} from "@angular/core"; | ||
|
||
export const INTL_DECIMAL_PIPE_DEFAULT_OPTIONS = new InjectionToken<Partial<Intl.NumberFormatOptions>>('IntlDecimalPipeDefaultOptions'); |
Oops, something went wrong.