UNPKG

mat-currency-format

Version:

This repos is creating a library for angular currency input and also it is showing how to use the library

192 lines (186 loc) 7.13 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@angular/common')) : typeof define === 'function' && define.amd ? define('mat-currency-format', ['exports', '@angular/core', '@angular/common'], factory) : (global = global || self, factory(global['mat-currency-format'] = {}, global.ng.core, global.ng.common)); }(this, (function (exports, core, common) { 'use strict'; /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * The function to reverse the number format * @param {?} val * @param {?} locale * @return {?} */ function reverseFormatNumber(val, locale) { /** @type {?} */ var group = new Intl.NumberFormat(locale).format(1111).replace(/1/g, ''); /** @type {?} */ var decimal = new Intl.NumberFormat(locale).format(1.1).replace(/1/g, ''); /** @type {?} */ var reversedCurrencyVal = val.replace(new RegExp('\\' + group, 'g'), ''); reversedCurrencyVal = reversedCurrencyVal.replace(new RegExp('\\' + decimal, 'g'), '.'); /** @type {?} */ var reversedVal = reversedCurrencyVal.replace(/[^\d.-]/g, ''); return Number.isNaN(reversedVal) ? 0 : reversedVal; } var MatCurrencyFormatDirective = /** @class */ (function () { function MatCurrencyFormatDirective(el, currencyPipe, renderer, locale) { this.el = el; this.currencyPipe = currencyPipe; this.renderer = renderer; this.locale = locale; this.currencyCode = 'USD'; this.allowNegative = false; this.regex = { positiveDecimal: new RegExp(/^\d*[\.|,]?\d*$/g), allDecimal: new RegExp(/^-?\d*[\.|,]?\d*$/g) }; this.specialKeys = ['Backspace', 'Delete', 'ArrowLeft', 'ArrowRight', 'Del']; } /** * @return {?} */ MatCurrencyFormatDirective.prototype.ngAfterViewInit = /** * @return {?} */ function () { this.el.nativeElement.value = this.currencyPipe.transform(this.el.nativeElement.value, this.currencyCode); }; /** * @return {?} */ MatCurrencyFormatDirective.prototype.onFocus = /** * @return {?} */ function () { this.el.nativeElement.value = this.el.nativeElement.value.replace(/[^\d.,-]/g, ''); this.el.nativeElement.select(); }; /** * @return {?} */ MatCurrencyFormatDirective.prototype.onFocusout = /** * @return {?} */ function () { /** @type {?} */ var val = this.el.nativeElement.value; if (val.length === 0 || (val.length === 1 && [',', '.', ''].indexOf(val[0]) > -1)) { val = '0'; } this.el.nativeElement.value = this.currencyPipe.transform(val, this.currencyCode); }; /** * @param {?} event * @return {?} */ MatCurrencyFormatDirective.prototype.onKeyDown = /** * @param {?} event * @return {?} */ function (event) { if (this.specialKeys.indexOf(event.key) !== -1) { return; } /** @type {?} */ var matches = this.allowNegative ? event.key.match(this.regex.allDecimal) : event.key.match(this.regex.positiveDecimal); if (!matches) { event.preventDefault(); } }; /** * @param {?} $event * @return {?} */ MatCurrencyFormatDirective.prototype.onBlur = /** * @param {?} $event * @return {?} */ function ($event) { /** @type {?} */ var amount = reverseFormatNumber(this.el.nativeElement.value, this.locale).replace(/^[^0-9]{2}..*/g, ''); this.el.nativeElement.value = amount; }; MatCurrencyFormatDirective.decorators = [ { type: core.Directive, args: [{ selector: '[mvndrMatCurrencyFormat]', providers: [common.CurrencyPipe] },] } ]; /** @nocollapse */ MatCurrencyFormatDirective.ctorParameters = function () { return [ { type: core.ElementRef }, { type: common.CurrencyPipe }, { type: core.Renderer2 }, { type: String, decorators: [{ type: core.Inject, args: [core.LOCALE_ID,] }] } ]; }; MatCurrencyFormatDirective.propDecorators = { currencyCode: [{ type: core.Input }], allowNegative: [{ type: core.Input }], onFocus: [{ type: core.HostListener, args: ['focus',] }], onFocusout: [{ type: core.HostListener, args: ['focusout',] }], onKeyDown: [{ type: core.HostListener, args: ['keydown', ['$event'],] }], onBlur: [{ type: core.HostListener, args: ['blur',] }] }; return MatCurrencyFormatDirective; }()); if (false) { /** @type {?} */ MatCurrencyFormatDirective.prototype.currencyCode; /** @type {?} */ MatCurrencyFormatDirective.prototype.allowNegative; /** * @type {?} * @private */ MatCurrencyFormatDirective.prototype.regex; /** * @type {?} * @private */ MatCurrencyFormatDirective.prototype.specialKeys; /** * @type {?} * @private */ MatCurrencyFormatDirective.prototype.el; /** * @type {?} * @private */ MatCurrencyFormatDirective.prototype.currencyPipe; /** * @type {?} * @private */ MatCurrencyFormatDirective.prototype.renderer; /** * @type {?} * @private */ MatCurrencyFormatDirective.prototype.locale; } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ var MatCurrencyFormatModule = /** @class */ (function () { function MatCurrencyFormatModule() { } MatCurrencyFormatModule.decorators = [ { type: core.NgModule, args: [{ declarations: [MatCurrencyFormatDirective], imports: [], exports: [MatCurrencyFormatDirective] },] } ]; return MatCurrencyFormatModule; }()); exports.MatCurrencyFormatDirective = MatCurrencyFormatDirective; exports.MatCurrencyFormatModule = MatCurrencyFormatModule; exports.reverseFormatNumber = reverseFormatNumber; Object.defineProperty(exports, '__esModule', { value: true }); }))); //# sourceMappingURL=mat-currency-format.umd.js.map