-
Notifications
You must be signed in to change notification settings - Fork 115
Feat/placa renavam #138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
eduardo-fernandes-marques
wants to merge
4
commits into
brazilian-utils:main
from
eduardo-fernandes-marques:feat/placa-renavam
Closed
Feat/placa renavam #138
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import { isValid } from '.'; | ||
|
|
||
| describe('isValid', () => { | ||
| describe('should return false', () => { | ||
| test('when is a empty string', () => { | ||
| expect(isValid('')).toBe(false); | ||
| }); | ||
|
|
||
| test('when is null', () => { | ||
| expect(isValid(null as any)).toBe(false); | ||
| }); | ||
|
|
||
| test('when is undefined', () => { | ||
| expect(isValid(undefined as any)).toBe(false); | ||
| }); | ||
|
|
||
| test('when is a boolean', () => { | ||
| expect(isValid(true as any)).toBe(false); | ||
| expect(isValid(false as any)).toBe(false); | ||
| }); | ||
|
|
||
| test('when is a object', () => { | ||
| expect(isValid({} as any)).toBe(false); | ||
| }); | ||
|
|
||
| test('when is a array', () => { | ||
| expect(isValid([] as any)).toBe(false); | ||
| }); | ||
|
|
||
| test(`when dont match with CPF length`, () => { | ||
| expect(isValid('IWH-86288')).toBe(false); | ||
| }); | ||
|
|
||
| test('when contains only letters', () => { | ||
| expect(isValid('IWH')).toBe(false); | ||
| }); | ||
|
|
||
| test('when contains only numbers', () => { | ||
| expect(isValid('8628')).toBe(false); | ||
| }); | ||
|
|
||
| test('when is invalid mercosul plate', () => { | ||
| expect(isValid('IWH-8628', { mercosul: true })).toBe(false); | ||
| }); | ||
|
|
||
| test('when is invalid motorcycle mercosul plate', () => { | ||
| expect(isValid('AAA-1A11', { mercosul: true, motorcycle: true })).toBe(false); | ||
| }); | ||
|
|
||
| test('when options params is not pass in mercosul plate', () => { | ||
| expect(isValid('AAA-1A11')).toBe(false); | ||
| }); | ||
|
|
||
| test('when options params is not pass in motorcycle mercosul plate', () => { | ||
| expect(isValid('AAA-11A1', { mercosul: true })).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('should return true', () => { | ||
| test('when is a valid plate without mask', () => { | ||
| expect(isValid('IWH8628')).toBe(true); | ||
| }); | ||
|
|
||
| test('when is a valid plate with mask', () => { | ||
| expect(isValid('IWH-8628')).toBe(true); | ||
| }); | ||
|
|
||
| test('when is a valid mercosul plate', () => { | ||
| expect(isValid('AAA-1A11', { mercosul: true })).toBe(true); | ||
| }); | ||
|
|
||
| test('when is a valid motorcycle mercosul plate', () => { | ||
| expect(isValid('AAA-11A1', { mercosul: true, motorcycle: true })).toBe(true); | ||
| }); | ||
|
|
||
| test('when is a valid mercosul plate without mask', () => { | ||
| expect(isValid('AAA1A11', { mercosul: true })).toBe(true); | ||
| }); | ||
|
|
||
| test('when is a valid motorcycle mercosul plate without mask', () => { | ||
| expect(isValid('AAA11A1', { mercosul: true, motorcycle: true })).toBe(true); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| export type Options = { | ||
| mercosul?: boolean; | ||
| motorcycle?: boolean; | ||
| }; | ||
|
|
||
| export const isValid = (input: string, options?: Options) => { | ||
| if (!input || typeof input !== 'string') return false; | ||
|
|
||
| const plate = input.replace('-', ''); | ||
|
|
||
| if (options?.mercosul && options?.motorcycle) { | ||
| return /^[a-zA-Z]{3}[0-9]{2}[a-zA-Z]{1}[0-9]{1}$/.test(plate); | ||
| } else if (options?.mercosul) { | ||
| return /^[a-zA-Z]{3}[0-9]{1}[a-zA-Z]{1}[0-9]{2}$/.test(plate); | ||
| } else { | ||
| return /^[a-zA-Z]{3}[0-9]{4}$/.test(plate); | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import { isValid, LENGTH } from '.'; | ||
|
|
||
| describe('isValid', () => { | ||
| describe('should return false', () => { | ||
| test('when is a empty string', () => { | ||
| expect(isValid('')).toBe(false); | ||
| }); | ||
|
|
||
| test('when is null', () => { | ||
| expect(isValid(null as any)).toBe(false); | ||
| }); | ||
|
|
||
| test('when is undefined', () => { | ||
| expect(isValid(undefined as any)).toBe(false); | ||
| }); | ||
|
|
||
| test('when is a boolean', () => { | ||
| expect(isValid(true as any)).toBe(false); | ||
| expect(isValid(false as any)).toBe(false); | ||
| }); | ||
|
|
||
| test('when is a object', () => { | ||
| expect(isValid({} as any)).toBe(false); | ||
| }); | ||
|
|
||
| test('when is a array', () => { | ||
| expect(isValid([] as any)).toBe(false); | ||
| }); | ||
|
|
||
| test(`when dont match with CPF length (${LENGTH})`, () => { | ||
| expect(isValid('123456')).toBe(false); | ||
| }); | ||
|
|
||
| test('when contains only letters or special characters', () => { | ||
| expect(isValid('abcabcabcde')).toBe(false); | ||
| }); | ||
|
|
||
| test('when is a invalid Renavam', () => { | ||
| expect(isValid('95965727047')).toBe(false); | ||
| }); | ||
|
|
||
| test('when is a invalid Renavam test numbers with letters', () => { | ||
| expect(isValid('foo391.838.38test0-66')).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('should return true', () => { | ||
| test('when is a Renavam valid without mask', () => { | ||
| expect(isValid('95965727048')).toBe(true); | ||
| }); | ||
|
|
||
| test('when is a valid Renavam with mask', () => { | ||
| expect(isValid('9596572704-8')).toBe(true); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import { onlyNumbers } from '../../helpers'; | ||
|
|
||
| export const MIN_LENGTH = 9; | ||
| export const LENGTH = 11; | ||
| export const SUM = 284; | ||
|
|
||
| export const isValid = (input: string) => { | ||
hyanmandian marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (!input || typeof input !== 'string' || input.length < MIN_LENGTH) return false; | ||
|
|
||
| let result = onlyNumbers(input.padStart(11, '0')); | ||
hyanmandian marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if (!result.match('[0-9]{11}')) return false; | ||
|
|
||
| let resultWithoutDigit = result.substring(0, 10); | ||
hyanmandian marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| resultWithoutDigit = resultWithoutDigit.split('').reverse().join(''); | ||
|
|
||
| const sum = sumCalculationRest(resultWithoutDigit); | ||
|
|
||
| const realDigit = parseInt(input.substring(input.length - 1, input.length)); | ||
|
|
||
| return sum === realDigit; | ||
| }; | ||
|
|
||
| const sumCalculationRest = (input: string) => { | ||
hyanmandian marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let sum = 0; | ||
| let multiple = 2; | ||
|
|
||
| for (let i = 0; i < 10; i++) { | ||
| sum += parseInt(input.substring(i, i + 1)) * multiple; | ||
|
|
||
| if (multiple >= 9) { | ||
| multiple = 2; | ||
| } else { | ||
| multiple++; | ||
| } | ||
| } | ||
|
|
||
| sum = LENGTH - (sum % LENGTH); | ||
|
|
||
| return sum; | ||
| }; | ||
|
|
||
| //https://github.com/eliseuborges/Renavam/blob/master/Renavam.js | ||
hyanmandian marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.