Skip to content

feat!: limit options according to EcmaScript specification #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ The input date can be one of the following:
* undefined

The options are the same as the options for `new Intl.DateTimeFormat()`. For a list of the options, see
their [docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat).
their [docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#options).

With the `INTL_DATE_PIPE_DEFAULT_OPTIONS` injection token you can specify default options.

Expand All @@ -85,7 +85,7 @@ The input can be one of the following:
* undefined

The options are the same as the options for `new Intl.NumberFormat()`. For a list of the options, see
their [docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat).
their [docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#options).

With the `INTL_DECIMAL_PIPE_DEFAULT_OPTIONS` injection token you can specify default options.

Expand All @@ -105,7 +105,7 @@ The input can be one of the following:
* undefined

The options are the same as the options for `new Intl.NumberFormat()`. For a list of the options, see
their [docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat).
their [docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#options).

With the `INTL_PERCENT_PIPE_DEFAULT_OPTIONS` injection token you can specify default options.

Expand All @@ -128,7 +128,7 @@ The currency code parameter is required and must be a valid ISO 4217 currency co
number instead, use the `intlDecimal` pipe.

The options are the same as the options for `new Intl.NumberFormat()`. For a list of the options, see
their [docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat).
their [docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#options).

With the `INTL_CURRENCY_PIPE_DEFAULT_OPTIONS` injection token you can specify default options.

Expand All @@ -147,7 +147,7 @@ The input can be one of the following:
* undefined

The options are the same as the options for `new Intl.DisplayNames()`. For a list of the options, see
their [docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DisplayNames/DisplayNames).
their [docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DisplayNames/DisplayNames#options).

With the `INTL_LANGUAGE_PIPE_DEFAULT_OPTIONS` injection token you can specify default options.

Expand All @@ -166,7 +166,7 @@ The input can be one of the following:
* undefined

The options are the same as the options for `new Intl.DisplayNames()`. For a list of the options, see
their [docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DisplayNames/DisplayNames).
their [docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DisplayNames/DisplayNames#options).

With the `INTL_COUNTRY_PIPE_DEFAULT_OPTIONS` injection token you can specify default options.

Expand All @@ -190,7 +190,7 @@ the [specification](https://tc39.es/proposal-unified-intl-numberformat/section6/
for a full list of possible values. If you want to transform a decimal number instead, use the `intlDecimal` pipe.

The options are the same as the options for `new Intl.NumberFormat()`. For a list of the options, see
their [docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat).
their [docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#options).

With the `INTL_UNIT_PIPE_DEFAULT_OPTIONS` injection token you can specify default options.

Expand All @@ -201,7 +201,6 @@ For more context, see the following [GitHub issue](https://github.com/angular/an
## Feature Roadmap

* Performance: Prepare Intl.* object with default options, only construct new object when necessary
* Limit options to only what is allowed by Intl API
* List pipe
* Relative time pipe
* Migration Schematics for usages of Angular pipes
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {InjectionToken} from "@angular/core";
import {IntlCountryPipeOptions} from "./intl-country.pipe";

export const INTL_COUNTRY_PIPE_DEFAULT_OPTIONS = new InjectionToken<Partial<Intl.DisplayNamesOptions>>('IntlCountryPipeDefaultOptions');
export const INTL_COUNTRY_PIPE_DEFAULT_OPTIONS = new InjectionToken<Omit<IntlCountryPipeOptions, 'locale'>>('IntlCountryPipeDefaultOptions');
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {INTL_LOCALES} from "../locale";
import {INTL_COUNTRY_PIPE_DEFAULT_OPTIONS} from "./intl-country-pipe-default-options";
import {IntlPipeOptions} from "../intl-pipe-options";

export type IntlCountryPipeOptions = Partial<Intl.DisplayNamesOptions> & IntlPipeOptions;
export type IntlCountryPipeOptions = Omit<Partial<Intl.DisplayNamesOptions>, 'languageDisplay'> & IntlPipeOptions;

@Pipe({
name: 'intlCountry',
Expand All @@ -12,7 +12,7 @@ export type IntlCountryPipeOptions = Partial<Intl.DisplayNamesOptions> & IntlPip
export class IntlCountryPipe implements PipeTransform {

constructor(@Optional() @Inject(INTL_LOCALES) readonly locale?: string | string[] | null,
@Optional() @Inject(INTL_COUNTRY_PIPE_DEFAULT_OPTIONS) readonly defaultOptions?: Partial<Intl.DisplayNamesOptions> | null) {
@Optional() @Inject(INTL_COUNTRY_PIPE_DEFAULT_OPTIONS) readonly defaultOptions?: Omit<IntlCountryPipeOptions, 'locale'> | null) {
}

transform(value: string | null | undefined, options?: IntlCountryPipeOptions): string | null {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {InjectionToken} from "@angular/core";
import {IntlCurrencyPipeOptions} from "./intl-currency.pipe";

export const INTL_CURRENCY_PIPE_DEFAULT_OPTIONS = new InjectionToken<Partial<Intl.NumberFormatOptions>>('IntlCurrencyPipeDefaultOptions');
export const INTL_CURRENCY_PIPE_DEFAULT_OPTIONS = new InjectionToken<Omit<IntlCurrencyPipeOptions, 'locale'>>('IntlCurrencyPipeDefaultOptions');
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ 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;
type OmitOptions = 'unit' | 'unitDisplay' | 'currency';
export type IntlCurrencyPipeOptions = Omit<Partial<Intl.NumberFormatOptions>, OmitOptions> & IntlPipeOptions;

@Pipe({
name: 'intlCurrency',
Expand All @@ -13,7 +14,7 @@ export type IntlCurrencyPipeOptions = Partial<Intl.NumberFormatOptions> & IntlPi
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) {
@Optional() @Inject(INTL_CURRENCY_PIPE_DEFAULT_OPTIONS) readonly defaultOptions?: Omit<IntlCurrencyPipeOptions, 'locale'> | null) {
}

transform(value: number | string | null | undefined, currency: string, options?: IntlCurrencyPipeOptions): string | null {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {InjectionToken} from "@angular/core";
import {IntlDatePipeOptions} from "./intl-date.pipe";

export const INTL_DATE_PIPE_DEFAULT_OPTIONS = new InjectionToken<Partial<Intl.DateTimeFormatOptions>>('IntlDatePipeDefaultOptions');
export const INTL_DATE_PIPE_DEFAULT_OPTIONS = new InjectionToken<Omit<IntlDatePipeOptions, 'locale'>>('IntlDatePipeDefaultOptions');
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export type IntlDatePipeOptions = Partial<Intl.DateTimeFormatOptions> & IntlPipe
export class IntlDatePipe implements PipeTransform {

constructor(@Optional() @Inject(INTL_LOCALES) readonly locale?: string | string[] | null,
@Optional() @Inject(INTL_DATE_PIPE_DEFAULT_OPTIONS) readonly defaultOptions?: Partial<Intl.DateTimeFormatOptions> | null) {
@Optional() @Inject(INTL_DATE_PIPE_DEFAULT_OPTIONS) readonly defaultOptions?: Omit<IntlDatePipeOptions, 'locale'> | null) {
}

transform(value: string | number | Date | null | undefined, options?: IntlDatePipeOptions): string | null {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {InjectionToken} from "@angular/core";
import {IntlDecimalPipeOptions} from "./intl-decimal.pipe";

export const INTL_DECIMAL_PIPE_DEFAULT_OPTIONS = new InjectionToken<Partial<Intl.NumberFormatOptions>>('IntlDecimalPipeDefaultOptions');
export const INTL_DECIMAL_PIPE_DEFAULT_OPTIONS = new InjectionToken<Omit<IntlDecimalPipeOptions, 'locale'>>('IntlDecimalPipeDefaultOptions');
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {INTL_LOCALES} from "../locale";
import {INTL_DECIMAL_PIPE_DEFAULT_OPTIONS} from "./intl-decimal-pipe-default-options";
import {getNumericValue} from "../utils/number-utils";

export type IntlDecimalPipeOptions = Partial<Intl.NumberFormatOptions> & IntlPipeOptions;
type OmitOptions = 'unit' | 'unitDisplay' | 'currency' | 'currencyDisplay' | 'currencySign';
export type IntlDecimalPipeOptions = Omit<Partial<Intl.NumberFormatOptions>, OmitOptions> & IntlPipeOptions;

@Pipe({
name: 'intlDecimal',
Expand All @@ -13,7 +14,7 @@ export type IntlDecimalPipeOptions = Partial<Intl.NumberFormatOptions> & IntlPip
export class IntlDecimalPipe implements PipeTransform {

constructor(@Optional() @Inject(INTL_LOCALES) readonly locale?: string | string[] | null,
@Optional() @Inject(INTL_DECIMAL_PIPE_DEFAULT_OPTIONS) readonly defaultOptions?: Partial<Intl.NumberFormatOptions> | null) {
@Optional() @Inject(INTL_DECIMAL_PIPE_DEFAULT_OPTIONS) readonly defaultOptions?: Omit<IntlDecimalPipeOptions, 'locale'> | null) {
}

transform(value: number | string | null | undefined, options?: IntlDecimalPipeOptions): string | null {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {InjectionToken} from "@angular/core";
import {IntlLanguagePipeOptions} from "./intl-language.pipe";

export const INTL_LANGUAGE_PIPE_DEFAULT_OPTIONS = new InjectionToken<Partial<Intl.DisplayNamesOptions>>('IntlLanguagePipeDefaultOptions');
export const INTL_LANGUAGE_PIPE_DEFAULT_OPTIONS = new InjectionToken<Omit<IntlLanguagePipeOptions, 'locale'>>('IntlLanguagePipeDefaultOptions');
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export type IntlLanguagePipeOptions = Partial<Intl.DisplayNamesOptions> & IntlPi
export class IntlLanguagePipe implements PipeTransform {

constructor(@Optional() @Inject(INTL_LOCALES) readonly locale?: string | string[] | null,
@Optional() @Inject(INTL_LANGUAGE_PIPE_DEFAULT_OPTIONS) readonly defaultOptions?: Partial<Intl.DisplayNamesOptions> | null) {
@Optional() @Inject(INTL_LANGUAGE_PIPE_DEFAULT_OPTIONS) readonly defaultOptions?: Omit<IntlLanguagePipeOptions, 'locale'> | null) {
}

transform(value: string | null | undefined, options?: IntlLanguagePipeOptions): string | null {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {InjectionToken} from "@angular/core";
import {IntlPercentPipeOptions} from "./intl-percent.pipe";

export const INTL_PERCENT_PIPE_DEFAULT_OPTIONS = new InjectionToken<Partial<Intl.NumberFormatOptions>>('IntlPercentPipeDefaultOptions');
export const INTL_PERCENT_PIPE_DEFAULT_OPTIONS = new InjectionToken<Omit<IntlPercentPipeOptions, 'locale'>>('IntlPercentPipeDefaultOptions');
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {INTL_LOCALES} from "../locale";
import {getNumericValue} from "../utils/number-utils";
import {INTL_PERCENT_PIPE_DEFAULT_OPTIONS} from "./intl-percent-pipe-default-options";

export type IntlPercentPipeOptions = Partial<Intl.NumberFormatOptions> & IntlPipeOptions;
type OmitOptions = 'unit' | 'unitDisplay' | 'currency' | 'currencyDisplay' | 'currencySign';
export type IntlPercentPipeOptions = Omit<Partial<Intl.NumberFormatOptions>, OmitOptions> & IntlPipeOptions;

@Pipe({
name: 'intlPercent',
Expand All @@ -13,7 +14,7 @@ export type IntlPercentPipeOptions = Partial<Intl.NumberFormatOptions> & IntlPip
export class IntlPercentPipe implements PipeTransform {

constructor(@Optional() @Inject(INTL_LOCALES) readonly locale?: string | string[] | null,
@Optional() @Inject(INTL_PERCENT_PIPE_DEFAULT_OPTIONS) readonly defaultOptions?: Partial<Intl.NumberFormatOptions> | null) {
@Optional() @Inject(INTL_PERCENT_PIPE_DEFAULT_OPTIONS) readonly defaultOptions?: Omit<IntlPercentPipeOptions, 'locale'> | null) {
}

transform(value: number | string | null | undefined, options?: IntlPercentPipeOptions): string | null {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {InjectionToken} from "@angular/core";
import {IntlUnitPipeOptions} from "./intl-unit.pipe";

export const INTL_UNIT_PIPE_DEFAULT_OPTIONS = new InjectionToken<Partial<Intl.NumberFormatOptions>>('IntlUnitPipeDefaultOptions');
export const INTL_UNIT_PIPE_DEFAULT_OPTIONS = new InjectionToken<Omit<IntlUnitPipeOptions, 'locale'>>('IntlUnitPipeDefaultOptions');
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {INTL_LOCALES} from "../locale";
import {getNumericValue} from "../utils/number-utils";
import {INTL_UNIT_PIPE_DEFAULT_OPTIONS} from "./intl-unit-pipe-default-options";

export type IntlUnitPipeOptions = Partial<Intl.NumberFormatOptions> & IntlPipeOptions;
type OmitOptions = 'unit' | 'currency' | 'currencyDisplay' | 'currencySign';
export type IntlUnitPipeOptions = Omit<Partial<Intl.NumberFormatOptions>, OmitOptions> & IntlPipeOptions;

@Pipe({
name: 'intlUnit',
Expand All @@ -13,7 +14,7 @@ export type IntlUnitPipeOptions = Partial<Intl.NumberFormatOptions> & IntlPipeOp
export class IntlUnitPipe implements PipeTransform {

constructor(@Optional() @Inject(INTL_LOCALES) readonly locale?: string | string[] | null,
@Optional() @Inject(INTL_UNIT_PIPE_DEFAULT_OPTIONS) readonly defaultOptions?: Partial<Intl.NumberFormatOptions> | null) {
@Optional() @Inject(INTL_UNIT_PIPE_DEFAULT_OPTIONS) readonly defaultOptions?: Omit<IntlUnitPipeOptions, 'locale'> | null) {
}

transform(value: number | string | null | undefined, unit: string | undefined, options?: IntlUnitPipeOptions): string | null {
Expand Down