diff --git a/README.md b/README.md index 08177af..42df95e 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ npm install --save 4devs # Navigation - [CPF](#cpf) +- [RG](#rg) - [Certificates](#certificates) # Docs: @@ -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; } ``` diff --git a/__test__/RG.test.ts b/__test__/RG.test.ts new file mode 100644 index 0000000..7fc3064 --- /dev/null +++ b/__test__/RG.test.ts @@ -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); + }); +}); diff --git a/examples/index.js b/examples/index.js index ddcb590..a48775b 100644 --- a/examples/index.js +++ b/examples/index.js @@ -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 }); diff --git a/lib/index.ts b/lib/index.ts index 6acda92..71c8cc5 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -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 }; diff --git a/lib/interfaces/IRG.ts b/lib/interfaces/IRG.ts new file mode 100644 index 0000000..d86c4f6 --- /dev/null +++ b/lib/interfaces/IRG.ts @@ -0,0 +1,8 @@ +export interface IRGValid { + isValid: boolean; + } + + export interface IRGGenerated { + rg: string; + } + \ No newline at end of file diff --git a/lib/modules/RG/GenerateRG.ts b/lib/modules/RG/GenerateRG.ts new file mode 100644 index 0000000..61c0a48 --- /dev/null +++ b/lib/modules/RG/GenerateRG.ts @@ -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 { + 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, + }; + } +} diff --git a/lib/modules/RG/ValidateRG.ts b/lib/modules/RG/ValidateRG.ts new file mode 100644 index 0000000..a14774d --- /dev/null +++ b/lib/modules/RG/ValidateRG.ts @@ -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 { + 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, + }; + } +} diff --git a/lib/modules/RG/index.ts b/lib/modules/RG/index.ts new file mode 100644 index 0000000..960b099 --- /dev/null +++ b/lib/modules/RG/index.ts @@ -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 { + const validateRG = new ValidateRG(); + return await validateRG.execute({ rg }); + } + public static async generate(options?: { + isWithDots?: boolean; + stateCode?: string; + }): Promise { + const generateRG = new GenerateRG(); + return await generateRG.execute(options ? options : {}); + } +}