@taiga-ui/addon-commerce
Version:
Extension package for Taiga UI related to commerce, payment systems, currencies etc.
76 lines (70 loc) • 3.39 kB
JavaScript
import { tuiCreateOptions } from '@taiga-ui/cdk/utils/di';
import * as i0 from '@angular/core';
import { inject, signal, computed, untracked, Pipe } from '@angular/core';
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';
const TUI_AMOUNT_DEFAULT_OPTIONS = {
currency: null,
currencyAlign: 'start',
sign: 'negative-only',
};
const [TUI_AMOUNT_OPTIONS, tuiAmountOptionsProvider] = tuiCreateOptions(TUI_AMOUNT_DEFAULT_OPTIONS);
function tuiFormatSignSymbol(value, sign) {
if (sign === 'never' || !value || (sign === 'negative-only' && value > 0)) {
return '';
}
return sign === 'force-negative' || (value < 0 && sign !== 'force-positive')
? CHAR_MINUS
: CHAR_PLUS;
}
const DEFAULT_PRECISION = 2;
class TuiAmountPipe {
constructor() {
this.options = inject(TUI_AMOUNT_OPTIONS);
this.format = inject(TUI_NUMBER_FORMAT);
this.value = signal(Number.NaN);
this.currency = signal(this.options.currency);
this.currencyAlign = signal(this.options.currencyAlign);
this.sign = signal(this.options.sign);
this.formatted = computed(() => {
const format = this.format();
const currencySymbol = tuiFormatCurrency(this.currency());
const formatted = tuiFormatNumber(Math.abs(this.value()), {
...format,
precision: Number.isNaN(format.precision)
? DEFAULT_PRECISION
: format.precision,
});
const sign = formatted === '0' ? '' : tuiFormatSignSymbol(this.value(), this.sign());
const space = currencySymbol &&
(currencySymbol?.length > 1 || this.currencyAlign() === 'end')
? CHAR_NO_BREAK_SPACE
: '';
return this.currencyAlign() === 'end'
? `${sign}${formatted}${space}${currencySymbol}`
: `${sign}${currencySymbol}${space}${formatted}`;
});
}
transform(value, currency = this.options.currency, currencyAlign = this.options.currencyAlign, sign = this.options.sign) {
untracked(() => {
this.value.set(value);
this.currency.set(currency);
this.currencyAlign.set(currencyAlign);
this.sign.set(sign);
});
return this.formatted();
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.21", ngImport: i0, type: TuiAmountPipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe }); }
static { this.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "19.2.21", ngImport: i0, type: TuiAmountPipe, isStandalone: true, name: "tuiAmount", pure: false }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.21", ngImport: i0, type: TuiAmountPipe, decorators: [{
type: Pipe,
args: [{ name: 'tuiAmount', pure: false }]
}] });
/**
* 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