Skip to content

Commit ebea587

Browse files
committed
add money mask
Signed-off-by: danieloprado <[email protected]>
1 parent 627f0c2 commit ebea587

File tree

10 files changed

+136
-77
lines changed

10 files changed

+136
-77
lines changed

lang/pt-br/masks.ts

Lines changed: 0 additions & 75 deletions
This file was deleted.

lang/pt-br/masks/cnpj.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { IMaskFunction } from '../../../mask';
2+
3+
const cnpj: IMaskFunction = {
4+
apply: (value: string) => {
5+
if (!value) return '';
6+
7+
const regexp = /^(\d{0,2})(\d{0,3})(\d{0,3})(\d{0,4})(\d{0,2}).*/;
8+
const result = '$1.$2.$3/$4-$5';
9+
10+
return value
11+
.replace(regexp, result)
12+
.replace(/[-.\\]$/, '')
13+
.replace(/[-.\\]$/, '')
14+
.replace(/[-.\\]$/, '');
15+
},
16+
clean: (value: string) => value.replace(/\D/gi, '').substr(0, 14)
17+
};
18+
19+
export default cnpj;

lang/pt-br/masks/cpf.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { IMaskFunction } from '../../../mask';
2+
3+
const cpf: IMaskFunction = {
4+
apply: (value: string) => {
5+
if (!value) return '';
6+
7+
const regexp = /^(\d{0,3})(\d{0,3})(\d{0,3})(\d{0,2}).*/;
8+
const result = '$1.$2.$3-$4';
9+
10+
return value
11+
.replace(regexp, result)
12+
.replace(/[-.\\]$/, '')
13+
.replace(/[-.\\]$/, '')
14+
.replace(/[-.\\]$/, '');
15+
},
16+
clean: (value: string) => value.replace(/\D/gi, '').substr(0, 11)
17+
};
18+
19+
export default cpf;

lang/pt-br/masks/document.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { IMaskFunction } from '../../../mask';
2+
import cnpj from './cnpj';
3+
import cpf from './cpf';
4+
5+
const document: IMaskFunction = {
6+
apply: (value: string) => {
7+
if (!value) return '';
8+
return value.length > 11 ? cnpj.apply(value) : cpf.apply(value);
9+
},
10+
clean: (value: string) => {
11+
if (!value) return '';
12+
return value.length > 11 ? cnpj.clean(value) : cpf.clean(value);
13+
}
14+
};
15+
16+
export default document;

lang/pt-br/masks/index.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { IMask } from '../../../mask';
2+
import cnpj from './cnpj';
3+
import cpf from './cpf';
4+
import document from './document';
5+
import money from './money';
6+
import phone from './phone';
7+
import zipcode from './zipcode';
8+
9+
const masks: IMask[] = [
10+
{ name: 'zipcode', ...zipcode },
11+
{ name: 'phone', ...phone },
12+
{ name: 'document', ...document },
13+
{ name: 'cpf', ...cpf },
14+
{ name: 'cnpj', ...cnpj },
15+
{ name: 'money', ...money }
16+
];
17+
export default masks;

lang/pt-br/masks/money.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import padStart = require('lodash/padStart');
2+
3+
import { IMaskFunction } from '../../../mask';
4+
5+
const money: IMaskFunction = {
6+
apply: (value: number) => {
7+
if (value === null || value === undefined) return '';
8+
return new Intl.NumberFormat('pt-BR', { style: 'currency', currency: 'BRL' }).format(value);
9+
},
10+
clean: value => {
11+
value = (value || '').replace(/[^\d\,]/gi, '');
12+
13+
if (!value.includes(',')) {
14+
value = '0,' + padStart(value, 2, '0');
15+
}
16+
17+
const [, cents] = value.split(',');
18+
if (cents && cents.length != 2) {
19+
value = value.replace(',', '').replace(/(\d+)?(\d{2})/gi, '$1,$2').replace(/^\,/gi, '0,');
20+
}
21+
22+
return parseFloat(value.replace(/\./gi, '').replace(',', '.'));
23+
}
24+
};
25+
26+
export default money;

lang/pt-br/masks/phone.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { IMaskFunction } from '../../../mask';
2+
3+
const phone: IMaskFunction = {
4+
apply: (value: string) => {
5+
if (!value) return '';
6+
7+
const regexp = value.length > 10 ?
8+
/^(\d{0,2})(\d{0,5})(\d{0,4}).*/ :
9+
/^(\d{0,2})(\d{0,4})(\d{0,4}).*/;
10+
11+
const result = value.length > 2 ?
12+
'($1) $2-$3' : '($1$2$3';
13+
14+
return value.replace(regexp, result).replace(/-$/, '');
15+
},
16+
clean: (value: string) => value.replace(/\D/gi, '').substr(0, 11)
17+
};
18+
19+
export default phone;

lang/pt-br/masks/zipcode.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { IMaskFunction } from '../../../mask';
2+
3+
const zipcode: IMaskFunction = {
4+
apply: (value: string) => {
5+
if (!value) return '';
6+
return value.replace(/^(\d{0,5})(\d{0,3}).*/, '$1-$2').replace(/-$/, '');
7+
},
8+
clean: (value: string) => value.replace(/\D/gi, '').substr(0, 8)
9+
};
10+
11+
export default zipcode;

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"validation",
1010
"material"
1111
],
12-
"version": "1.4.7",
12+
"version": "1.4.8",
1313
"main": "./index.js",
1414
"types": "./index.d.ts",
1515
"license": "MIT",
@@ -36,6 +36,7 @@
3636
}
3737
},
3838
"dependencies": {
39+
"lodash": ">=4.0.0",
3940
"react": ">=16.3.0",
4041
"tslib": ">=1.9.3",
4142
"validatorjs": "^3.15.1"
@@ -44,6 +45,7 @@
4445
"react": ">=16.3.0"
4546
},
4647
"devDependencies": {
48+
"@types/lodash": "4.14.120",
4749
"@types/node": "10.12.18",
4850
"@types/react": "16.7.20",
4951
"@types/validatorjs": "3.15.0",

yarn.lock

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
# yarn lockfile v1
33

44

5+
6+
version "4.14.120"
7+
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.120.tgz#cf265d06f6c7a710db087ed07523ab8c1a24047b"
8+
integrity sha512-jQ21kQ120mo+IrDs1nFNVm/AsdFxIx2+vZ347DbogHJPd/JzKNMOqU6HCYin1W6v8l5R9XSO2/e9cxmn7HAnVw==
9+
510
611
version "10.12.18"
712
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.12.18.tgz#1d3ca764718915584fcd9f6344621b7672665c67"
@@ -472,7 +477,7 @@ locate-path@^3.0.0:
472477
p-locate "^3.0.0"
473478
path-exists "^3.0.0"
474479

475-
lodash@^4.17.10:
480+
lodash@>=4.0.0, lodash@^4.17.10:
476481
version "4.17.11"
477482
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.11.tgz#b39ea6229ef607ecd89e2c8df12536891cac9b8d"
478483
integrity sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==

0 commit comments

Comments
 (0)