Skip to content

Commit

Permalink
Release 1.0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
amarkes committed Mar 6, 2018
1 parent 015a71a commit 814b0b0
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 31 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import { BrMaskerModule } from 'brmasker-ionic-3';
phone: boolean;
person: boolean;
percent:boolean;
type: 'alfa' | 'num' | 'all'
}
```

Expand All @@ -73,6 +74,32 @@ import { BrMaskerModule } from 'brmasker-ionic-3';
| phone | boolean | Optional |
| person | boolean | Optional |
| percent | boolean | Optional |
| type | string | Optional default 'all' |


### Exemple use of alphanumeric characters

```html
<ion-item>
<ion-input type="text" name="cpf" placeholder="CPF/CNPJ" [brmasker]="{mask:'00/00/0000', len:10, type:'alfa'}"></ion-input>
</ion-item>
```

### Exemple use of number characters

```html
<ion-item>
<ion-input type="text" name="cpf" placeholder="CPF/CNPJ" [brmasker]="{mask:'00/00/0000', len:10, type:'num'}"></ion-input>
</ion-item>
```

### Exemple use of all characters

```html
<ion-item>
<ion-input type="text" name="cpf" placeholder="CPF/CNPJ" [brmasker]="{mask:'00/00/0000', len:10, type:'all'}"></ion-input>
</ion-item>
```

### Exemple for CPF/CNPJ `999.999.999-99` / `99.999.999/9999-99`

Expand Down Expand Up @@ -184,6 +211,10 @@ npm publish

# Changelog

### v1.0.8

- Fix mask type caracter

### v1.0.7

- Fix mask caracter `:`
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "brmasker-ionic-3",
"version": "1.0.7",
"version": "1.0.8",
"description": "mascara para inputs",
"main": "./dist/index.js",
"typings": "./dist/index.d.ts",
Expand Down
82 changes: 52 additions & 30 deletions src/directives/brmasker-ionic-3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export class BrModel {
phone: boolean;
money: boolean;
percent:boolean;
type: 'alfa' | 'num' | 'all' = 'alfa';
}

@Directive({
Expand Down Expand Up @@ -49,45 +50,65 @@ export class BrMaskerIonic3 implements OnInit, ControlValueAccessor {
}

ngOnInit(): void {
if (!this.brmasker.type) {
this.brmasker.type = 'all';
}
}

writeValue(fn: any): void {
this._renderer.setElementProperty(this._elementRef.nativeElement, 'value', fn);
}

registerOnChange(fn: any): void {
this._renderer.setElementProperty(this._elementRef.nativeElement, 'value', fn);
return;
}

registerOnTouched(fn: any): void {
this._renderer.setElementProperty(this._elementRef.nativeElement, 'value', fn);
registerOnTouched(fn: any): void {
return;
}

setDisabledState(isDisabled: boolean): void {
if (isDisabled) {
this._renderer.setElementAttribute(this._elementRef.nativeElement, 'disabled', 'true');
} else {
this._renderer.setElementAttribute(this._elementRef.nativeElement, 'disabled', 'false');
}
}

returnValue(value: string): any {
if (!this.brmasker.mask) { this.brmasker.mask = ''; }
if (value) {
let v = value;
if (this.brmasker.type == 'alfa') {
v = v.replace(/\d/gi,'');
}
if (this.brmasker.type == 'num') {
v = v.replace(/\D/gi,'');
}

if (this.brmasker.money) {
return this.moneyMask(this.onInput(value));
return this.moneyMask(this.onInput(v));
}
if (this.brmasker.phone) {
return this.phoneMask(value);
return this.phoneMask(v);
}
if (this.brmasker.person) {
return this.peapollMask(value);
return this.peapollMask(v);
}
if (this.brmasker.percent) {
return this.percentMask(value)
return this.percentMask(v)
}
return this.onInput(value);
return this.onInput(v);
} else {
return '';
}
}

private percentMask(v:any):void {
let tmp = v;
tmp = tmp.replace(/%/,'');
tmp = tmp.replace(/([0-9]{0})$/g, '%$1');
tmp = tmp.replace(/\D/gi,'');
tmp = tmp.replace(/%/gi,'');
tmp = tmp.replace(/([0-9]{0})$/gi, '%$1');
return tmp;
}

Expand All @@ -96,17 +117,17 @@ export class BrMaskerIonic3 implements OnInit, ControlValueAccessor {
if (n.length > 14) {
this.brmasker.len = 15;
this.brmasker.mask = '(99) 99999-9999';
n = n.replace(/\D/g,'');
n = n.replace(/(\d{2})(\d)/,'$1 $2');
n = n.replace(/(\d{5})(\d)/,'$1-$2');
n = n.replace(/(\d{4})(\d)/,'$1$2');
n = n.replace(/\D/gi,'');
n = n.replace(/(\d{2})(\d)/gi,'$1 $2');
n = n.replace(/(\d{5})(\d)/gi,'$1-$2');
n = n.replace(/(\d{4})(\d)/gi,'$1$2');
} else {
this.brmasker.len = 14;
this.brmasker.mask = '(99) 9999-9999';
n = n.replace(/\D/g,'');
n = n.replace(/(\d{2})(\d)/,'$1 $2');
n = n.replace(/(\d{4})(\d)/,'$1-$2');
n = n.replace(/(\d{4})(\d)/,'$1$2');
n = n.replace(/\D/gi,'');
n = n.replace(/(\d{2})(\d)/gi,'$1 $2');
n = n.replace(/(\d{4})(\d)/gi,'$1-$2');
n = n.replace(/(\d{4})(\d)/gi,'$1$2');
}
return this.onInput(n);
}
Expand All @@ -116,26 +137,27 @@ export class BrMaskerIonic3 implements OnInit, ControlValueAccessor {
if (n.length > 14) {
this.brmasker.len = 18;
this.brmasker.mask = '99.999.999/9999-99';
n = n.replace(/\D/g,'');
n = n.replace(/(\d{2})(\d)/,'$1.$2');
n = n.replace(/(\d{3})(\d)/,'$1.$2');
n = n.replace(/(\d{3})(\d)/,'$1/$2');
n = n.replace(/(\d{4})(\d{1,4})$/,'$1-$2');
n = n.replace(/(\d{2})(\d{1,2})$/,'$1$2');
n = n.replace(/\D/gi,'');
n = n.replace(/(\d{2})(\d)/gi,'$1.$2');
n = n.replace(/(\d{3})(\d)/gi,'$1.$2');
n = n.replace(/(\d{3})(\d)/gi,'$1/$2');
n = n.replace(/(\d{4})(\d{1,4})$/gi,'$1-$2');
n = n.replace(/(\d{2})(\d{1,2})$/gi,'$1$2');
} else {
this.brmasker.len = 14;
this.brmasker.mask = '999.999.999-99';
n = n.replace(/\D/g,'');
n = n.replace(/(\d{3})(\d)/,'$1.$2');
n = n.replace(/(\d{3})(\d)/,'$1.$2');
n = n.replace(/(\d{3})(\d{1,2})$/,'$1-$2');
n = n.replace(/\D/gi,'');
n = n.replace(/(\d{3})(\d)/gi,'$1.$2');
n = n.replace(/(\d{3})(\d)/gi,'$1.$2');
n = n.replace(/(\d{3})(\d{1,2})$/gi,'$1-$2');
}
return this.onInput(n);
}

private moneyMask(v: any): string {
let tmp = v;
tmp = tmp.replace(/([0-9]{2})$/g, ',$1');
tmp = tmp.replace(/\D/gi,'');
tmp = tmp.replace(/([0-9]{2})$/gi, ',$1');
return tmp;
}

Expand All @@ -150,7 +172,7 @@ export class BrMaskerIonic3 implements OnInit, ControlValueAccessor {
private formatField(campo: string, Mascara: string, tamanho: number): any {
if (!tamanho) { tamanho = 99999999999; }
let boleanoMascara;
const exp = /\-|\.|\/|\(|\)|\,|\*|\+|\@|\#|\$|\&|\%|\:| /g;
const exp = /\-|\.|\/|\(|\)|\,|\*|\+|\@|\#|\$|\&|\%|\:| /gi;
const campoSoNumeros = campo.toString().replace(exp, '');
let posicaoCampo = 0;
let NovoValorCampo = '';
Expand Down

0 comments on commit 814b0b0

Please sign in to comment.