ngx-rutificador
Version:
196 lines (187 loc) • 9.28 kB
JavaScript
import * as i0 from '@angular/core';
import { Injectable, EventEmitter, Directive, Output, Pipe, Component, Input, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
class NgxRutificadorService {
constructor() { }
validateRut(params) {
let value = this.cleanRut(params);
let sum = 0;
let rut = value.substring(0, value.length - 1);
let dv = value.substring(value.length - 1);
if (dv.toUpperCase() == 'K') {
dv = '10';
}
let resultDv;
let arrRut = rut.split('');
if (arrRut.length < 7) {
return false;
}
else {
let b = 0;
let c = 0;
let arrResult = [];
var arrReverse = arrRut.reverse();
arrReverse.map((value, index) => {
if (index > 5) {
arrResult[b] = Number(value) * (2 + c);
c++;
}
else {
arrResult[b] = Number(value) * (2 + b);
}
b++;
});
arrResult.map((value) => {
sum += value;
});
resultDv = 11 - (sum - Math.trunc(sum / 11) * 11);
if (resultDv > 9 && dv == "0") {
return true;
}
if (resultDv == Number(dv)) {
return true;
}
else {
return false;
}
}
}
formatRut(value) {
const rut = this.cleanRut(value);
if (rut.length <= 1) {
return rut;
}
let result = `${rut.slice(-4, -1)}-${rut.substr(rut.length - 1)}`;
for (let i = 4; i < rut.length; i += 3) {
result = `${rut.slice(-3 - i, -i)}.${result}`;
}
return result;
}
cleanRut(num) {
let value = num.toString().split('.').join('').split('-').join('');
return value;
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.4", ngImport: i0, type: NgxRutificadorService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.3.4", ngImport: i0, type: NgxRutificadorService, providedIn: 'root' }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.4", ngImport: i0, type: NgxRutificadorService, decorators: [{
type: Injectable,
args: [{
providedIn: 'root'
}]
}], ctorParameters: () => [] });
class NgxRutificadorDirective {
constructor(ngxRutificadorService) {
this.ngxRutificadorService = ngxRutificadorService;
this.valueChange = new EventEmitter();
}
onFocus(ev) {
const htmlInputElement = ev.target;
htmlInputElement.value = this.ngxRutificadorService.cleanRut(htmlInputElement.value.trim());
this.valueChange.emit(htmlInputElement.value);
}
onBlur(ev) {
const htmlInputElement = ev.target;
htmlInputElement.value = this.ngxRutificadorService.formatRut(htmlInputElement.value.trim());
this.valueChange.emit(htmlInputElement.value);
}
onChange(ev) {
const htmlInputElement = ev.target;
htmlInputElement.value = this.ngxRutificadorService.cleanRut(htmlInputElement.value.trim());
this.valueChange.emit(htmlInputElement.value);
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.4", ngImport: i0, type: NgxRutificadorDirective, deps: [{ token: NgxRutificadorService }], target: i0.ɵɵFactoryTarget.Directive }); }
static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "17.3.4", type: NgxRutificadorDirective, isStandalone: true, selector: "[ngxRutificador]", outputs: { valueChange: "valueChange" }, host: { listeners: { "input": "onChange($event)", "blur": "onBlur($event)", "focus": "onFocus($event)" } }, ngImport: i0 }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.4", ngImport: i0, type: NgxRutificadorDirective, decorators: [{
type: Directive,
args: [{
selector: '[ngxRutificador]',
standalone: true,
host: {
'(input)': 'onChange($event)',
'(blur)': 'onBlur($event)',
'(focus)': 'onFocus($event)',
}
}]
}], ctorParameters: () => [{ type: NgxRutificadorService }], propDecorators: { valueChange: [{
type: Output
}] } });
class NgxRutificadorPipe {
constructor(ngxRutificadorService) {
this.ngxRutificadorService = ngxRutificadorService;
}
transform(value) {
return this.ngxRutificadorService.formatRut(value);
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.4", ngImport: i0, type: NgxRutificadorPipe, deps: [{ token: NgxRutificadorService }], target: i0.ɵɵFactoryTarget.Pipe }); }
static { this.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "17.3.4", ngImport: i0, type: NgxRutificadorPipe, isStandalone: true, name: "fotmatRut" }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.4", ngImport: i0, type: NgxRutificadorPipe, decorators: [{
type: Pipe,
args: [{
name: 'fotmatRut',
standalone: true,
}]
}], ctorParameters: () => [{ type: NgxRutificadorService }] });
class NgxRutificadorComponent {
constructor(ngxRutificadorService) {
this.ngxRutificadorService = ngxRutificadorService;
this.class = '';
this.style = '';
this.placeholder = '';
this.isValidRut = new EventEmitter(false);
}
validateRut(ev) {
const isValid = this.ngxRutificadorService.validateRut(ev.target.value);
this.isValidRut.emit(isValid);
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.4", ngImport: i0, type: NgxRutificadorComponent, deps: [{ token: NgxRutificadorService }], target: i0.ɵɵFactoryTarget.Component }); }
static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "17.3.4", type: NgxRutificadorComponent, isStandalone: true, selector: "input-rut", inputs: { class: "class", style: "style", placeholder: "placeholder" }, outputs: { isValidRut: "isValidRut" }, ngImport: i0, template: "<input type=\"text\" [class]=\"class\" [style]=\"style\" (keyup)=\"validateRut($event)\" [placeholder]=\"placeholder\" ngxRutificador>", styles: [""], dependencies: [{ kind: "directive", type: NgxRutificadorDirective, selector: "[ngxRutificador]", outputs: ["valueChange"] }] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.4", ngImport: i0, type: NgxRutificadorComponent, decorators: [{
type: Component,
args: [{ selector: 'input-rut', standalone: true, imports: [NgxRutificadorDirective], template: "<input type=\"text\" [class]=\"class\" [style]=\"style\" (keyup)=\"validateRut($event)\" [placeholder]=\"placeholder\" ngxRutificador>" }]
}], ctorParameters: () => [{ type: NgxRutificadorService }], propDecorators: { class: [{
type: Input
}], style: [{
type: Input
}], placeholder: [{
type: Input
}], isValidRut: [{
type: Output
}] } });
class NgxRutificadorModule {
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.4", ngImport: i0, type: NgxRutificadorModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "17.3.4", ngImport: i0, type: NgxRutificadorModule, imports: [NgxRutificadorComponent,
NgxRutificadorDirective,
NgxRutificadorPipe,
CommonModule], exports: [NgxRutificadorComponent,
NgxRutificadorDirective,
NgxRutificadorPipe] }); }
static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "17.3.4", ngImport: i0, type: NgxRutificadorModule, imports: [CommonModule] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.4", ngImport: i0, type: NgxRutificadorModule, decorators: [{
type: NgModule,
args: [{
imports: [
NgxRutificadorComponent,
NgxRutificadorDirective,
NgxRutificadorPipe,
CommonModule,
],
exports: [
NgxRutificadorComponent,
NgxRutificadorDirective,
NgxRutificadorPipe,
]
}]
}] });
/*
* Public API Surface of ngx-rutificador
*/
/**
* Generated bundle index. Do not edit.
*/
export { NgxRutificadorComponent, NgxRutificadorDirective, NgxRutificadorModule, NgxRutificadorPipe };
//# sourceMappingURL=ngx-rutificador.mjs.map