Skip to content

Commit

Permalink
chore: fix lint and tests (#34)
Browse files Browse the repository at this point in the history
* chore: fix lint and tests

* chore: fix lint

* chore: change actions version
  • Loading branch information
AZagatti committed Jan 15, 2023
1 parent 9031f2c commit cb97be1
Show file tree
Hide file tree
Showing 10 changed files with 3,607 additions and 4,795 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Expand Up @@ -15,14 +15,14 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Set up Node
uses: actions/setup-node@v1
uses: actions/setup-node@v3
with:
node-version: 12
node-version: 16

- name: Install dependencies
run: yarn
Expand Down
1 change: 1 addition & 0 deletions .nvmrc
@@ -0,0 +1 @@
16
30 changes: 15 additions & 15 deletions package.json
Expand Up @@ -28,21 +28,21 @@
"postversion": "git push && git push --tags"
},
"devDependencies": {
"@types/jest": "^26.0.22",
"@typescript-eslint/eslint-plugin": "^4.22.0",
"@typescript-eslint/parser": "^4.22.0",
"eslint": "^7.24.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-config-prettier": "^8.2.0",
"eslint-import-resolver-typescript": "^2.4.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-prettier": "^3.4.0",
"husky": "6",
"jest": "^26.6.3",
"lint-staged": "^10.5.4",
"prettier": "^2.2.1",
"ts-jest": "^26.5.5",
"typescript": "^4.2.4"
"@types/jest": "^29.2.5",
"@typescript-eslint/eslint-plugin": "^5.48.1",
"@typescript-eslint/parser": "^5.48.1",
"eslint": "^8.32.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-prettier": "^8.6.0",
"eslint-import-resolver-typescript": "^3.5.3",
"eslint-plugin-import": "^2.27.4",
"eslint-plugin-prettier": "^4.2.1",
"husky": "8.0.3",
"jest": "^29.3.1",
"lint-staged": "^13.1.0",
"prettier": "^2.8.3",
"ts-jest": "^29.0.5",
"typescript": "^4.9.4"
},
"lint-staged": {
"*.{js,json,jsx,ts,tsx}": [
Expand Down
8 changes: 3 additions & 5 deletions src/__tests__/validateCep.spec.ts
Expand Up @@ -10,7 +10,7 @@ describe('Validate CEP', () => {
it('should be able return false to invalid CEP', () => {
const parameters = [
'172800-0000',
'17280000',
'172800000',
'172800-000',
'17280-0000',
'',
Expand All @@ -22,12 +22,10 @@ describe('Validate CEP', () => {
});

it('should return false for invalid parameter type', () => {
const parameters = [{}, [], 0, NaN, Boolean, true];
const parameters = [{}, [], 0, NaN, Boolean, true] as string[];

expect.assertions(parameters.length);

parameters.forEach(parameter =>
expect(validateCep((parameter as unknown) as string)).toBe(false),
);
parameters.forEach(parameter => expect(validateCep(parameter)).toBe(false));
});
});
12 changes: 6 additions & 6 deletions src/__tests__/validateUF.spec.ts
Expand Up @@ -10,11 +10,11 @@ describe('Validate UF', () => {
});

it('should be able return false if invalid parameter type', () => {
expect(validateUF((undefined as unknown) as string)).toBe(false);
expect(validateUF((null as unknown) as string)).toBe(false);
expect(validateUF((0 as unknown) as string)).toBe(false);
expect(validateUF(({} as unknown) as string)).toBe(false);
expect(validateUF(([] as unknown) as string)).toBe(false);
expect(validateUF((true as unknown) as string)).toBe(false);
expect(validateUF(undefined as unknown as string)).toBe(false);
expect(validateUF(null as unknown as string)).toBe(false);
expect(validateUF(0 as unknown as string)).toBe(false);
expect(validateUF({} as unknown as string)).toBe(false);
expect(validateUF([] as unknown as string)).toBe(false);
expect(validateUF(true as unknown as string)).toBe(false);
});
});
2 changes: 1 addition & 1 deletion src/validations/usevalidationsbr.ts
Expand Up @@ -30,7 +30,7 @@ export const useValidationsBR = (
break;
case 'cnh':
result = validateCNH(value);
break;
break;
case 'phone':
result = validatePhone(value);
break;
Expand Down
28 changes: 16 additions & 12 deletions src/validations/validateCNH.ts
Expand Up @@ -3,25 +3,29 @@
* O tamanho deve ser respeitado como em '000000000-00' ou '00000000000'.
* @param value
*/
export function validateCNH (value: string) {
export function validateCNH(value: string) {
const clearValue = String(value).replace(/\D/g, '');
if (!clearValue || (clearValue.replace(new RegExp(clearValue[0], 'g'), '').trim().length === 0)) {
if (
!clearValue ||
clearValue.replace(new RegExp(clearValue[0], 'g'), '').trim().length === 0
) {
return false;
}
let v = 0;
for (let i = 0, j = 9, v = 0; i < 9; ++i, --j) {
v += +(value.charAt(i) * j);
let val = 0;
for (let i = 0, j = 9; i < 9; ++i, --j) {
val += Number(value.charAt(i)) * j;
}
let dsc = 0, vl1 = v % 11;
let dsc = 0;
let vl1 = val % 11;
if (vl1 >= 10) {
vl1 = 0;
dsc = 2;
}
for (let i = 0, j = 1, v = 0; i < 9; ++i, ++j) {
v += +(value.charAt(i) * j);
val = 0;
for (let i = 0, j = 1; i < 9; ++i, ++j) {
val += Number(value.charAt(i)) * j;
}
let x = v % 11;
let vl2 = (x >= 10) ? 0 : x - dsc;
return ('' + vl1 + vl2) === value.substr(-2);
const x = val % 11;
const vl2 = x >= 10 ? 0 : x - dsc;
return `${vl1}${vl2}` === value.substring(value.length - 2);
}

3 changes: 2 additions & 1 deletion src/validations/validateEmail.ts
@@ -1,5 +1,6 @@
export function validateEmail(value: string): boolean {
const regex = /^[\w!#$%&'*+/=?`{|}~^-]+(?:\.[\w!#$%&'*+/=?`{|}~^-]+)*@(?:[A-Z0-9-]+\.)+[A-Z]{2,10}$/i;
const regex =
/^[\w!#$%&'*+/=?`{|}~^-]+(?:\.[\w!#$%&'*+/=?`{|}~^-]+)*@(?:[A-Z0-9-]+\.)+[A-Z]{2,10}$/i;
const email = value;
if (regex.test(email)) {
return Boolean(email);
Expand Down
3 changes: 2 additions & 1 deletion src/validations/validateUF.ts
@@ -1,4 +1,5 @@
export const validateUF = (() => {
const regex = /^(?:ac|al|ap|am|ba|ce|df|es|go|ma|mt|ms|mg|pa|pb|pr|pe|pi|rj|rn|rs|ro|rr|sc|sp|se|to)$/i;
const regex =
/^(?:ac|al|ap|am|ba|ce|df|es|go|ma|mt|ms|mg|pa|pb|pr|pe|pi|rj|rn|rs|ro|rr|sc|sp|se|to)$/i;
return (uf: string) => typeof uf === 'string' && regex.test(uf);
})();

0 comments on commit cb97be1

Please sign in to comment.