All files / src/components/zd-currency currency.ts

100% Statements 30/30
100% Branches 14/14
100% Functions 3/3
100% Lines 30/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 5993x 93x 93x 93x           93x           12x         12x   12x     12x 12x 12x 12x 12x       93x 5x   5x 1x 1x   5x 5x 5x     93x 5x   5x 1x 1x   5x 5x 5x     93x  
import { Accessor, Config, FormatterParserProvider, Loader } from '@zeedhi/core';
import { AutoNumeric } from '../../index';
import { InputFactory } from '../zd-input/input-factory';
import { NumberInput as ZdNumber } from '../zd-number-input/number-input';
import { ICurrency } from './interfaces';
 
/**
 * Base class for Currency component
 */
export class Currency extends ZdNumber implements ICurrency {
	/*
	 * Mask definition
	 */
	public mask: any;
 
	protected defaultMask: any = {
		...Config.masks.numberMask,
		...Config.masks.currencyMask,
	};
 
	protected formatterFn = FormatterParserProvider.getFormatter('ZdCurrency');
 
	protected parserFn = FormatterParserProvider.getParser('ZdCurrency');
 
	constructor(props: ICurrency) {
		super(props);
		const newMask = { ...this.defaultMask, ...props.mask };
		this.mask = this.getInitValue('mask', newMask, this.defaultMask);
		this.createAccessors();
		this.validateMask();
	}
}
 
FormatterParserProvider.registerFormatter('ZdCurrency', (value: any, { mask = {} }: any = {}) => {
	let maskDef = mask;
 
	if (Accessor.isAccessorDefinition(maskDef)) {
		const [controller, accessor] = Accessor.getAccessor(maskDef);
		maskDef = Loader.getInstance(controller)[accessor];
	}
	let maskValue = typeof maskDef === 'function' ? maskDef(AutoNumeric.unformat(value)) : maskDef;
	maskValue = { ...Config.masks.numberMask, ...Config.masks.currencyMask, ...maskValue };
	return value === null ? '' : AutoNumeric.format(value, maskValue);
});
 
FormatterParserProvider.registerParser('ZdCurrency', (value: any, { mask = {} }: any = {}) => {
	let maskDef = mask;
 
	if (Accessor.isAccessorDefinition(maskDef)) {
		const [controller, accessor] = Accessor.getAccessor(maskDef);
		maskDef = Loader.getInstance(controller)[accessor];
	}
	let maskValue = typeof maskDef === 'function' ? maskDef(AutoNumeric.format(value)) : maskDef;
	maskValue = { ...Config.masks.numberMask, ...Config.masks.currencyMask, ...maskValue };
	return value === '' ? null : AutoNumeric.unformat(value, maskValue);
});
 
InputFactory.register('ZdCurrency', Currency);