Skip to content

Commit 43a0f09

Browse files
arttigervfarylevych
and
vfarylevych
authored
feat(isTaxID): added TaxID for Ukraine uk-UA (#2358)
Co-authored-by: vfarylevych <[email protected]>
1 parent 0836777 commit 43a0f09

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ Validator | Description
158158
**isSlug(str)** | check if the string is of type slug.
159159
**isStrongPassword(str [, options])** | check if the string can be considered a strong password 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 }`
160160
**isTime(str [, options])** | check if the string is a valid time e.g. [`23:01:59`, new Date().toLocaleTimeString()].<br/><br/> `options` is an object which can contain the keys `hourFormat` or `mode`.<br/><br/>`hourFormat` is a key and defaults to `'hour24'`.<br/><br/>`mode` is a key and defaults to `'default'`. <br/><br/>`hourFomat` can contain the values `'hour12'` or `'hour24'`, `'hour24'` will validate hours in 24 format and `'hour12'` will validate hours in 12 format. <br/><br/>`mode` can contain the values `'default'` or `'withSeconds'`, `'default'` will validate `HH:MM` format, `'withSeconds'` will validate the `HH:MM:SS` format.
161-
**isTaxID(str, locale)** | check if the string 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-AR', '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' ]`.
161+
**isTaxID(str, locale)** | check if the string 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-AR', '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', 'uk-UA']`.
162162
**isURL(str [, options])** | check if the string is a 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 to 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 to false isURL will not check if host is present in the URL.<br/>`require_port` - if set to true isURL will check if port is present in the URL.<br/>`allow_protocol_relative_urls` - if set to true protocol relative URLs will be allowed.<br/>`allow_fragments` - if set to false isURL will return false if fragments are present.<br/>`allow_query_components` - if set to false isURL will return false if query components are present.<br/>`validate_length` - if set to false isURL will skip string length validation (2083 characters is IE max URL length).
163163
**isUUID(str [, version])** | check if the string is a UUID (version 1, 2, 3, 4 or 5).
164164
**isVariableWidth(str)** | check if the string contains a mixture of full and half-width chars.

src/lib/isTaxID.js

+17
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,21 @@ function svSeCheck(tin) {
11411141
return algorithms.luhnCheck(tin.replace(/\W/, ''));
11421142
}
11431143

1144+
/**
1145+
* uk-UA validation function
1146+
* Verify TIN validity by calculating check (last) digit (variant of MOD 11)
1147+
*/
1148+
function ukUaCheck(tin) {
1149+
// Calculate check digit
1150+
const digits = tin.split('').map(a => parseInt(a, 10));
1151+
const multipliers = [-1, 5, 7, 9, 4, 6, 10, 5, 7];
1152+
let checksum = 0;
1153+
for (let i = 0; i < multipliers.length; i++) {
1154+
checksum += digits[i] * multipliers[i];
1155+
}
1156+
return checksum % 11 === 10 ? digits[9] === 0 : digits[9] === checksum % 11;
1157+
}
1158+
11441159
// Locale lookup objects
11451160

11461161
/*
@@ -1181,6 +1196,7 @@ const taxIdFormat = {
11811196
'sk-SK': /^\d{6}\/{0,1}\d{3,4}$/,
11821197
'sl-SI': /^[1-9]\d{7}$/,
11831198
'sv-SE': /^(\d{6}[-+]{0,1}\d{4}|(18|19|20)\d{6}[-+]{0,1}\d{4})$/,
1199+
'uk-UA': /^\d{10}$/,
11841200
};
11851201
// taxIdFormat locale aliases
11861202
taxIdFormat['lb-LU'] = taxIdFormat['fr-LU'];
@@ -1220,6 +1236,7 @@ const taxIdCheck = {
12201236
'sk-SK': skSkCheck,
12211237
'sl-SI': slSiCheck,
12221238
'sv-SE': svSeCheck,
1239+
'uk-UA': ukUaCheck,
12231240
};
12241241
// taxIdCheck locale aliases
12251242
taxIdCheck['lb-LU'] = taxIdCheck['fr-LU'];

test/validators.test.js

+13
Original file line numberDiff line numberDiff line change
@@ -12956,6 +12956,19 @@ describe('Validators', () => {
1295612956
'19640823-32333',
1295712957
'1964082332333'],
1295812958
});
12959+
test({
12960+
validator: 'isTaxID',
12961+
args: ['uk-UA'],
12962+
valid: [
12963+
'3006321856',
12964+
'3003102490',
12965+
'2164212906'],
12966+
invalid: [
12967+
'2565975632',
12968+
'256597563287',
12969+
'КС00123456',
12970+
'2896235845'],
12971+
});
1295912972
test({
1296012973
validator: 'isTaxID',
1296112974
valid: [

0 commit comments

Comments
 (0)