-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add validation to phone number by format
* feat(telephones): verify phone numbers * test(telephones): check valid and invalid cases Use property-based testing with fast-check * docs(telephones): update README.md Add examples for new functions * test(telephones): verify unassigned phone numbers The SIGET did assign yet phones outside the range of 2000-0000 to 2999-9999 and 6000-0000 to 7999-9999 SEE https://www.siget.gob.sv/numeros-fijos/ https://www.siget.gob.sv/numeros-moviles/
- Loading branch information
Showing
6 changed files
with
208 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 |
---|---|---|
@@ -1,2 +1,7 @@ | ||
export { isDUI, isNIT } from './lib/documents'; | ||
export { isMunicipalityCode } from './lib/municipalities'; | ||
export { | ||
isMobilePhoneNumber, | ||
isResidentialPhoneNumber, | ||
isPhoneNumber, | ||
} from './lib/telephones'; |
This file contains 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,90 @@ | ||
import fc from 'fast-check'; | ||
import { | ||
isMobilePhoneNumber, | ||
isResidentialPhoneNumber, | ||
isPhoneNumber, | ||
} from '../telephones'; | ||
|
||
/** | ||
* Mask a number with phone format | ||
* | ||
* @memberof helpers | ||
* @param {number} number A 8 digits number | ||
* @returns {string} A phone number formatted | ||
*/ | ||
const maskPhoneNumber = (number: number): string => | ||
number.toString().replace(/^(\d{4})(\d{4})$/, '$1-$2'); | ||
/** | ||
* Generate mobile phone numbers from 6000-0000 to 7999-9999 | ||
* | ||
* @returns {() => fc.Arbitrary<string>} A random mobile phone number | ||
*/ | ||
const mobilePhone: () => fc.Arbitrary<string> = () => | ||
fc.integer(60000000, 79999999).map(maskPhoneNumber); | ||
/** | ||
* Generate residential phone number from 2000-0000 to 2999-9999 | ||
* | ||
* @returns {() => fc.Arbitrary<string>} A random residential phone number | ||
*/ | ||
const residentialPhone: () => fc.Arbitrary<string> = () => | ||
fc.integer(20000000, 29999999).map(maskPhoneNumber); | ||
/** | ||
* Generate not assigned phone numbers | ||
* | ||
* @returns {() => fc.Arbitrary<string>} A random residential phone number | ||
*/ | ||
const unassignedPhone: () => fc.Arbitrary<string> = () => | ||
fc | ||
.oneof(fc.integer(10000000, 19999999), fc.integer(80000000, 99999999)) | ||
.map(maskPhoneNumber); | ||
|
||
describe('telephones', () => { | ||
describe('isMobilePhone', () => { | ||
test('should verify mobile phone numbers', () => | ||
fc.assert( | ||
fc.property(mobilePhone(), (phone) => isMobilePhoneNumber(phone)) | ||
)); | ||
|
||
test('should returns false for everything else', () => | ||
fc.assert( | ||
fc.property( | ||
fc.oneof(fc.anything(), fc.string(9)), | ||
(phone) => !isMobilePhoneNumber(phone as any) | ||
) | ||
)); | ||
}); | ||
|
||
describe('isResidentialPhone', () => { | ||
test('should verify residential phone numbers', () => | ||
fc.assert( | ||
fc.property(residentialPhone(), (phone) => | ||
isResidentialPhoneNumber(phone) | ||
) | ||
)); | ||
|
||
test('should returns false for everything else', () => | ||
fc.assert( | ||
fc.property( | ||
fc.oneof(fc.anything(), fc.string(9)), | ||
(phone) => !isResidentialPhoneNumber(phone as any) | ||
) | ||
)); | ||
}); | ||
|
||
describe('isPhoneNumber', () => { | ||
test('should verify phone numbers', () => | ||
fc.assert( | ||
fc.property(fc.oneof(mobilePhone(), residentialPhone()), (phone) => | ||
isPhoneNumber(phone) | ||
) | ||
)); | ||
|
||
test('should returns false for everything else', () => | ||
fc.assert( | ||
fc.property( | ||
fc.oneof(unassignedPhone()), | ||
(phone) => !isPhoneNumber(phone) | ||
) | ||
)); | ||
}); | ||
}); |
This file contains 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,42 @@ | ||
/** | ||
* Beginning with 6 or 7 follow by three digits, then a dash and ends with four digits only | ||
*/ | ||
const mobilePhoneRegExP = /^[67]\d{3}-\d{4}$/; | ||
/** | ||
* Beginning with 2 follow by three digits, then a dash and ends with four digits only | ||
*/ | ||
const residentialPhoneRegExP = /^2\d{3}-\d{4}$/; | ||
|
||
/** | ||
* Verifies that given string number is a mobile number | ||
* | ||
* @param {string} phone A string phone number with a dash included | ||
* @returns {boolean} Is a valid mobile phone number | ||
*/ | ||
export function isMobilePhoneNumber(phone: string): boolean { | ||
if (typeof phone !== 'string') return false; | ||
|
||
return mobilePhoneRegExP.test(phone); | ||
} | ||
|
||
/** | ||
* Verifies that given string number is a residential number | ||
* | ||
* @param {string} phone A string phone number with a dash included | ||
* @returns {boolean} Is a valid residential phone number | ||
*/ | ||
export function isResidentialPhoneNumber(phone: string): boolean { | ||
if (typeof phone !== 'string') return false; | ||
|
||
return residentialPhoneRegExP.test(phone); | ||
} | ||
|
||
/** | ||
* Verifies that given string number is a phone number | ||
* | ||
* @param {string} phone A string phone number with a dash included | ||
* @returns {boolean} Is a valid phone number | ||
*/ | ||
export function isPhoneNumber(phone: string): boolean { | ||
return isMobilePhoneNumber(phone) || isResidentialPhoneNumber(phone); | ||
} |