Skip to content

Commit 3296456

Browse files
authored
Merge pull request #20 from jesieldotdev/master
Feature: RG GENERATE AND VALIDATE
2 parents 8d0a223 + 38ca971 commit 3296456

File tree

8 files changed

+162
-10
lines changed

8 files changed

+162
-10
lines changed

README.md

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ npm install --save 4devs
3131
# Navigation
3232

3333
- [CPF](#cpf)
34+
- [RG](#rg)
3435
- [Certificates](#certificates)
3536

3637
# Docs:
@@ -61,25 +62,49 @@ return:
6162
}
6263
```
6364

64-
### generate a new CPF
65+
## RG:
66+
67+
RG is one of the classes that 4devs has, this class is able to generate and validate a rg based on the services on the pages [https://www.4devs.com.br/gerador_de_cpf](https://www.4devs.com.br/gerador_de_rg) and [https://www.4devs.com.br/validador_rg](https://www.4devs.com.br/validador_rg)
68+
69+
### validate a RG
6570

6671
```ts
67-
import { CPF } from '4devs';
72+
import { RG } from '4devs';
73+
74+
const { isValid } = await RG.validate({ rg: '30.737.817-2' });
75+
```
76+
77+
parameters:
78+
79+
- `rg` (required) the RG you want to validate
80+
81+
return:
82+
83+
```ts
84+
{
85+
isValid: boolean;
86+
}
87+
```
88+
89+
### generate a new RG
90+
91+
```ts
92+
import { RG } from '4devs';
6893

69-
const { cpf } = await CPF.generate({ isWithDots: true, stateCode: 'BA' });
94+
const { rg } = await RG.generate({ isWithDots: true, stateCode: 'BA' });
7095
```
7196

7297
parameters:
7398

74-
- `isWithDots` (optional) if true the CPF will be generated with dots example: 218.61.715-34 if not generated without example: 60778779564
99+
- `isWithDots` (optional) if true the RG will be generated with dots example: 30.737.817-2 if not generated without example: 60.778.777-0
75100

76-
- `stateCode` (optional) code of the state where the CPF belongs, example: "BA" or "SP"
101+
- `stateCode` (optional) code of the state where the RG belongs, example: "BA" or "SP"
77102

78103
return:
79104

80105
```ts
81106
{
82-
cpf: string;
107+
rg: string;
83108
}
84109
```
85110

__test__/RG.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { RG } from '../lib/index';
2+
3+
interface IRG {
4+
rg: string;
5+
isValid: boolean;
6+
}
7+
8+
const rgMock: IRG = {
9+
rg: '30.737.817-2',
10+
isValid: true,
11+
};
12+
13+
describe('RG lib', () => {
14+
it('should create an instance', () => {
15+
expect(new RG()).toBeTruthy();
16+
});
17+
18+
it('should be valid RG', async () => {
19+
const RGIsValid = await RG.validate({ rg: rgMock.rg });
20+
21+
expect(RGIsValid.isValid).toBe(rgMock.isValid);
22+
});
23+
24+
it('should generate a RG with dots', async () => {
25+
const { rg } = await RG.generate({ isWithDots: true });
26+
27+
expect(rg).toHaveLength(12);
28+
});
29+
30+
it('should generate a RG without dots', async () => {
31+
const { rg } = await RG.generate({ isWithDots: false });
32+
33+
expect(rg).toHaveLength(9);
34+
35+
36+
});
37+
38+
it('should generate a RG with state', async () => {
39+
const { rg } = await RG.generate({ stateCode: 'BA' });
40+
41+
expect(rg).toHaveLength(9);
42+
});
43+
});

examples/index.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
1-
const { Certificates, CPF } = require('../dist');
1+
const { Certificates, CPF, RG } = require('../dist');
22

33
(async () => {
44
{
55
// CPF
66
const { cpf } = await CPF.generate({ isWithDots: true, stateCode: 'BA' }); // 34317074087
77

8-
console.log(cpf);
8+
console.log('CPF: ',cpf);
99

1010
const { isValid } = await CPF.validate({ cpf });
1111

1212
console.log(isValid);
1313
}
14+
{
15+
// RG
16+
const { rg } = await RG.generate({ isWithDots: true, stateCode: 'BA' }); // 34317074087
17+
18+
console.log('RG: ',rg);
19+
20+
const { isValid } = await RG.validate({ rg });
21+
22+
console.log(isValid);
23+
}
1424
{
1525
// Certificates
1626
const { certificate } = await Certificates.generate();
1727

18-
console.log(certificate);
28+
console.log('certificate: ', certificate);
1929

2030
const { isValid } = await Certificates.validate({ certificate });
2131

lib/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Certificates } from './modules/Certificates';
22
import { CPF } from './modules/CPF';
33
import { CNH } from './modules/CNH';
4+
import { RG } from './modules/RG';
45

5-
export { Certificates, CPF, CNH };
6+
export { Certificates, CPF, CNH, RG };

lib/interfaces/IRG.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export interface IRGValid {
2+
isValid: boolean;
3+
}
4+
5+
export interface IRGGenerated {
6+
rg: string;
7+
}
8+

lib/modules/RG/GenerateRG.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { RequestUtil } from '../../utils/RequestUtil';
2+
import { IRGGenerated } from '../../interfaces/IRG';
3+
4+
export class GenerateRG {
5+
requestUtil = new RequestUtil();
6+
async execute({
7+
isWithDots,
8+
stateCode,
9+
}: {
10+
isWithDots?: boolean;
11+
stateCode?: string;
12+
}): Promise<IRGGenerated> {
13+
const { data: rg }: { data: string } = await this.requestUtil.post({
14+
path: '/ferramentas_online.php',
15+
json: {
16+
acao: 'gerar_rg',
17+
pontuacao: isWithDots ? 'S' : 'N',
18+
rg_estado: stateCode || '',
19+
},
20+
});
21+
22+
return {
23+
rg,
24+
};
25+
}
26+
}

lib/modules/RG/ValidateRG.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { RequestUtil } from '../../utils/RequestUtil';
2+
import { IRGValid } from '../../interfaces/IRG';
3+
4+
export class ValidateRG {
5+
requestUtil = new RequestUtil();
6+
async execute({ rg }: { rg: string }): Promise<IRGValid> {
7+
const { data }: { data: string } = await this.requestUtil.post({
8+
path: '/ferramentas_online.php',
9+
json: {
10+
acao: 'validar_rg',
11+
txt_rg: rg,
12+
},
13+
});
14+
const isValid = data.toLowerCase().includes('verdadeiro');
15+
16+
return {
17+
isValid,
18+
};
19+
}
20+
}

lib/modules/RG/index.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { ValidateRG } from './ValidateRG';
2+
import { GenerateRG } from './GenerateRG';
3+
4+
import { IRGValid } from '../../interfaces/IRG';
5+
import { IRGGenerated } from '../../interfaces/IRG';
6+
7+
export class RG {
8+
public static async validate({ rg }: { rg: string }): Promise<IRGValid> {
9+
const validateRG = new ValidateRG();
10+
return await validateRG.execute({ rg });
11+
}
12+
public static async generate(options?: {
13+
isWithDots?: boolean;
14+
stateCode?: string;
15+
}): Promise<IRGGenerated> {
16+
const generateRG = new GenerateRG();
17+
return await generateRG.execute(options ? options : {});
18+
}
19+
}

0 commit comments

Comments
 (0)