pix-utils-js 1.0.6 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,25 +1,25 @@
1
- name: Node.js CI
2
-
3
- on:
4
- push:
5
- branches: [ "main" ]
6
- pull_request:
7
- branches: [ "main" ]
8
-
9
- jobs:
10
- build:
11
-
12
- runs-on: ubuntu-latest
13
-
14
- strategy:
15
- matrix:
16
- node-version: [18.x]
17
-
18
- steps:
19
- - uses: actions/checkout@v3
20
- - name: Use Node.js ${{ matrix.node-version }}
21
- uses: actions/setup-node@v3
22
- with:
23
- node-version: ${{ matrix.node-version }}
24
- - run: yarn
25
- - run: yarn test
1
+ name: Node.js CI
2
+
3
+ on:
4
+ push:
5
+ branches: [ "main" ]
6
+ pull_request:
7
+ branches: [ "main" ]
8
+
9
+ jobs:
10
+ build:
11
+
12
+ runs-on: ubuntu-latest
13
+
14
+ strategy:
15
+ matrix:
16
+ node-version: [18.x]
17
+
18
+ steps:
19
+ - uses: actions/checkout@v3
20
+ - name: Use Node.js ${{ matrix.node-version }}
21
+ uses: actions/setup-node@v3
22
+ with:
23
+ node-version: ${{ matrix.node-version }}
24
+ - run: yarn
25
+ - run: yarn test
package/README.md CHANGED
@@ -1,71 +1,71 @@
1
-
2
- # 💸 pix-utils-js
3
-
4
- ![](https://img.shields.io/npm/dw/pix-utils-js)
5
- ![](https://img.shields.io/npm/v/pix-utils-js)
6
- ![](https://img.shields.io/github/contributors/paulohenriquesn/pix-utils-js)
7
- ![](https://img.shields.io/npm/l/pix-utils-js)
8
-
9
- Uma pequena biblioteca que irá te ajudar a lidar com chaves pix
10
-
11
- Demo https://pix-key-js-demo.vercel.app/
12
-
13
-
14
- ## Instalação
15
-
16
- Instale pix-utils-js com npm ou yarn
17
-
18
- ```bash
19
- npm install pix-utils-js
20
- yarn add pix-utils-js
21
- ```
22
-
23
- ## Documentação
24
-
25
- #### Identifique uma chave pix
26
-
27
- ```js
28
- import { identify } from 'pix-utils-js'
29
- const { pix, type } = identify({pix: 'test@gmail.com'}) // {pix: 'test@gmail.com', type: 'email'}
30
- ```
31
-
32
- | Função | Parametro | Retorno |
33
- | :---------- | :--------- | :---------------------------------- |
34
- | `identify` | `input: {pix: string}` | Pix `{ pix: string, type: 'email' \| 'cpf' \| 'cnpj' \| 'random' \|'phone' }` |
35
-
36
- #### Validar uma chave pix
37
-
38
- ```js
39
- import { validate } from 'pix-utils-js'
40
- console.log(validate({pix: 'test@gmail.com'})) // true
41
- console.log(validate({pix: 'test'})) // false
42
- console.log(validate({pix: 'test@gmail.com', type : 'cpf'})) // false
43
- ```
44
-
45
- | Função | Parametro | Retorno |
46
- | :---------- | :--------- | :---------------------------------- |
47
- | `validate` | `input: {pix: string}` | boolean |
48
-
49
- #### Normalizar uma chave pix
50
-
51
- ```js
52
- import { normalize } from 'pix-utils-js'
53
- console.log(normalize({pix: '000.000.000-00'})) // {pix: '00000000000', type: 'cpf'}
54
- console.log(normalize({pix: '00.000.000/0000-00'})) // {pix: '00000000000000', type: 'cnpj'}
55
- console.log(normalize({pix: '+55 (11) 0000-0000'})) // {pix: '551100000000', type: 'phone'}
56
- ```
57
-
58
- | Função | Parametro | Retorno |
59
- | :---------- | :--------- | :---------------------------------- |
60
- | `normalize` | `input: {pix: string}` | Pix `{ pix: string, type: 'email' \| 'cpf' \| 'cnpj' \| 'random' \|'phone' }` |
61
-
62
-
63
-
64
- ## Rodando os testes
65
-
66
- Para rodar os testes, rode o seguinte comando
67
-
68
- ```bash
69
- npm run test
70
- ```
71
-
1
+
2
+ # 💸 pix-utils-js
3
+
4
+ ![](https://img.shields.io/npm/dw/pix-utils-js)
5
+ ![](https://img.shields.io/npm/v/pix-utils-js)
6
+ ![](https://img.shields.io/github/contributors/paulohenriquesn/pix-utils-js)
7
+ ![](https://img.shields.io/npm/l/pix-utils-js)
8
+
9
+ Uma pequena biblioteca que irá te ajudar a lidar com chaves pix
10
+
11
+
12
+ ## Instalação
13
+
14
+ Instale pix-utils-js com npm ou yarn
15
+
16
+ ```bash
17
+ npm install pix-utils-js
18
+ yarn add pix-utils-js
19
+ ```
20
+
21
+ ## Documentação
22
+
23
+ #### Identifique uma chave pix
24
+
25
+ ```js
26
+ import { identify } from 'pix-utils-js'
27
+ const { pix, type } = identify({pix: 'test@gmail.com'}) // {pix: 'test@gmail.com', type: 'email'}
28
+ ```
29
+
30
+ | Função | Parametro | Retorno |
31
+ | :---------- | :--------- | :---------------------------------- |
32
+ | `identify` | `input: {pix: string}` | Pix `{ pix: string, type: 'email' \| 'cpf' \| 'cnpj' \| 'random' \|'phone' \| 'qrcode' }` |
33
+
34
+ #### Validar uma chave pix
35
+
36
+ ```js
37
+ import { validate } from 'pix-utils-js'
38
+ console.log(validate({pix: 'test@gmail.com'})) // true
39
+ console.log(validate({pix: 'test'})) // false
40
+ console.log(validate({pix: 'test@gmail.com', type : 'cpf'})) // false
41
+ console.log(validate({pix: '00020126360014BR.GOV...', type : 'qrcode'})) // true
42
+ ```
43
+
44
+ | Função | Parametro | Retorno |
45
+ | :---------- | :--------- | :---------------------------------- |
46
+ | `validate` | `input: {pix: string}` | boolean |
47
+
48
+ #### Normalizar uma chave pix
49
+
50
+ ```js
51
+ import { normalize } from 'pix-utils-js'
52
+ console.log(normalize({pix: '000.000.000-00'})) // {pix: '00000000000', type: 'cpf'}
53
+ console.log(normalize({pix: '00.000.000/0000-00'})) // {pix: '00000000000000', type: 'cnpj'}
54
+ console.log(normalize({pix: '(11) 98888-8888'})) // {pix: '11988888888', type: 'phone'}
55
+ console.log(normalize({pix: '00020126360014BR.GOV...'})) // {pix: '00020126360014BR.GOV...', type: 'qrcode'}
56
+ ```
57
+
58
+ | Função | Parametro | Retorno |
59
+ | :---------- | :--------- | :---------------------------------- |
60
+ | `normalize` | `input: {pix: string}` | Pix `{ pix: string, type: 'email' \| 'cpf' \| 'cnpj' \| 'random' \|'phone' \| 'qrcode' }` |
61
+
62
+
63
+
64
+ ## Rodando os testes
65
+
66
+ Para rodar os testes, rode o seguinte comando
67
+
68
+ ```bash
69
+ npm run test
70
+ ```
71
+
package/index.d.ts CHANGED
@@ -1,27 +1,27 @@
1
- export type PixType = "email" | "cpf" | "cnpj" | "random" | "phone"
2
-
3
- export interface InvalidPix extends Error {
4
- message: "chave pix invalida."
5
- }
6
-
7
- export type Pix = {
8
- type: PixType,
9
- pix: string,
10
- }
11
-
12
- export interface IdentifyDto {
13
- pix: string
14
- }
15
-
16
- export interface NormalizeDto {
17
- pix: string
18
- }
19
-
20
- export interface ValidateDto {
21
- type?: PixType,
22
- pix: string
23
- }
24
-
25
- export function identify(input: IdentifyDto): Pix | InvalidPix
26
- export function normalize(input: NormalizeDto): Pix | InvalidPix
1
+ export type PixType = "email" | "cpf" | "cnpj" | "random" | "phone" | "qrcode"
2
+
3
+ export interface InvalidPix extends Error {
4
+ message: "chave pix invalida."
5
+ }
6
+
7
+ export type Pix = {
8
+ type: PixType,
9
+ pix: string,
10
+ }
11
+
12
+ export interface IdentifyDto {
13
+ pix: string
14
+ }
15
+
16
+ export interface NormalizeDto {
17
+ pix: string
18
+ }
19
+
20
+ export interface ValidateDto {
21
+ type?: PixType,
22
+ pix: string
23
+ }
24
+
25
+ export function identify(input: IdentifyDto): Pix | InvalidPix
26
+ export function normalize(input: NormalizeDto): Pix | InvalidPix
27
27
  export function validate(input: ValidateDto): Boolean | InvalidPix
package/index.js CHANGED
@@ -1,70 +1,71 @@
1
- const { Validator } = require('./utils/validator')
2
-
3
- const validator = new Validator();
4
-
5
- const validationPixMap = {
6
- email: validator.isEmail,
7
- random: validator.isUUID,
8
- phone: validator.isPhoneNumber,
9
- cnpj: validator.isCNPJ,
10
- cpf: validator.isCPF
11
- };
12
-
13
-
14
- const validate = (input) => {
15
- const { pix } = input
16
- let { type } = input
17
-
18
- if(!pix) {
19
- throw new Error("chave pix invalida.")
20
- }
21
-
22
- if(!type) {
23
- try {
24
- let identifiedPix = identify({ pix })
25
- type = identifiedPix.type
26
- }catch {
27
- return false
28
- }
29
- }
30
-
31
- return validationPixMap[type](pix)
32
- }
33
-
34
- const identify = (input) => {
35
- const { pix } = input
36
-
37
- if(!pix) {
38
- throw new Error("chave pix invalida.")
39
- }
40
-
41
- for (const type in validationPixMap) {
42
- if (validationPixMap[type](pix)) {
43
- return { type, pix };
44
- }
45
- }
46
-
47
- throw new Error("chave pix invalida.")
48
- }
49
-
50
- const normalize = (input) => {
51
- let { pix } = input
52
-
53
- if(!pix || !validate({ pix})) {
54
- throw new Error("chave pix invalida.")
55
- }
56
-
57
- const { type } = identify({ pix })
58
-
59
- if(type === 'cpf' || type === 'cnpj' || type === 'phone') {
60
- pix = pix.replace(/[^0-9]/g, '')
61
- }
62
-
63
- return { pix, type }
64
- }
65
-
66
- module.exports = {
67
- identify,
68
- validate,
69
- normalize
1
+ const { Validator } = require('./utils/validator')
2
+
3
+ const validator = new Validator();
4
+
5
+ const validationPixMap = {
6
+ email: validator.isEmail,
7
+ random: validator.isUUID,
8
+ phone: validator.isPhoneNumber,
9
+ cnpj: validator.isCNPJ,
10
+ cpf: validator.isCPF,
11
+ qrcode: validator.isQRCode
12
+ };
13
+
14
+
15
+ const validate = (input) => {
16
+ const { pix } = input
17
+ let { type } = input
18
+
19
+ if(!pix) {
20
+ throw new Error("chave pix invalida.")
21
+ }
22
+
23
+ if(!type) {
24
+ try {
25
+ let identifiedPix = identify({ pix })
26
+ type = identifiedPix.type
27
+ }catch {
28
+ return false
29
+ }
30
+ }
31
+
32
+ return validationPixMap[type](pix)
33
+ }
34
+
35
+ const identify = (input) => {
36
+ const { pix } = input
37
+
38
+ if(!pix) {
39
+ throw new Error("chave pix invalida.")
40
+ }
41
+
42
+ for (const type in validationPixMap) {
43
+ if (validationPixMap[type](pix)) {
44
+ return { type, pix };
45
+ }
46
+ }
47
+
48
+ throw new Error("chave pix invalida.")
49
+ }
50
+
51
+ const normalize = (input) => {
52
+ let { pix } = input
53
+
54
+ if(!pix || !validate({ pix})) {
55
+ throw new Error("chave pix invalida.")
56
+ }
57
+
58
+ const { type } = identify({ pix })
59
+
60
+ if(type === 'cpf' || type === 'cnpj' || type === 'phone') {
61
+ pix = pix.replace(/[^0-9]/g, '')
62
+ }
63
+
64
+ return { pix, type }
65
+ }
66
+
67
+ module.exports = {
68
+ identify,
69
+ validate,
70
+ normalize
70
71
  }
package/package.json CHANGED
@@ -1,23 +1,23 @@
1
- {
2
- "name": "pix-utils-js",
3
- "version": "1.0.6",
4
- "author": "Paulo Henrique",
5
- "description": "Um pacote para fornecer utilidades e validações para chaves pix.",
6
- "main": "index.js",
7
- "repository": {
8
- "type": "git",
9
- "url": "https://github.com/paulohenriquesn/pix-utils-js.git"
10
- },
11
- "bugs": {
12
- "url": "https://github.com/paulohenriquesn/pix-utils-js/issues"
13
- },
14
- "homepage": "https://github.com/paulohenriquesn/pix-utils-js#readme",
15
- "scripts": {
16
- "test": "node tests/index.js"
17
- },
18
- "keywords": [
19
- "pix",
20
- "chave pix"
21
- ],
22
- "license": "MIT"
23
- }
1
+ {
2
+ "name": "pix-utils-js",
3
+ "version": "1.0.8",
4
+ "author": "Paulo Henrique",
5
+ "description": "Um pacote para fornecer utilidades e validações para chaves pix.",
6
+ "main": "index.js",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/paulohenriquesn/pix-utils-js.git"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/paulohenriquesn/pix-utils-js/issues"
13
+ },
14
+ "homepage": "https://github.com/paulohenriquesn/pix-utils-js#readme",
15
+ "scripts": {
16
+ "test": "node tests/index.js"
17
+ },
18
+ "keywords": [
19
+ "pix",
20
+ "chave pix"
21
+ ],
22
+ "license": "MIT"
23
+ }
@@ -1,107 +1,107 @@
1
- class Generator {
2
- static generateValidPhoneNumber() {
3
- const dddList = ['11', '21', '31', '41', '51', '61', '71', '81', '91'];
4
- const selectedDDD = dddList[Math.floor(Math.random() * dddList.length)];
5
-
6
- const phoneNumber = `${selectedDDD}9${Generator.generateRandomNumber(8)}`;
7
- return phoneNumber;
8
- }
9
-
10
- static generateRandomNumber(length) {
11
- let randomNumber = '';
12
- for (let i = 0; i < length; i++) {
13
- randomNumber += Math.floor(Math.random() * 10);
14
- }
15
- return randomNumber;
16
- }
17
-
18
- static generateRandomCPF() {
19
- function randomDigit() {
20
- return Math.floor(Math.random() * 10);
21
- }
22
-
23
- const cpfDigits = [];
24
- for (let i = 0; i < 9; i++) {
25
- cpfDigits.push(randomDigit());
26
- }
27
-
28
- const firstVerifierDigit = Generator.calculateCPFVerifierDigit(cpfDigits);
29
- cpfDigits.push(firstVerifierDigit);
30
-
31
- const secondVerifierDigit = Generator.calculateCPFVerifierDigit(cpfDigits);
32
- cpfDigits.push(secondVerifierDigit);
33
-
34
- return cpfDigits.join('');
35
- }
36
-
37
- static calculateCPFVerifierDigit(digits) {
38
- const sum = digits.reduce((acc, digit, index) => {
39
- const multiplier = digits.length + 1 - index;
40
- return acc + digit * multiplier;
41
- }, 0);
42
-
43
- const remainder = sum % 11;
44
- return remainder < 2 ? 0 : 11 - remainder;
45
- }
46
- static generateValidCNPJ() {
47
- function randomDigit() {
48
- return Math.floor(Math.random() * 10);
49
- }
50
-
51
- const cnpjBase = [];
52
- for (let i = 0; i < 12; i++) {
53
- cnpjBase.push(randomDigit());
54
- }
55
-
56
- const verifierDigits = Generator.calculateCNPJVerifierDigits(cnpjBase);
57
- cnpjBase.push(verifierDigits[0], verifierDigits[1]);
58
-
59
- const formattedCNPJ = cnpjBase.join('');
60
-
61
- return formattedCNPJ.replace(/(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})/, '$1.$2.$3/$4-$5');
62
- }
63
-
64
- static calculateCNPJVerifierDigits(digits) {
65
- const multipliers1 = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
66
- const multipliers2 = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3];
67
-
68
- function calculateDigit(multipliers) {
69
- const sum = digits.reduce((acc, digit, index) => acc + digit * multipliers[index], 0);
70
- const remainder = sum % 11;
71
- return remainder < 2 ? 0 : 11 - remainder;
72
- }
73
-
74
- const firstDigit = calculateDigit(multipliers1);
75
- const secondDigit = calculateDigit(multipliers2);
76
-
77
- return [firstDigit, secondDigit];
78
- }
79
-
80
- static generateRandomUUID() {
81
- return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
82
- const r = Math.random() * 16 | 0;
83
- const v = c === 'x' ? r : (r & 0x3 | 0x8);
84
- return v.toString(16);
85
- });
86
- }
87
-
88
- static generateRandomEmail() {
89
- const domains = ['gmail.com', 'yahoo.com', 'hotmail.com', 'outlook.com', 'example.com'];
90
- const username = Generator.generateRandomString(8);
91
- const domain = domains[Math.floor(Math.random() * domains.length)];
92
- return `${username}@${domain}`;
93
- }
94
-
95
- static generateRandomString(length) {
96
- const characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
97
- let randomString = '';
98
- for (let i = 0; i < length; i++) {
99
- const randomIndex = Math.floor(Math.random() * characters.length);
100
- randomString += characters.charAt(randomIndex);
101
- }
102
- return randomString;
103
- }
104
- }
105
-
106
-
1
+ class Generator {
2
+ static generateValidPhoneNumber() {
3
+ const dddList = ['11', '21', '31', '41', '51', '61', '71', '81', '91'];
4
+ const selectedDDD = dddList[Math.floor(Math.random() * dddList.length)];
5
+ const firstDigit = Math.random() < 0.5 ? '8' : '9';
6
+ const phoneNumber = `${selectedDDD}${firstDigit}${Generator.generateRandomNumber(8)}`;
7
+ return phoneNumber;
8
+ }
9
+
10
+ static generateRandomNumber(length) {
11
+ let randomNumber = '';
12
+ for (let i = 0; i < length; i++) {
13
+ randomNumber += Math.floor(Math.random() * 10);
14
+ }
15
+ return randomNumber;
16
+ }
17
+
18
+ static generateRandomCPF() {
19
+ function randomDigit() {
20
+ return Math.floor(Math.random() * 10);
21
+ }
22
+
23
+ const cpfDigits = [];
24
+ for (let i = 0; i < 9; i++) {
25
+ cpfDigits.push(randomDigit());
26
+ }
27
+
28
+ const firstVerifierDigit = Generator.calculateCPFVerifierDigit(cpfDigits);
29
+ cpfDigits.push(firstVerifierDigit);
30
+
31
+ const secondVerifierDigit = Generator.calculateCPFVerifierDigit(cpfDigits);
32
+ cpfDigits.push(secondVerifierDigit);
33
+
34
+ return cpfDigits.join('');
35
+ }
36
+
37
+ static calculateCPFVerifierDigit(digits) {
38
+ const sum = digits.reduce((acc, digit, index) => {
39
+ const multiplier = digits.length + 1 - index;
40
+ return acc + digit * multiplier;
41
+ }, 0);
42
+
43
+ const remainder = sum % 11;
44
+ return remainder < 2 ? 0 : 11 - remainder;
45
+ }
46
+ static generateValidCNPJ() {
47
+ function randomDigit() {
48
+ return Math.floor(Math.random() * 10);
49
+ }
50
+
51
+ const cnpjBase = [];
52
+ for (let i = 0; i < 12; i++) {
53
+ cnpjBase.push(randomDigit());
54
+ }
55
+
56
+ const verifierDigits = Generator.calculateCNPJVerifierDigits(cnpjBase);
57
+ cnpjBase.push(verifierDigits[0], verifierDigits[1]);
58
+
59
+ const formattedCNPJ = cnpjBase.join('');
60
+
61
+ return formattedCNPJ.replace(/(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})/, '$1.$2.$3/$4-$5');
62
+ }
63
+
64
+ static calculateCNPJVerifierDigits(digits) {
65
+ const multipliers1 = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
66
+ const multipliers2 = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3];
67
+
68
+ function calculateDigit(multipliers) {
69
+ const sum = digits.reduce((acc, digit, index) => acc + digit * multipliers[index], 0);
70
+ const remainder = sum % 11;
71
+ return remainder < 2 ? 0 : 11 - remainder;
72
+ }
73
+
74
+ const firstDigit = calculateDigit(multipliers1);
75
+ const secondDigit = calculateDigit(multipliers2);
76
+
77
+ return [firstDigit, secondDigit];
78
+ }
79
+
80
+ static generateRandomUUID() {
81
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
82
+ const r = Math.random() * 16 | 0;
83
+ const v = c === 'x' ? r : (r & 0x3 | 0x8);
84
+ return v.toString(16);
85
+ });
86
+ }
87
+
88
+ static generateRandomEmail() {
89
+ const domains = ['gmail.com', 'yahoo.com', 'hotmail.com', 'outlook.com', 'example.com'];
90
+ const username = Generator.generateRandomString(8);
91
+ const domain = domains[Math.floor(Math.random() * domains.length)];
92
+ return `${username}@${domain}`;
93
+ }
94
+
95
+ static generateRandomString(length) {
96
+ const characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
97
+ let randomString = '';
98
+ for (let i = 0; i < length; i++) {
99
+ const randomIndex = Math.floor(Math.random() * characters.length);
100
+ randomString += characters.charAt(randomIndex);
101
+ }
102
+ return randomString;
103
+ }
104
+ }
105
+
106
+
107
107
  module.exports = { Generator }
