Skip to content

Commit

Permalink
Release v2.3.0 (#98)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
se-panfilov authored Apr 15, 2020
1 parent 54bcd61 commit c70536c
Show file tree
Hide file tree
Showing 79 changed files with 1,082 additions and 722 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v10.3.0
v13.13.0
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ language: node_js
node_js:
- '10'
- '11'
- '12'
- '13'
notifications:
email: false
28 changes: 18 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,26 @@ What is it?

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

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

Installation
----------

Installation:

`npm i jsvat --save` or `yarn add jsvat`
```bash
npm i jsvat --save
```

(or `yarn add jsvat`)

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

Expand All @@ -52,14 +58,16 @@ Getting Started
Return value
---------
`checkVAT()` returns a `Result` Object:
`checkVAT()` returns `VatCheckResult` object:
```typescript
export interface VatCheckResult {
value?: string; // 'BE0411905847': your VAT without extra characters (like '-', spaces, etc)
isValid: boolean; // main result - is VAT correct against provided countries or not
country?: { // VAT's couuntry (null if not found)
isValid: boolean; // The main result. Indicates if VAT is correct against provided countries or not
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"
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
country?: { // VAT's country (null if not found). By "supported" I mean imported.
name: string; // ISO country name of VAT
isoCode: { // Country ISO codes
short: string;
Expand Down
42 changes: 16 additions & 26 deletions lib/amd/lib/jsvat.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,38 @@ define(["require", "exports"], function (require, exports) {
return {
value: vat || undefined,
isValid: Boolean(isValid),
isValidFormat: country ? isVatValidToRegexp(vat, country.rules.regex).isValid : false,
isSupportedCountry: Boolean(country),
country: (!country) ? undefined : {
name: country.name,
isoCode: {
short: country.codes[0],
long: country.codes[1],
numeric: country.codes[2]
},
formatValid: country.formatValid,
}
}
};
}
function removeExtraChars(vat = '') {
return vat.toString().toUpperCase().replace(/(\s|-|\.)+/g, '');
}
function getCountryCode(country) {
if (country.name === 'Greece') {
return 'EL';
}
else {
return country.codes[0];
}
function getCountryCodes(country) {
return ([
...country.codes,
(country.name === 'Greece') ? 'EL' : undefined
]).filter(Boolean);
}
function getCountry(vat, countriesList) {
for (const country of countriesList) {
const countryCode = getCountryCode(country);
if (vat.startsWith(countryCode)) {
const formatValid = isVatValidToRegexp(vat, country.rules.regex);
return {
...country,
formatValid: formatValid.isValid
};
}
if (startsWithCode(vat, country))
return { ...country };
}
return undefined;
}
function startsWithCode(vat, country) {
const countryCodes = getCountryCodes(country);
return countryCodes.filter(code => vat.startsWith(code)).length > 0;
}
function isVatValidToRegexp(vat, regexArr) {
for (const regex of regexArr) {
const isValid = regex.test(vat);
Expand All @@ -61,16 +58,9 @@ define(["require", "exports"], function (require, exports) {
if (!vat)
return makeResult(vat, false);
const cleanVAT = removeExtraChars(vat);
const result = makeResult(cleanVAT);
const country = getCountry(cleanVAT, countriesList);
if (!country)
return result;
if (!country.formatValid)
return makeResult(cleanVAT, country.formatValid, country);
const isValid = isVatValid(cleanVAT, country);
if (isValid)
return makeResult(cleanVAT, isValid, country);
return result;
const isValid = (country) ? isVatValid(cleanVAT, country) : false;
return makeResult(cleanVAT, isValid, country);
}
exports.checkVAT = checkVAT;
});
45 changes: 22 additions & 23 deletions lib/commonjs/lib/jsvat.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,51 @@ var __assign = (this && this.__assign) || function () {
};
return __assign.apply(this, arguments);
};
var __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
};
Object.defineProperty(exports, "__esModule", { value: true });
function makeResult(vat, isValid, country) {
return {
value: vat || undefined,
isValid: Boolean(isValid),
isValidFormat: country ? isVatValidToRegexp(vat, country.rules.regex).isValid : false,
isSupportedCountry: Boolean(country),
country: (!country) ? undefined : {
name: country.name,
isoCode: {
short: country.codes[0],
long: country.codes[1],
numeric: country.codes[2]
},
formatValid: country.formatValid,
}
}
};
}
function removeExtraChars(vat) {
if (vat === void 0) { vat = ''; }
return vat.toString().toUpperCase().replace(/(\s|-|\.)+/g, '');
}
function getCountryCode(country) {
if (country.name === 'Greece') {
return 'EL';
}
else {
return country.codes[0];
}
function getCountryCodes(country) {
return (__spreadArrays(country.codes, [
(country.name === 'Greece') ? 'EL' : undefined
])).filter(Boolean);
}
function getCountry(vat, countriesList) {
for (var _i = 0, countriesList_1 = countriesList; _i < countriesList_1.length; _i++) {
var country = countriesList_1[_i];
var countryCode = getCountryCode(country);
if (vat.startsWith(countryCode)) {
var formatValid = isVatValidToRegexp(vat, country.rules.regex);
return __assign(__assign({}, country), { formatValid: formatValid.isValid });
}
if (startsWithCode(vat, country))
return __assign({}, country);
}
return undefined;
}
function startsWithCode(vat, country) {
var countryCodes = getCountryCodes(country);
return countryCodes.filter(function (code) { return vat.startsWith(code); }).length > 0;
}
function isVatValidToRegexp(vat, regexArr) {
for (var _i = 0, regexArr_1 = regexArr; _i < regexArr_1.length; _i++) {
var regex = regexArr_1[_i];
Expand All @@ -72,15 +78,8 @@ function checkVAT(vat, countriesList) {
if (!vat)
return makeResult(vat, false);
var cleanVAT = removeExtraChars(vat);
var result = makeResult(cleanVAT);
var country = getCountry(cleanVAT, countriesList);
if (!country)
return result;
if (!country.formatValid)
return makeResult(cleanVAT, country.formatValid, country);
var isValid = isVatValid(cleanVAT, country);
if (isValid)
return makeResult(cleanVAT, isValid, country);
return result;
var isValid = (country) ? isVatValid(cleanVAT, country) : false;
return makeResult(cleanVAT, isValid, country);
}
exports.checkVAT = checkVAT;
42 changes: 16 additions & 26 deletions lib/es6/lib/jsvat.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,38 @@ function makeResult(vat, isValid, country) {
return {
value: vat || undefined,
isValid: Boolean(isValid),
isValidFormat: country ? isVatValidToRegexp(vat, country.rules.regex).isValid : false,
isSupportedCountry: Boolean(country),
country: (!country) ? undefined : {
name: country.name,
isoCode: {
short: country.codes[0],
long: country.codes[1],
numeric: country.codes[2]
},
formatValid: country.formatValid,
}
}
};
}
function removeExtraChars(vat = '') {
return vat.toString().toUpperCase().replace(/(\s|-|\.)+/g, '');
}
function getCountryCode(country) {
if (country.name === 'Greece') {
return 'EL';
}
else {
return country.codes[0];
}
function getCountryCodes(country) {
return ([
...country.codes,
(country.name === 'Greece') ? 'EL' : undefined
]).filter(Boolean);
}
function getCountry(vat, countriesList) {
for (const country of countriesList) {
const countryCode = getCountryCode(country);
if (vat.startsWith(countryCode)) {
const formatValid = isVatValidToRegexp(vat, country.rules.regex);
return {
...country,
formatValid: formatValid.isValid
};
}
if (startsWithCode(vat, country))
return { ...country };
}
return undefined;
}
function startsWithCode(vat, country) {
const countryCodes = getCountryCodes(country);
return countryCodes.filter(code => vat.startsWith(code)).length > 0;
}
function isVatValidToRegexp(vat, regexArr) {
for (const regex of regexArr) {
const isValid = regex.test(vat);
Expand All @@ -58,14 +55,7 @@ export function checkVAT(vat, countriesList = []) {
if (!vat)
return makeResult(vat, false);
const cleanVAT = removeExtraChars(vat);
const result = makeResult(cleanVAT);
const country = getCountry(cleanVAT, countriesList);
if (!country)
return result;
if (!country.formatValid)
return makeResult(cleanVAT, country.formatValid, country);
const isValid = isVatValid(cleanVAT, country);
if (isValid)
return makeResult(cleanVAT, isValid, country);
return result;
const isValid = (country) ? isVatValid(cleanVAT, country) : false;
return makeResult(cleanVAT, isValid, country);
}
42 changes: 16 additions & 26 deletions lib/system/lib/jsvat.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,38 @@ System.register([], function (exports_1, context_1) {
return {
value: vat || undefined,
isValid: Boolean(isValid),
isValidFormat: country ? isVatValidToRegexp(vat, country.rules.regex).isValid : false,
isSupportedCountry: Boolean(country),
country: (!country) ? undefined : {
name: country.name,
isoCode: {
short: country.codes[0],
long: country.codes[1],
numeric: country.codes[2]
},
formatValid: country.formatValid,
}
}
};
}
function removeExtraChars(vat = '') {
return vat.toString().toUpperCase().replace(/(\s|-|\.)+/g, '');
}
function getCountryCode(country) {
if (country.name === 'Greece') {
return 'EL';
}
else {
return country.codes[0];
}
function getCountryCodes(country) {
return ([
...country.codes,
(country.name === 'Greece') ? 'EL' : undefined
]).filter(Boolean);
}
function getCountry(vat, countriesList) {
for (const country of countriesList) {
const countryCode = getCountryCode(country);
if (vat.startsWith(countryCode)) {
const formatValid = isVatValidToRegexp(vat, country.rules.regex);
return {
...country,
formatValid: formatValid.isValid
};
}
if (startsWithCode(vat, country))
return { ...country };
}
return undefined;
}
function startsWithCode(vat, country) {
const countryCodes = getCountryCodes(country);
return countryCodes.filter(code => vat.startsWith(code)).length > 0;
}
function isVatValidToRegexp(vat, regexArr) {
for (const regex of regexArr) {
const isValid = regex.test(vat);
Expand All @@ -61,16 +58,9 @@ System.register([], function (exports_1, context_1) {
if (!vat)
return makeResult(vat, false);
const cleanVAT = removeExtraChars(vat);
const result = makeResult(cleanVAT);
const country = getCountry(cleanVAT, countriesList);
if (!country)
return result;
if (!country.formatValid)
return makeResult(cleanVAT, country.formatValid, country);
const isValid = isVatValid(cleanVAT, country);
if (isValid)
return makeResult(cleanVAT, isValid, country);
return result;
const isValid = (country) ? isVatValid(cleanVAT, country) : false;
return makeResult(cleanVAT, isValid, country);
}
exports_1("checkVAT", checkVAT);
return {
Expand Down
Loading

0 comments on commit c70536c

Please sign in to comment.