Skip to content

Commit

Permalink
Valid ISO 639-1 + ISO 3166-1 alpha 2 locale
Browse files Browse the repository at this point in the history
  • Loading branch information
boonya committed Nov 17, 2021
1 parent af0ea11 commit f8892f1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 45 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ Validator | Description
**isUppercase(str)** | check if the string is uppercase.
**isSlug** | Check if the string is of type slug. `Options` allow a single hyphen between string. e.g. [`cn-cn`, `cn-c-c`]
**isStrongPassword(str [, options])** | Check if a password is strong or not. Allows for custom requirements or scoring rules. If `returnScore` is true, then the function returns an integer score for the password rather than a boolean.<br/>Default options: <br/>`{ minLength: 8, minLowercase: 1, minUppercase: 1, minNumbers: 1, minSymbols: 1, returnScore: false, pointsPerUnique: 1, pointsPerRepeat: 0.5, pointsForContainingLower: 10, pointsForContainingUpper: 10, pointsForContainingNumber: 10, pointsForContainingSymbol: 10 }`
**isTaxID(str, locale)** | Check if the given value is a valid Tax Identification Number. Default locale is `en-US`.<br/><br/>More info about exact TIN support can be found in `src/lib/isTaxID.js`<br/><br/>Supported locales: `[ 'bg-BG', 'ca-CA', 'cs-CZ', 'de-AT', 'de-DE', 'dk-DK', 'el-CY', 'el-GR', 'en-GB', 'en-IE', 'en-US', 'es-ES', 'et-EE', 'fi-FI', 'fr-BE', 'fr-FR', 'fr-LU', 'hr-HR', 'hu-HU', 'it-IT', 'lb-LU', 'lt-LT', 'lv-LV' 'mt-MT', 'nl-BE', 'nl-NL', 'pl-PL', 'pt-BR', 'pt-PT', 'ro-RO', 'sk-SK', 'sl-SI', 'sv-SE' ]`
**isTaxID(str, locale)** | Check if the given value is a valid Tax Identification Number. Default locale is `en-US`.<br/><br/>More info about exact TIN support can be found in `src/lib/isTaxID.js`<br/><br/>Supported locales: `[ 'bg-BG', 'cs-CZ', 'de-AT', 'de-DE', 'dk-DK', 'el-CY', 'el-GR', 'en-CA', 'en-GB', 'en-IE', 'en-US', 'es-ES', 'et-EE', 'fi-FI', 'fr-BE', 'fr-CA', 'fr-FR', 'fr-LU', 'hr-HR', 'hu-HU', 'it-IT', 'lb-LU', 'lt-LT', 'lv-LV' 'mt-MT', 'nl-BE', 'nl-NL', 'pl-PL', 'pt-BR', 'pt-PT', 'ro-RO', 'sk-SK', 'sl-SI', 'sv-SE' ]`
**isURL(str [, options])** | check if the string is an URL.<br/><br/>`options` is an object which defaults to `{ protocols: ['http','https','ftp'], require_tld: true, require_protocol: false, require_host: true, require_port: false, require_valid_protocol: true, allow_underscores: false, host_whitelist: false, host_blacklist: false, allow_trailing_dot: false, allow_protocol_relative_urls: false, allow_fragments: true, allow_query_components: true, disallow_auth: false, validate_length: true }`.<br/><br/>require_protocol - if set as true isURL will return false if protocol is not present in the URL.<br/>require_valid_protocol - isURL will check if the URL's protocol is present in the protocols option.<br/>protocols - valid protocols can be modified with this option.<br/>require_host - if set as false isURL will not check if host is present in the URL.<br/>require_port - if set as true isURL will check if port is present in the URL.<br/>allow_protocol_relative_urls - if set as true protocol relative URLs will be allowed.<br/>allow_fragments - if set as false isURL will return false if fragments are present.<br/>allow_query_components - if set as false isURL will return false if query components are present.<br/>validate_length - if set as false isURL will skip string length validation (2083 characters is IE max URL length).
**isUUID(str [, version])** | check if the string is a UUID (version 1, 2, 3, 4 or 5).
**isVariableWidth(str)** | check if the string contains a mixture of full and half-width chars.
Expand Down
14 changes: 7 additions & 7 deletions src/lib/isTaxID.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ function bgBgCheck(tin) {
* https://www.canada.ca/en/employment-social-development/services/sin.html
* https://www.codercrunch.com/challenge/819302488/sin-validator
*
* @param {string} tin
* @param {string} input
* @return {boolean}
*/
function caCaCheck(tin) {
const digitsArray = tin.split('');
function isCanadianSIN(input) {
const digitsArray = input.split('');
const even = digitsArray
.filter((_, idx) => idx % 2)
.map(i => Number(i) * 2)
Expand Down Expand Up @@ -1127,13 +1127,13 @@ function svSeCheck(tin) {
*/
const taxIdFormat = {
'bg-BG': /^\d{10}$/,
'ca-CA': /^\d{9}$/,
'cs-CZ': /^\d{6}\/{0,1}\d{3,4}$/,
'de-AT': /^\d{9}$/,
'de-DE': /^[1-9]\d{10}$/,
'dk-DK': /^\d{6}-{0,1}\d{4}$/,
'el-CY': /^[09]\d{7}[A-Z]$/,
'el-GR': /^([0-4]|[7-9])\d{8}$/,
'en-CA': /^\d{9}$/,
'en-GB': /^\d{10}$|^(?!GB|NK|TN|ZZ)(?![DFIQUV])[A-Z](?![DFIQUVO])[A-Z]\d{6}[ABCD ]$/i,
'en-IE': /^\d{7}[A-W][A-IW]{0,1}$/i,
'en-US': /^\d{2}[- ]{0,1}\d{7}$/,
Expand All @@ -1156,23 +1156,23 @@ const taxIdFormat = {
'sk-SK': /^\d{6}\/{0,1}\d{3,4}$/,
'sl-SI': /^[1-9]\d{7}$/,
'sv-SE': /^(\d{6}[-+]{0,1}\d{4}|(18|19|20)\d{6}[-+]{0,1}\d{4})$/,

};
// taxIdFormat locale aliases
taxIdFormat['lb-LU'] = taxIdFormat['fr-LU'];
taxIdFormat['lt-LT'] = taxIdFormat['et-EE'];
taxIdFormat['nl-BE'] = taxIdFormat['fr-BE'];
taxIdFormat['fr-CA'] = taxIdFormat['en-CA'];

// Algorithmic tax id check functions for various locales
const taxIdCheck = {
'ca-CA': caCaCheck,
'bg-BG': bgBgCheck,
'cs-CZ': csCzCheck,
'de-AT': deAtCheck,
'de-DE': deDeCheck,
'dk-DK': dkDkCheck,
'el-CY': elCyCheck,
'el-GR': elGrCheck,
'en-CA': isCanadianSIN,
'en-IE': enIeCheck,
'en-US': enUsCheck,
'es-ES': esEsCheck,
Expand All @@ -1194,12 +1194,12 @@ const taxIdCheck = {
'sk-SK': skSkCheck,
'sl-SI': slSiCheck,
'sv-SE': svSeCheck,

};
// taxIdCheck locale aliases
taxIdCheck['lb-LU'] = taxIdCheck['fr-LU'];
taxIdCheck['lt-LT'] = taxIdCheck['et-EE'];
taxIdCheck['nl-BE'] = taxIdCheck['fr-BE'];
taxIdCheck['fr-CA'] = taxIdCheck['en-CA'];

// Regexes for locales where characters should be omitted before checking format
const allsymbols = /[-\\\/!@#$%\^&\*\(\)\+\=\[\]]+/g;
Expand Down
74 changes: 37 additions & 37 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -10717,43 +10717,6 @@ describe('Validators', () => {
'7521320010',
'7501010019'],
});
test({
validator: 'isTaxID',
args: ['ca-CA'],
valid: [
'000000000',
'521719666',
'469317481',
'120217450',
'480534858',
'325268597',
'336475660',
'744797853',
'130692544',
'046454286',
],
invalid: [
' ',
'any value',
'012345678',
'111111111',
'999999999',
'657449110',
'74 47 978 53',
'744 797 853',
'744-797-853',
'981062432',
'267500713',
'2675o0713',
'70597312',
'7058973122',
'069437151',
'046454281',
'146452286',
'30x92544',
'30692544',
],
});
test({
validator: 'isTaxID',
args: ['cs-CZ'],
Expand Down Expand Up @@ -10856,6 +10819,43 @@ describe('Validators', () => {
'658426713',
'558426713'],
});
test({
validator: 'isTaxID',
args: ['en-CA'],
valid: [
'000000000',
'521719666',
'469317481',
'120217450',
'480534858',
'325268597',
'336475660',
'744797853',
'130692544',
'046454286',
],
invalid: [
' ',
'any value',
'012345678',
'111111111',
'999999999',
'657449110',
'74 47 978 53',
'744 797 853',
'744-797-853',
'981062432',
'267500713',
'2675o0713',
'70597312',
'7058973122',
'069437151',
'046454281',
'146452286',
'30x92544',
'30692544',
],
});
test({
validator: 'isTaxID',
args: ['en-GB'],
Expand Down

0 comments on commit f8892f1

Please sign in to comment.