Skip to content

Commit c70536c

Browse files
authored
Release v2.3.0 (#98)
* bumped version * style fixes * migrated node v10 -> node v13 * updated dependencies * updated contributors * build project's files * Added new flags: "isSupportedCountry" and "isValidFormat" * improved unit tests * fixed test description * (wip) introduced new tests * fixes for the tests * fixes for the tests * fixes for the tests * fixed README.md
1 parent 54bcd61 commit c70536c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+1082
-722
lines changed

.nvmrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v10.3.0
1+
v13.13.0

.travis.yml

+2
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@ language: node_js
22
node_js:
33
- '10'
44
- '11'
5+
- '12'
6+
- '13'
57
notifications:
68
email: false

README.md

+18-10
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,26 @@ What is it?
2121

2222
Small library to check validity VAT numbers (European + some others counties). ([learn more][1] about VAT)
2323

24-
- no dependencies;
25-
- no http calls;
24+
- No dependencies;
25+
- No http calls;
2626
- 2-step checks: math + regexp;
27-
- tree-shakeable;
28-
- extendable;
29-
- dynamically add/remove countries with which you want to check the VAT;
30-
- written in `typescript`;
27+
- Tree-shakeable;
28+
- Extendable;
29+
- Separate checks for valid VAT and valid VAT format;
30+
- Dynamically add/remove countries with which you want to check the VAT;
31+
- Detecting possible country before you finish;
32+
- Typescript;
3133

3234
Installation
3335
----------
3436

3537
Installation:
3638

37-
`npm i jsvat --save` or `yarn add jsvat`
39+
```bash
40+
npm i jsvat --save
41+
```
42+
43+
(or `yarn add jsvat`)
3844

3945
For legacy versions (below v2.0.0) also possible: Bower: `bower i jsvat --save`
4046

@@ -52,14 +58,16 @@ Getting Started
5258
Return value
5359
---------
5460

55-
`checkVAT()` returns a `Result` Object:
61+
`checkVAT()` returns `VatCheckResult` object:
5662

5763
```typescript
5864

5965
export interface VatCheckResult {
6066
value?: string; // 'BE0411905847': your VAT without extra characters (like '-', spaces, etc)
61-
isValid: boolean; // main result - is VAT correct against provided countries or not
62-
country?: { // VAT's couuntry (null if not found)
67+
isValid: boolean; // The main result. Indicates if VAT is correct against provided countries or not
68+
isValidFormat: boolean; // Indicates the validation of the format of VAT only. E.g. "BE0411905847" is a valid VAT, and "BE0897221791" is not. But they both has valid format, so "isValidFormat" will return "true"
69+
isSupportedCountry: boolean; // Indicates if "jsvat" could recognize the VAT. Sometimes you want to understand - if it's an invalid VAT from supported country or from an unknown one
70+
country?: { // VAT's country (null if not found). By "supported" I mean imported.
6371
name: string; // ISO country name of VAT
6472
isoCode: { // Country ISO codes
6573
short: string;

lib/amd/lib/jsvat.js

+16-26
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,38 @@ define(["require", "exports"], function (require, exports) {
55
return {
66
value: vat || undefined,
77
isValid: Boolean(isValid),
8+
isValidFormat: country ? isVatValidToRegexp(vat, country.rules.regex).isValid : false,
9+
isSupportedCountry: Boolean(country),
810
country: (!country) ? undefined : {
911
name: country.name,
1012
isoCode: {
1113
short: country.codes[0],
1214
long: country.codes[1],
1315
numeric: country.codes[2]
14-
},
15-
formatValid: country.formatValid,
16+
}
1617
}
1718
};
1819
}
1920
function removeExtraChars(vat = '') {
2021
return vat.toString().toUpperCase().replace(/(\s|-|\.)+/g, '');
2122
}
22-
function getCountryCode(country) {
23-
if (country.name === 'Greece') {
24-
return 'EL';
25-
}
26-
else {
27-
return country.codes[0];
28-
}
23+
function getCountryCodes(country) {
24+
return ([
25+
...country.codes,
26+
(country.name === 'Greece') ? 'EL' : undefined
27+
]).filter(Boolean);
2928
}
3029
function getCountry(vat, countriesList) {
3130
for (const country of countriesList) {
32-
const countryCode = getCountryCode(country);
33-
if (vat.startsWith(countryCode)) {
34-
const formatValid = isVatValidToRegexp(vat, country.rules.regex);
35-
return {
36-
...country,
37-
formatValid: formatValid.isValid
38-
};
39-
}
31+
if (startsWithCode(vat, country))
32+
return { ...country };
4033
}
4134
return undefined;
4235
}
36+
function startsWithCode(vat, country) {
37+
const countryCodes = getCountryCodes(country);
38+
return countryCodes.filter(code => vat.startsWith(code)).length > 0;
39+
}
4340
function isVatValidToRegexp(vat, regexArr) {
4441
for (const regex of regexArr) {
4542
const isValid = regex.test(vat);
@@ -61,16 +58,9 @@ define(["require", "exports"], function (require, exports) {
6158
if (!vat)
6259
return makeResult(vat, false);
6360
const cleanVAT = removeExtraChars(vat);
64-
const result = makeResult(cleanVAT);
6561
const country = getCountry(cleanVAT, countriesList);
66-
if (!country)
67-
return result;
68-
if (!country.formatValid)
69-
return makeResult(cleanVAT, country.formatValid, country);
70-
const isValid = isVatValid(cleanVAT, country);
71-
if (isValid)
72-
return makeResult(cleanVAT, isValid, country);
73-
return result;
62+
const isValid = (country) ? isVatValid(cleanVAT, country) : false;
63+
return makeResult(cleanVAT, isValid, country);
7464
}
7565
exports.checkVAT = checkVAT;
7666
});

lib/commonjs/lib/jsvat.js

+22-23
Original file line numberDiff line numberDiff line change
@@ -10,45 +10,51 @@ var __assign = (this && this.__assign) || function () {
1010
};
1111
return __assign.apply(this, arguments);
1212
};
13+
var __spreadArrays = (this && this.__spreadArrays) || function () {
14+
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
15+
for (var r = Array(s), k = 0, i = 0; i < il; i++)
16+
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
17+
r[k] = a[j];
18+
return r;
19+
};
1320
Object.defineProperty(exports, "__esModule", { value: true });
1421
function makeResult(vat, isValid, country) {
1522
return {
1623
value: vat || undefined,
1724
isValid: Boolean(isValid),
25+
isValidFormat: country ? isVatValidToRegexp(vat, country.rules.regex).isValid : false,
26+
isSupportedCountry: Boolean(country),
1827
country: (!country) ? undefined : {
1928
name: country.name,
2029
isoCode: {
2130
short: country.codes[0],
2231
long: country.codes[1],
2332
numeric: country.codes[2]
24-
},
25-
formatValid: country.formatValid,
33+
}
2634
}
2735
};
2836
}
2937
function removeExtraChars(vat) {
3038
if (vat === void 0) { vat = ''; }
3139
return vat.toString().toUpperCase().replace(/(\s|-|\.)+/g, '');
3240
}
33-
function getCountryCode(country) {
34-
if (country.name === 'Greece') {
35-
return 'EL';
36-
}
37-
else {
38-
return country.codes[0];
39-
}
41+
function getCountryCodes(country) {
42+
return (__spreadArrays(country.codes, [
43+
(country.name === 'Greece') ? 'EL' : undefined
44+
])).filter(Boolean);
4045
}
4146
function getCountry(vat, countriesList) {
4247
for (var _i = 0, countriesList_1 = countriesList; _i < countriesList_1.length; _i++) {
4348
var country = countriesList_1[_i];
44-
var countryCode = getCountryCode(country);
45-
if (vat.startsWith(countryCode)) {
46-
var formatValid = isVatValidToRegexp(vat, country.rules.regex);
47-
return __assign(__assign({}, country), { formatValid: formatValid.isValid });
48-
}
49+
if (startsWithCode(vat, country))
50+
return __assign({}, country);
4951
}
5052
return undefined;
5153
}
54+
function startsWithCode(vat, country) {
55+
var countryCodes = getCountryCodes(country);
56+
return countryCodes.filter(function (code) { return vat.startsWith(code); }).length > 0;
57+
}
5258
function isVatValidToRegexp(vat, regexArr) {
5359
for (var _i = 0, regexArr_1 = regexArr; _i < regexArr_1.length; _i++) {
5460
var regex = regexArr_1[_i];
@@ -72,15 +78,8 @@ function checkVAT(vat, countriesList) {
7278
if (!vat)
7379
return makeResult(vat, false);
7480
var cleanVAT = removeExtraChars(vat);
75-
var result = makeResult(cleanVAT);
7681
var country = getCountry(cleanVAT, countriesList);
77-
if (!country)
78-
return result;
79-
if (!country.formatValid)
80-
return makeResult(cleanVAT, country.formatValid, country);
81-
var isValid = isVatValid(cleanVAT, country);
82-
if (isValid)
83-
return makeResult(cleanVAT, isValid, country);
84-
return result;
82+
var isValid = (country) ? isVatValid(cleanVAT, country) : false;
83+
return makeResult(cleanVAT, isValid, country);
8584
}
8685
exports.checkVAT = checkVAT;

lib/es6/lib/jsvat.js

+16-26
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,38 @@ function makeResult(vat, isValid, country) {
22
return {
33
value: vat || undefined,
44
isValid: Boolean(isValid),
5+
isValidFormat: country ? isVatValidToRegexp(vat, country.rules.regex).isValid : false,
6+
isSupportedCountry: Boolean(country),
57
country: (!country) ? undefined : {
68
name: country.name,
79
isoCode: {
810
short: country.codes[0],
911
long: country.codes[1],
1012
numeric: country.codes[2]
11-
},
12-
formatValid: country.formatValid,
13+
}
1314
}
1415
};
1516
}
1617
function removeExtraChars(vat = '') {
1718
return vat.toString().toUpperCase().replace(/(\s|-|\.)+/g, '');
1819
}
19-
function getCountryCode(country) {
20-
if (country.name === 'Greece') {
21-
return 'EL';
22-
}
23-
else {
24-
return country.codes[0];
25-
}
20+
function getCountryCodes(country) {
21+
return ([
22+
...country.codes,
23+
(country.name === 'Greece') ? 'EL' : undefined
24+
]).filter(Boolean);
2625
}
2726
function getCountry(vat, countriesList) {
2827
for (const country of countriesList) {
29-
const countryCode = getCountryCode(country);
30-
if (vat.startsWith(countryCode)) {
31-
const formatValid = isVatValidToRegexp(vat, country.rules.regex);
32-
return {
33-
...country,
34-
formatValid: formatValid.isValid
35-
};
36-
}
28+
if (startsWithCode(vat, country))
29+
return { ...country };
3730
}
3831
return undefined;
3932
}
33+
function startsWithCode(vat, country) {
34+
const countryCodes = getCountryCodes(country);
35+
return countryCodes.filter(code => vat.startsWith(code)).length > 0;
36+
}
4037
function isVatValidToRegexp(vat, regexArr) {
4138
for (const regex of regexArr) {
4239
const isValid = regex.test(vat);
@@ -58,14 +55,7 @@ export function checkVAT(vat, countriesList = []) {
5855
if (!vat)
5956
return makeResult(vat, false);
6057
const cleanVAT = removeExtraChars(vat);
61-
const result = makeResult(cleanVAT);
6258
const country = getCountry(cleanVAT, countriesList);
63-
if (!country)
64-
return result;
65-
if (!country.formatValid)
66-
return makeResult(cleanVAT, country.formatValid, country);
67-
const isValid = isVatValid(cleanVAT, country);
68-
if (isValid)
69-
return makeResult(cleanVAT, isValid, country);
70-
return result;
59+
const isValid = (country) ? isVatValid(cleanVAT, country) : false;
60+
return makeResult(cleanVAT, isValid, country);
7161
}

lib/system/lib/jsvat.js

+16-26
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,38 @@ System.register([], function (exports_1, context_1) {
55
return {
66
value: vat || undefined,
77
isValid: Boolean(isValid),
8+
isValidFormat: country ? isVatValidToRegexp(vat, country.rules.regex).isValid : false,
9+
isSupportedCountry: Boolean(country),
810
country: (!country) ? undefined : {
911
name: country.name,
1012
isoCode: {
1113
short: country.codes[0],
1214
long: country.codes[1],
1315
numeric: country.codes[2]
14-
},
15-
formatValid: country.formatValid,
16+
}
1617
}
1718
};
1819
}
1920
function removeExtraChars(vat = '') {
2021
return vat.toString().toUpperCase().replace(/(\s|-|\.)+/g, '');
2122
}
22-
function getCountryCode(country) {
23-
if (country.name === 'Greece') {
24-
return 'EL';
25-
}
26-
else {
27-
return country.codes[0];
28-
}
23+
function getCountryCodes(country) {
24+
return ([
25+
...country.codes,
26+
(country.name === 'Greece') ? 'EL' : undefined
27+
]).filter(Boolean);
2928
}
3029
function getCountry(vat, countriesList) {
3130
for (const country of countriesList) {
32-
const countryCode = getCountryCode(country);
33-
if (vat.startsWith(countryCode)) {
34-
const formatValid = isVatValidToRegexp(vat, country.rules.regex);
35-
return {
36-
...country,
37-
formatValid: formatValid.isValid
38-
};
39-
}
31+
if (startsWithCode(vat, country))
32+
return { ...country };
4033
}
4134
return undefined;
4235
}
36+
function startsWithCode(vat, country) {
37+
const countryCodes = getCountryCodes(country);
38+
return countryCodes.filter(code => vat.startsWith(code)).length > 0;
39+
}
4340
function isVatValidToRegexp(vat, regexArr) {
4441
for (const regex of regexArr) {
4542
const isValid = regex.test(vat);
@@ -61,16 +58,9 @@ System.register([], function (exports_1, context_1) {
6158
if (!vat)
6259
return makeResult(vat, false);
6360
const cleanVAT = removeExtraChars(vat);
64-
const result = makeResult(cleanVAT);
6561
const country = getCountry(cleanVAT, countriesList);
66-
if (!country)
67-
return result;
68-
if (!country.formatValid)
69-
return makeResult(cleanVAT, country.formatValid, country);
70-
const isValid = isVatValid(cleanVAT, country);
71-
if (isValid)
72-
return makeResult(cleanVAT, isValid, country);
73-
return result;
62+
const isValid = (country) ? isVatValid(cleanVAT, country) : false;
63+
return makeResult(cleanVAT, isValid, country);
7464
}
7565
exports_1("checkVAT", checkVAT);
7666
return {

0 commit comments

Comments
 (0)