UNPKG

angular-ecmascript-intl

Version:

Contains Angular pipes to transform internationalization data using Intl.* browser APIs

485 lines (458 loc) 20.6 kB
import * as i0 from '@angular/core'; import { InjectionToken, inject, Pipe, ChangeDetectorRef, NgModule } from '@angular/core'; import { Subject, interval, takeUntil } from 'rxjs'; const INTL_COUNTRY_PIPE_DEFAULT_OPTIONS = new InjectionToken('IntlCountryPipeDefaultOptions'); /** * Set the locale(s) for all Intl Pipes. If not specified, the user language specified in the browser will be used. If * specified, it needs to be a string with a BCP 47 language tag, or an array of such strings. */ const INTL_LOCALES = new InjectionToken('IntlLocales'); class IntlCountryPipe { constructor() { this.locale = inject(INTL_LOCALES, { optional: true }); this.defaultOptions = inject(INTL_COUNTRY_PIPE_DEFAULT_OPTIONS, { optional: true }); } transform(value, options) { if (!value) { return null; } const { locale, ...intlOptions } = options ?? {}; try { return (new Intl.DisplayNames(locale ?? this.locale ?? undefined, { ...this.defaultOptions, ...intlOptions, type: 'region', }).of(value) ?? null); } catch (e) { console.error('Error while transforming the country', e); return null; } } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.0.0", ngImport: i0, type: IntlCountryPipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe }); } static { this.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "20.0.0", ngImport: i0, type: IntlCountryPipe, isStandalone: true, name: "intlCountry" }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.0.0", ngImport: i0, type: IntlCountryPipe, decorators: [{ type: Pipe, args: [{ name: 'intlCountry', standalone: true, }] }] }); const INTL_CURRENCY_PIPE_DEFAULT_OPTIONS = new InjectionToken('IntlCurrencyPipeDefaultOptions'); const getNumericValue = (value) => { if (typeof value === 'number') { return value; } if (isNaN(Number(value) - parseFloat(value))) { throw new Error(`${value} is not a number!`); } return Number(value); }; class IntlCurrencyPipe { constructor() { this.locale = inject(INTL_LOCALES, { optional: true }); this.defaultOptions = inject(INTL_CURRENCY_PIPE_DEFAULT_OPTIONS, { optional: true }); } transform(value, currency, options) { 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; } } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.0.0", ngImport: i0, type: IntlCurrencyPipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe }); } static { this.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "20.0.0", ngImport: i0, type: IntlCurrencyPipe, isStandalone: true, name: "intlCurrency" }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.0.0", ngImport: i0, type: IntlCurrencyPipe, decorators: [{ type: Pipe, args: [{ name: 'intlCurrency', standalone: true, }] }] }); const INTL_DATE_PIPE_DEFAULT_OPTIONS = new InjectionToken('IntlDatePipeDefaultOptions'); class IntlDatePipe { constructor() { this.locale = inject(INTL_LOCALES, { optional: true }); this.defaultOptions = inject(INTL_DATE_PIPE_DEFAULT_OPTIONS, { optional: true }); } transform(value, options) { if (typeof value !== 'number' && !value) { return null; } const date = new Date(value); if (isNaN(date.getTime())) { return null; } const { locale, ...intlOptions } = options ?? {}; try { return new Intl.DateTimeFormat(locale ?? this.locale ?? undefined, { ...this.defaultOptions, ...intlOptions, }).format(date); } catch (e) { console.error('Error while transforming the date', e); return null; } } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.0.0", ngImport: i0, type: IntlDatePipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe }); } static { this.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "20.0.0", ngImport: i0, type: IntlDatePipe, isStandalone: true, name: "intlDate" }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.0.0", ngImport: i0, type: IntlDatePipe, decorators: [{ type: Pipe, args: [{ name: 'intlDate', standalone: true, }] }] }); const INTL_DECIMAL_PIPE_DEFAULT_OPTIONS = new InjectionToken('IntlDecimalPipeDefaultOptions'); class IntlDecimalPipe { constructor() { this.locale = inject(INTL_LOCALES, { optional: true }); this.defaultOptions = inject(INTL_DECIMAL_PIPE_DEFAULT_OPTIONS, { optional: true }); } transform(value, options) { 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, style: 'decimal', }).format(numericValue); } catch (e) { console.error('Error while transforming the decimal number', e); return null; } } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.0.0", ngImport: i0, type: IntlDecimalPipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe }); } static { this.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "20.0.0", ngImport: i0, type: IntlDecimalPipe, isStandalone: true, name: "intlDecimal" }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.0.0", ngImport: i0, type: IntlDecimalPipe, decorators: [{ type: Pipe, args: [{ name: 'intlDecimal', standalone: true, }] }] }); const INTL_DURATION_PIPE_DEFAULT_OPTIONS = new InjectionToken('IntlDurationPipeDefaultOptions'); class IntlDurationPipe { constructor() { this.locale = inject(INTL_LOCALES, { optional: true }); this.defaultOptions = inject(INTL_DURATION_PIPE_DEFAULT_OPTIONS, { optional: true, }); } transform(value, options) { if (!value) { return null; } const { locale, ...intlOptions } = options ?? {}; try { return new Intl.DurationFormat(locale ?? this.locale ?? undefined, { ...this.defaultOptions, ...intlOptions, }).format(value); } catch (e) { console.error('Error while transforming the duration value', e); return null; } } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.0.0", ngImport: i0, type: IntlDurationPipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe }); } static { this.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "20.0.0", ngImport: i0, type: IntlDurationPipe, isStandalone: true, name: "intlDuration" }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.0.0", ngImport: i0, type: IntlDurationPipe, decorators: [{ type: Pipe, args: [{ name: 'intlDuration', standalone: true, }] }] }); const INTL_LANGUAGE_PIPE_DEFAULT_OPTIONS = new InjectionToken('IntlLanguagePipeDefaultOptions'); class IntlLanguagePipe { constructor() { this.locale = inject(INTL_LOCALES, { optional: true }); this.defaultOptions = inject(INTL_LANGUAGE_PIPE_DEFAULT_OPTIONS, { optional: true }); } transform(value, options) { if (!value) { return null; } const { locale, ...intlOptions } = options ?? {}; try { return (new Intl.DisplayNames(locale ?? this.locale ?? undefined, { ...this.defaultOptions, ...intlOptions, type: 'language', }).of(value) ?? null); } catch (e) { console.error('Error while transforming the language', e); return null; } } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.0.0", ngImport: i0, type: IntlLanguagePipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe }); } static { this.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "20.0.0", ngImport: i0, type: IntlLanguagePipe, isStandalone: true, name: "intlLanguage" }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.0.0", ngImport: i0, type: IntlLanguagePipe, decorators: [{ type: Pipe, args: [{ name: 'intlLanguage', standalone: true, }] }] }); const INTL_LIST_PIPE_DEFAULT_OPTIONS = new InjectionToken('IntlListPipeDefaultOptions'); class IntlListPipe { constructor() { this.locale = inject(INTL_LOCALES, { optional: true }); this.defaultOptions = inject(INTL_LIST_PIPE_DEFAULT_OPTIONS, { optional: true }); } transform(value, options) { if (!value) { return null; } const { locale, ...intlOptions } = options ?? {}; try { return new Intl.ListFormat(locale ?? this.locale ?? undefined, { ...this.defaultOptions, ...intlOptions, }).format(value); } catch (e) { console.error('Error while transforming the list', e); return null; } } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.0.0", ngImport: i0, type: IntlListPipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe }); } static { this.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "20.0.0", ngImport: i0, type: IntlListPipe, isStandalone: true, name: "intlList" }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.0.0", ngImport: i0, type: IntlListPipe, decorators: [{ type: Pipe, args: [{ name: 'intlList', standalone: true, }] }] }); const INTL_PERCENT_PIPE_DEFAULT_OPTIONS = new InjectionToken('IntlPercentPipeDefaultOptions'); class IntlPercentPipe { constructor() { this.locale = inject(INTL_LOCALES, { optional: true }); this.defaultOptions = inject(INTL_PERCENT_PIPE_DEFAULT_OPTIONS, { optional: true }); } transform(value, options) { 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, style: 'percent', }).format(numericValue); } catch (e) { console.error('Error while transforming the percent value', e); return null; } } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.0.0", ngImport: i0, type: IntlPercentPipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe }); } static { this.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "20.0.0", ngImport: i0, type: IntlPercentPipe, isStandalone: true, name: "intlPercent" }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.0.0", ngImport: i0, type: IntlPercentPipe, decorators: [{ type: Pipe, args: [{ name: 'intlPercent', standalone: true, }] }] }); const INTL_RELATIVE_TIME_PIPE_DEFAULT_OPTIONS = new InjectionToken('IntlRelativeTimePipeDefaultOptions'); var Time; (function (Time) { Time[Time["oneSecond"] = 1000] = "oneSecond"; Time[Time["oneMinute"] = 60000] = "oneMinute"; Time[Time["oneHour"] = 3600000] = "oneHour"; Time[Time["oneDay"] = 86400000] = "oneDay"; Time[Time["oneWeek"] = 604800000] = "oneWeek"; Time[Time["oneMonth"] = 2592000000] = "oneMonth"; Time[Time["oneYear"] = 31536000000] = "oneYear"; })(Time || (Time = {})); class IntlRelativeTimePipe { constructor() { this.cdr = inject(ChangeDetectorRef, { optional: true }); this.locales = inject(INTL_LOCALES, { optional: true }); this.defaultOptions = inject(INTL_RELATIVE_TIME_PIPE_DEFAULT_OPTIONS, { optional: true }); } #destroy$; transform(value, options) { if (typeof value !== 'number' && !value) { return null; } const time = new Date(value).getTime(); if (isNaN(time)) { throw new Error(`${value.toString()} is not a valid date`); } this.#destroy(); this.#destroy$ = new Subject(); interval(Time.oneMinute) .pipe(takeUntil(this.#destroy$)) .subscribe(() => this.cdr?.markForCheck()); const relativeTimeFormat = new Intl.RelativeTimeFormat(options?.locale ?? this.locales ?? undefined, { ...this.defaultOptions, ...options }); const currentTime = new Date().getTime(); const factor = time < currentTime ? -1 : 1; const diff = Math.abs(time - currentTime); if (diff > Time.oneYear) { return relativeTimeFormat.format(factor * Math.floor(diff / Time.oneYear), 'year'); } else if (diff > Time.oneMonth) { return relativeTimeFormat.format(factor * Math.floor(diff / Time.oneMonth), 'month'); } else if (diff > Time.oneWeek) { return relativeTimeFormat.format(factor * Math.floor(diff / Time.oneWeek), 'week'); } else if (diff > Time.oneDay) { return relativeTimeFormat.format(factor * Math.floor(diff / Time.oneDay), 'day'); } else if (diff > Time.oneHour) { return relativeTimeFormat.format(factor * Math.floor(diff / Time.oneHour), 'hour'); } else if (diff > Time.oneMinute) { return relativeTimeFormat.format(factor * Math.floor(diff / Time.oneMinute), 'minute'); } else { return relativeTimeFormat.format(0, 'minute'); } } ngOnDestroy() { this.#destroy(); } #destroy() { this.#destroy$?.next(); this.#destroy$?.complete(); } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.0.0", ngImport: i0, type: IntlRelativeTimePipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe }); } static { this.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "20.0.0", ngImport: i0, type: IntlRelativeTimePipe, isStandalone: true, name: "intlRelativeTime", pure: false }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.0.0", ngImport: i0, type: IntlRelativeTimePipe, decorators: [{ type: Pipe, args: [{ name: 'intlRelativeTime', standalone: true, pure: false, }] }] }); const INTL_UNIT_PIPE_DEFAULT_OPTIONS = new InjectionToken('IntlUnitPipeDefaultOptions'); class IntlUnitPipe { constructor() { this.locale = inject(INTL_LOCALES, { optional: true }); this.defaultOptions = inject(INTL_UNIT_PIPE_DEFAULT_OPTIONS, { optional: true }); } transform(value, unit, options) { 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, unit, style: 'unit', }).format(numericValue); } catch (e) { console.error('Error while transforming the unit value', e); return null; } } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.0.0", ngImport: i0, type: IntlUnitPipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe }); } static { this.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "20.0.0", ngImport: i0, type: IntlUnitPipe, isStandalone: true, name: "intlUnit" }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.0.0", ngImport: i0, type: IntlUnitPipe, decorators: [{ type: Pipe, args: [{ name: 'intlUnit', standalone: true, }] }] }); class IntlModule { static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.0.0", ngImport: i0, type: IntlModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); } static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "20.0.0", ngImport: i0, type: IntlModule, imports: [IntlDatePipe, IntlLanguagePipe, IntlDecimalPipe, IntlPercentPipe, IntlCurrencyPipe, IntlCountryPipe, IntlUnitPipe, IntlListPipe, IntlRelativeTimePipe, IntlDurationPipe], exports: [IntlDatePipe, IntlLanguagePipe, IntlDecimalPipe, IntlPercentPipe, IntlCurrencyPipe, IntlCountryPipe, IntlUnitPipe, IntlListPipe, IntlRelativeTimePipe, IntlDurationPipe] }); } static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "20.0.0", ngImport: i0, type: IntlModule }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.0.0", ngImport: i0, type: IntlModule, decorators: [{ type: NgModule, args: [{ imports: [ IntlDatePipe, IntlLanguagePipe, IntlDecimalPipe, IntlPercentPipe, IntlCurrencyPipe, IntlCountryPipe, IntlUnitPipe, IntlListPipe, IntlRelativeTimePipe, IntlDurationPipe, ], exports: [ IntlDatePipe, IntlLanguagePipe, IntlDecimalPipe, IntlPercentPipe, IntlCurrencyPipe, IntlCountryPipe, IntlUnitPipe, IntlListPipe, IntlRelativeTimePipe, IntlDurationPipe, ], }] }] }); /* * Public API Surface of angular-ecmascript-intl */ /** * Generated bundle index. Do not edit. */ export { INTL_COUNTRY_PIPE_DEFAULT_OPTIONS, INTL_CURRENCY_PIPE_DEFAULT_OPTIONS, INTL_DATE_PIPE_DEFAULT_OPTIONS, INTL_DECIMAL_PIPE_DEFAULT_OPTIONS, INTL_DURATION_PIPE_DEFAULT_OPTIONS, INTL_LANGUAGE_PIPE_DEFAULT_OPTIONS, INTL_LIST_PIPE_DEFAULT_OPTIONS, INTL_LOCALES, INTL_PERCENT_PIPE_DEFAULT_OPTIONS, INTL_RELATIVE_TIME_PIPE_DEFAULT_OPTIONS, INTL_UNIT_PIPE_DEFAULT_OPTIONS, IntlCountryPipe, IntlCurrencyPipe, IntlDatePipe, IntlDecimalPipe, IntlDurationPipe, IntlLanguagePipe, IntlListPipe, IntlModule, IntlPercentPipe, IntlRelativeTimePipe, IntlUnitPipe }; //# sourceMappingURL=angular-ecmascript-intl.mjs.map