UNPKG

@ioniczoo/bird-format-pipe

Version:

Format Pipe for Ionic

89 lines 2.96 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; import { NgModule, Pipe } from '@angular/core'; /** * Generated class for the FormatPipe pipe. * * See https://angular.io/api/core/Pipe for more info on Angular Pipes. * * @example * ``` html * {{ cpf | format:'999.999.999-99' }} * {{ tel | format:'(99) 9 9999-9999' }} * ``` */ var FormatPipe = /** @class */ (function () { function FormatPipe() { } /** * Format value. * * @param {any} value Waits to receive string or integer * @param {string} pattern Waits to receive a pattern in string * @return {[type]} returns a formatted string. */ FormatPipe.prototype.transform = function (value, pattern) { return this.format(String(value), pattern); }; /** * Format value * @param {string} value virgin value. * @param {string} pattern standard format. * @return {string} returns a formatted string. */ FormatPipe.prototype.format = function (value, pattern) { var data = '', cont = 0, plus = ''; for (var i = 0; i < pattern.length; i++) { if (parseInt(pattern[i])) { cont++; } } /** * Addes zeros to the left to complete the characters in the format. */ if (cont > value.length) { for (var i = 0; i < cont - value.length; i++) { plus = plus.concat('0'); } value = plus + value; } cont = 0; /** * Replaces pattern numbers with values. */ for (var i = 0; i < pattern.length; i++) { if (parseInt(pattern[i]) && cont < value.length) { data = data.concat(value[cont]); cont++; } else { data = data.concat(pattern[i]); } } return data; }; FormatPipe = __decorate([ Pipe({ name: 'format' }) ], FormatPipe); return FormatPipe; }()); export { FormatPipe }; var FormatPipeModule = /** @class */ (function () { function FormatPipeModule() { } FormatPipeModule = __decorate([ NgModule({ declarations: [FormatPipe], exports: [FormatPipe] }) ], FormatPipeModule); return FormatPipeModule; }()); export { FormatPipeModule }; //# sourceMappingURL=index.js.map