package/tests/index.js CHANGED
@@ -1,82 +1,109 @@
1
- const assert = require('assert');
2
- const { describe, it } = require('node:test')
3
- const { identify, normalize, validate } = require('../index')
4
- const { Generator } = require('./generator')
5
-
6
- describe('validate', () => {
7
- it('should returns false if no pix key is provided', (t) => {
8
- const sut = validate({pix: 'invalid'})
9
- assert.strictEqual(sut, false);
10
- });
11
-
12
- it('should returns true if email key is provided and is valid', (t) => {
13
- const sut = validate({pix: Generator.generateRandomEmail()})
14
- assert.strictEqual(sut, true);
15
- });
16
-
17
- it('should returns true if phone number key is provided and is valid', (t) => {
18
- const sut = validate({pix: Generator.generateValidPhoneNumber()})
19
- assert.strictEqual(sut, true);
20
- });
21
-
22
- it('should returns true if random key is provided and is valid', (t) => {
23
- const sut = validate({pix: Generator.generateRandomUUID()})
24
- assert.strictEqual(sut, true);
25
- });
26
- })
27
-
28
- describe('identify', () => {
29
- it('should returns email if email pix is provided', (t) => {
30
- const sut = identify({ pix: 'test@gmail.com'})
31
- assert.notStrictEqual(sut, { pix: 'test@gmail.com', type: 'email' });
32
- });
33
-
34
- it('should returns cpf if cpf pix is provided', (t) => {
35
- const cpf = Generator.generateRandomCPF()
36
- const sut = identify({ pix: cpf})
37
- assert.notStrictEqual(sut, { pix: cpf, type: 'cpf' });
38
- });
39
-
40
-
41
- it('should returns cnpj if cnpj pix is provided', (t) => {
42
- const cnpj = Generator.generateValidCNPJ()
43
- const sut = identify({ pix: cnpj})
44
- assert.notStrictEqual(sut, { pix: cnpj, type: 'cnpj' });
45
- });
46
-
47
- it('should returns random if random pix is provided', (t) => {
48
- const random = Generator.generateRandomUUID()
49
- const sut = identify({ pix: random })
50
- assert.notStrictEqual(sut, { pix: random, type: 'random' });
51
- });
52
-
53
- it('should returns phone if random pix is provided', (t) => {
54
- const phone = Generator.generateValidPhoneNumber()
55
- const sut = identify({ pix: phone })
56
- assert.notStrictEqual(sut, { pix: phone, type: 'phone' });
57
- });
58
-
59
- it('should throws if invalid pix is provided', (t) => {
60
- let sut;
61
- try {
62
- sut = identify({ pix: 'abc' })
63
- }catch (err) {
64
- sut = err;
65
- }
66
- assert.notStrictEqual(sut, Error);
67
- });
68
- })
69
-
70
- describe("normalize", () => {
71
- it('should normalize cpf key', (t) => {
72
- assert.deepEqual(normalize({ pix: "000.000.000-00"}), { pix: '00000000000', type: 'cpf'});
73
- });
74
-
75
- it('should normalize cnpj key', (t) => {
76
- assert.deepEqual(normalize({ pix: "00.000.000/0000-00"}), { pix: '00000000000000', type: 'cnpj'});
77
- });
78
-
79
- it('should normalize phone key', (t) => {
80
- assert.deepEqual(normalize({ pix: "+55 (11) 0000-0000"}), { pix: '551100000000', type: 'phone'});
81
- });
82
- })
1
+ const assert = require('assert');
2
+ const { describe, it } = require('node:test')
3
+ const { identify, normalize, validate } = require('../index')
4
+ const { Generator } = require('./generator')
5
+
6
+ describe('validate', () => {
7
+ it('should returns false if no pix key is provided', (t) => {
8
+ const sut = validate({pix: 'invalid'})
9
+ assert.strictEqual(sut, false);
10
+ });
11
+
12
+ it('should returns false if qr pix key is provided', (t) => {
13
+ const sut = validate({pix: 'invalid', type: 'qrcode'})
14
+ assert.strictEqual(sut, false);
15
+ });
16
+
17
+ it('should returns true if email key is provided and is valid', (t) => {
18
+ const sut = validate({pix: Generator.generateRandomEmail()})
19
+ assert.strictEqual(sut, true);
20
+ });
21
+
22
+ it('should returns true if phone number key is provided and is valid', (t) => {
23
+ const sut = validate({pix: Generator.generateValidPhoneNumber()})
24
+ assert.strictEqual(sut, true);
25
+ });
26
+
27
+ it('should returns true if random key is provided and is valid', (t) => {
28
+ const sut = validate({pix: Generator.generateRandomUUID()})
29
+ assert.strictEqual(sut, true);
30
+ });
31
+
32
+ it('should returns true qrcode if qrcode pix is provided and is valid', (t) => {
33
+ const qrCode = '00020126360014BR.GOV.BCB.PIX0114+5511999226498520400005303986540813213.005802BR5904test6004test62070503***630450CF'
34
+ const sut = validate({ pix: qrCode })
35
+ assert.strictEqual(sut, true);
36
+ });
37
+
38
+ it('should returns false qrcode if qrcode pix is provided and is invalid', (t) => {
39
+ const qrCode = '/INVALID/00020126360014BR.GOV.BCB.PIX0114+5511999226498520400005303986540813213.005802BR5904test6004test62070503***630450CF'
40
+ const sut = validate({ pix: qrCode })
41
+ assert.strictEqual(sut, false);
42
+ });
43
+ })
44
+
45
+ describe('identify', () => {
46
+ it('should returns email if email pix is provided', (t) => {
47
+ const sut = identify({ pix: 'test@gmail.com'})
48
+ assert.notStrictEqual(sut, { pix: 'test@gmail.com', type: 'email' });
49
+ });
50
+
51
+ it('should returns cpf if cpf pix is provided', (t) => {
52
+ const cpf = Generator.generateRandomCPF()
53
+ const sut = identify({ pix: cpf})
54
+ assert.notStrictEqual(sut, { pix: cpf, type: 'cpf' });
55
+ });
56
+
57
+
58
+ it('should returns cnpj if cnpj pix is provided', (t) => {
59
+ const cnpj = Generator.generateValidCNPJ()
60
+ const sut = identify({ pix: cnpj})
61
+ assert.notStrictEqual(sut, { pix: cnpj, type: 'cnpj' });
62
+ });
63
+
64
+ it('should returns random if random pix is provided', (t) => {
65
+ const random = Generator.generateRandomUUID()
66
+ const sut = identify({ pix: random })
67
+ assert.notStrictEqual(sut, { pix: random, type: 'random' });
68
+ });
69
+
70
+ it('should returns phone if random pix is provided', (t) => {
71
+ const phone = Generator.generateValidPhoneNumber()
72
+ const sut = identify({ pix: phone })
73
+ assert.notStrictEqual(sut, { pix: phone, type: 'phone' });
74
+ });
75
+
76
+ it('should returns qrcode if qrcode pix is provided', (t) => {
77
+ const qrCode = '00020126360014BR.GOV.BCB.PIX0114+5511999226498520400005303986540813213.005802BR5904test6004test62070503***630450CF'
78
+ const sut = identify({ pix: qrCode })
79
+ assert.notStrictEqual(sut, { pix: qrCode, type: 'qrcode' });
80
+ });
81
+
82
+ it('should throws if invalid pix is provided', (t) => {
83
+ let sut;
84
+ try {
85
+ sut = identify({ pix: 'abc' })
86
+ }catch (err) {
87
+ sut = err;
88
+ }
89
+ assert.notStrictEqual(sut, Error);
90
+ });
91
+ })
92
+
93
+ describe("normalize", () => {
94
+ it('should normalize cpf key', (t) => {
95
+ assert.deepEqual(normalize({ pix: "000.000.000-00"}), { pix: '00000000000', type: 'cpf'});
96
+ });
97
+
98
+ it('should normalize cnpj key', (t) => {
99
+ assert.deepEqual(normalize({ pix: "00.000.000/0000-00"}), { pix: '00000000000000', type: 'cnpj'});
100
+ });
101
+
102
+ it('should normalize phone key', (t) => {
103
+ assert.deepEqual(normalize({ pix: "(11) 98888-8888"}), { pix: '11988888888', type: 'phone'});
104
+ });
105
+
106
+ it('should normalize qrcode key', (t) => {
107
+ assert.deepEqual(normalize({ pix: "00020126360014BR.GOV.BCB.PIX0114+5511999226498520400005303986540813213.005802BR5904test6004test62070503***630450CF"}), { pix: '00020126360014BR.GOV.BCB.PIX0114+5511999226498520400005303986540813213.005802BR5904test6004test62070503***630450CF', type: 'qrcode'});
108
+ });
109
+ })
@@ -1,30 +1,38 @@
1
- class Validator {
2
- isEmail(email) {
3
- const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
4
- return emailRegex.test(email)
5
- }
6
-
7
- isUUID(uuid) {
8
- const uuidRegex = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/
9
- return uuidRegex.test(uuid)
10
- }
11
-
12
- isPhoneNumber(phoneNumber) {
13
- const phoneNumberRegex = /^(\+\d{2}\s*)?(\(\d{2}\))?\s*(\d{4,5}-?\d{4})$/
14
- return phoneNumberRegex.test(phoneNumber)
15
- }
16
-
17
- isCPF(cpf) {
18
- const cpfRegex = /^(\d{3}\.\d{3}\.\d{3}-\d{2}|\d{11})$/;
19
- return cpfRegex.test(cpf)
20
- }
21
-
22
- isCNPJ(cnpj) {
23
- const cnpjRegex = /^(\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2}|\d{14})$/;
24
- return cnpjRegex.test(cnpj)
25
- }
26
- }
27
-
28
- module.exports = {
29
- Validator
1
+ class Validator {
2
+ isEmail(email) {
3
+ const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
4
+ return emailRegex.test(email)
5
+ }
6
+
7
+ isUUID(uuid) {
8
+ const uuidRegex = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/
9
+ return uuidRegex.test(uuid)
10
+ }
11
+
12
+ isPhoneNumber(phoneNumber) {
13
+ const normalized = phoneNumber.replace(/[^0-9]/g, '');
14
+
15
+ const phoneNumberRegex = /^\d{2}[89]\d{8}$/;
16
+
17
+ return phoneNumberRegex.test(normalized);
18
+ }
19
+
20
+ isCPF(cpf) {
21
+ const cpfRegex = /^(\d{3}\.\d{3}\.\d{3}-\d{2}|\d{11})$/;
22
+ return cpfRegex.test(cpf)
23
+ }
24
+
25
+ isCNPJ(cnpj) {
26
+ const cnpjRegex = /^(\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2}|\d{14})$/;
27
+ return cnpjRegex.test(cnpj)
28
+ }
29
+
30
+ isQRCode(pixKeyCopyPaste) {
31
+ const qrCodeRegex = /^(000201|01|2636)/
32
+ return qrCodeRegex.test(pixKeyCopyPaste)
33
+ }
34
+ }
35
+
36
+ module.exports = {
37
+ Validator
30
38
  }