@taiga-ui/addon-commerce
Version:
Extension package for Taiga UI related to commerce, payment systems, currencies etc.
73 lines (67 loc) • 3.1 kB
JavaScript
import * as i0 from '@angular/core';
import { InjectionToken, inject, Pipe } from '@angular/core';
import { tuiProvideOptions } from '@taiga-ui/cdk/utils/miscellaneous';
import { tuiFormatCurrency } from '@taiga-ui/addon-commerce/utils';
import { CHAR_MINUS, CHAR_PLUS, CHAR_NO_BREAK_SPACE } from '@taiga-ui/cdk/constants';
import { TUI_NUMBER_FORMAT } from '@taiga-ui/core/tokens';
import { tuiFormatNumber } from '@taiga-ui/core/utils/format';
import { map } from 'rxjs';
const TUI_AMOUNT_DEFAULT_OPTIONS = {
currency: null,
currencyAlign: 'left',
sign: 'negative-only',
};
const TUI_AMOUNT_OPTIONS = new InjectionToken(ngDevMode ? 'TUI_AMOUNT_OPTIONS' : '', {
factory: () => TUI_AMOUNT_DEFAULT_OPTIONS,
});
function tuiAmountOptionsProvider(options) {
return tuiProvideOptions(TUI_AMOUNT_OPTIONS, options, TUI_AMOUNT_DEFAULT_OPTIONS);
}
function tuiFormatSignSymbol(value, sign) {
if (sign === 'never' || !value || (sign === 'negative-only' && value > 0)) {
return '';
}
if (sign === 'force-negative' || (value < 0 && sign !== 'force-positive')) {
return CHAR_MINUS;
}
return CHAR_PLUS;
}
const DEFAULT_PRECISION = 2;
class TuiAmountPipe {
constructor() {
this.options = inject(TUI_AMOUNT_OPTIONS);
this.format = inject(TUI_NUMBER_FORMAT);
}
transform(value, currency = this.options.currency, currencyAlign = this.options.currencyAlign) {
return this.format.pipe(map((format) => {
const currencySymbol = tuiFormatCurrency(currency);
const formatted = tuiFormatNumber(Math.abs(value), {
...format,
precision: Number.isNaN(format.precision)
? DEFAULT_PRECISION
: format.precision,
});
const sign = formatted === '0'
? ''
: tuiFormatSignSymbol(value, this.options.sign);
const space = currencySymbol &&
(currencySymbol?.length > 1 || currencyAlign === 'right')
? CHAR_NO_BREAK_SPACE
: '';
return currencyAlign === 'right'
? `${sign}${formatted}${space}${currencySymbol}`
: `${sign}${currencySymbol}${space}${formatted}`;
}));
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: TuiAmountPipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe }); }
static { this.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "16.2.12", ngImport: i0, type: TuiAmountPipe, isStandalone: true, name: "tuiAmount" }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: TuiAmountPipe, decorators: [{
type: Pipe,
args: [{ standalone: true, name: 'tuiAmount' }]
}] });
/**
* Generated bundle index. Do not edit.
*/
export { TUI_AMOUNT_DEFAULT_OPTIONS, TUI_AMOUNT_OPTIONS, TuiAmountPipe, tuiAmountOptionsProvider, tuiFormatSignSymbol };
//# sourceMappingURL=taiga-ui-addon-commerce-pipes-amount.mjs.map