Skip to content

Commit

Permalink
Merge pull request #20 from jesieldotdev/master
Browse files Browse the repository at this point in the history
Feature: RG GENERATE AND VALIDATE
  • Loading branch information
Theryston authored Nov 13, 2024
2 parents 8d0a223 + 38ca971 commit 3296456
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 10 deletions.
37 changes: 31 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ npm install --save 4devs
# Navigation

- [CPF](#cpf)
- [RG](#rg)
- [Certificates](#certificates)

# Docs:
Expand Down Expand Up @@ -61,25 +62,49 @@ return:
}
```

### generate a new CPF
## RG:

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)

### validate a RG

```ts
import { CPF } from '4devs';
import { RG } from '4devs';

const { isValid } = await RG.validate({ rg: '30.737.817-2' });
```

parameters:

- `rg` (required) the RG you want to validate

return:

```ts
{
isValid: boolean;
}
```

### generate a new RG

```ts
import { RG } from '4devs';

const { cpf } = await CPF.generate({ isWithDots: true, stateCode: 'BA' });
const { rg } = await RG.generate({ isWithDots: true, stateCode: 'BA' });
```

parameters:

- `isWithDots` (optional) if true the CPF will be generated with dots example: 218.61.715-34 if not generated without example: 60778779564
- `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

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

return:

```ts
{
cpf: string;
rg: string;
}
```

Expand Down
43 changes: 43 additions & 0 deletions __test__/RG.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { RG } from '../lib/index';

interface IRG {
rg: string;
isValid: boolean;
}

const rgMock: IRG = {
rg: '30.737.817-2',
isValid: true,
};

describe('RG lib', () => {
it('should create an instance', () => {
expect(new RG()).toBeTruthy();
});

it('should be valid RG', async () => {
const RGIsValid = await RG.validate({ rg: rgMock.rg });

expect(RGIsValid.isValid).toBe(rgMock.isValid);
});

it('should generate a RG with dots', async () => {
const { rg } = await RG.generate({ isWithDots: true });

expect(rg).toHaveLength(12);
});

it('should generate a RG without dots', async () => {
const { rg } = await RG.generate({ isWithDots: false });

expect(rg).toHaveLength(9);


});

it('should generate a RG with state', async () => {
const { rg } = await RG.generate({ stateCode: 'BA' });

expect(rg).toHaveLength(9);
});
});
16 changes: 13 additions & 3 deletions examples/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
const { Certificates, CPF } = require('../dist');
const { Certificates, CPF, RG } = require('../dist');

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

console.log(cpf);
console.log('CPF: ',cpf);

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

console.log(isValid);
}
{
// RG
const { rg } = await RG.generate({ isWithDots: true, stateCode: 'BA' }); // 34317074087

console.log('RG: ',rg);

const { isValid } = await RG.validate({ rg });

console.log(isValid);
}
{
// Certificates
const { certificate } = await Certificates.generate();

console.log(certificate);
console.log('certificate: ', certificate);

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

Expand Down
3 changes: 2 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Certificates } from './modules/Certificates';
import { CPF } from './modules/CPF';
import { CNH } from './modules/CNH';
import { RG } from './modules/RG';

export { Certificates, CPF, CNH };
export { Certificates, CPF, CNH, RG };
8 changes: 8 additions & 0 deletions lib/interfaces/IRG.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface IRGValid {
isValid: boolean;
}

export interface IRGGenerated {
rg: string;
}

26 changes: 26 additions & 0 deletions lib/modules/RG/GenerateRG.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { RequestUtil } from '../../utils/RequestUtil';
import { IRGGenerated } from '../../interfaces/IRG';

export class GenerateRG {
requestUtil = new RequestUtil();
async execute({
isWithDots,
stateCode,
}: {
isWithDots?: boolean;
stateCode?: string;
}): Promise<IRGGenerated> {
const { data: rg }: { data: string } = await this.requestUtil.post({
path: '/ferramentas_online.php',
json: {
acao: 'gerar_rg',
pontuacao: isWithDots ? 'S' : 'N',
rg_estado: stateCode || '',
},
});

return {
rg,
};
}
}
20 changes: 20 additions & 0 deletions lib/modules/RG/ValidateRG.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { RequestUtil } from '../../utils/RequestUtil';
import { IRGValid } from '../../interfaces/IRG';

export class ValidateRG {
requestUtil = new RequestUtil();
async execute({ rg }: { rg: string }): Promise<IRGValid> {
const { data }: { data: string } = await this.requestUtil.post({
path: '/ferramentas_online.php',
json: {
acao: 'validar_rg',
txt_rg: rg,
},
});
const isValid = data.toLowerCase().includes('verdadeiro');

return {
isValid,
};
}
}
19 changes: 19 additions & 0 deletions lib/modules/RG/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ValidateRG } from './ValidateRG';
import { GenerateRG } from './GenerateRG';

import { IRGValid } from '../../interfaces/IRG';
import { IRGGenerated } from '../../interfaces/IRG';

export class RG {
public static async validate({ rg }: { rg: string }): Promise<IRGValid> {
const validateRG = new ValidateRG();
return await validateRG.execute({ rg });
}
public static async generate(options?: {
isWithDots?: boolean;
stateCode?: string;
}): Promise<IRGGenerated> {
const generateRG = new GenerateRG();
return await generateRG.execute(options ? options : {});
}
}

0 comments on commit 3296456

Please sign in to comment.