Skip to content

Commit

Permalink
Release/2.4.0 (#103)
Browse files Browse the repository at this point in the history
* checkstyle

* fixed version

* bumped version

* build

* update deps

* added tests

* fixed export of all countries and andorra

* updated readme
  • Loading branch information
se-panfilov authored Apr 22, 2020
1 parent 4757136 commit 8bef79a
Show file tree
Hide file tree
Showing 22 changed files with 581 additions and 653 deletions.
207 changes: 103 additions & 104 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@
[![NPM](https://nodei.co/npm/jsvat.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/jsvat/)
[![Package Quality](http://npm.packagequality.com/badge/jsvat.png)](http://packagequality.com/#?package=jsvat)

jsvat
-------
## jsvat

[[Demo and Examples]][2]

Check the validity of the format of an EU VAT number. No dependencies.

What is it?
--------
## What is it?

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

Expand All @@ -31,92 +29,99 @@ Small library to check validity VAT numbers (European + some others counties). (
- Detecting possible country before you finish;
- Typescript;

Installation
----------
## Installation

Installation:

```bash
npm i jsvat --save
```
```bash
npm i jsvat --save
```

(or `yarn add jsvat`)

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

Getting Started
----------
## Getting Started

```javascript
import { checkVAT, belgium, austria } from 'jsvat';

checkVAT('BE0411905847', [belgium]); // true: accept only Belgium VATs
checkVAT('BE0411905847', [belgium, austria]); // true: accept only Belgium or Austria VATs
checkVAT('BE0411905847', [austria]); // false: accept only Austria VATs
```

or

```javascript
import { checkVAT, belgium, austria } from 'jsvat';

checkVAT('BE0411905847', [belgium]); // true: accept only Belgium VATs
checkVAT('BE0411905847', [belgium, austria]); // true: accept only Belgium or Austria VATs
checkVAT('BE0411905847', [austria]); // false: accept only Austria VATs
```
```javascript
import { checkVAT, countries } from 'jsvat';
('countries');
checkVAT('BE0411905847', countries); // check against all supported countries
```

to check against all supported countries

## Return value

Return value
---------

`checkVAT()` returns `VatCheckResult` object:

```typescript

export interface VatCheckResult {
value?: string; // 'BE0411905847': your VAT without extra characters (like '-', spaces, etc)
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;
long: string;
numeric: string;
};
value?: string; // 'BE0411905847': your VAT without extra characters (like '-', spaces, etc)
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;
long: string;
numeric: string;
};
};
}
```

List of supported Countries:
---------

- Austria
- Belgium
- Bulgaria
- Switzerland
- Cyprus
- Czech Republic
- Germany
- Denmark
- Greece
- Spain
- Europe
- Estonia
- Finland
- France
- United Kingdom
- Croatia
- Hungary
- Ireland
- Italy
- Latvia
- Lithuania
- Luxembourg
- Malta
- Netherlands
- Norway
- Poland
- Portugal
- Romania
- Russia Federation
- Serbia
- Slovenia
- Slovakia republic
- Sweden

How to import all countries at once?
----------
## List of supported Countries:

- Andorra
- Austria
- Belgium
- Bulgaria
- Switzerland
- Cyprus
- Czech Republic
- Germany
- Denmark
- Greece
- Spain
- Europe
- Estonia
- Finland
- France
- United Kingdom
- Croatia
- Hungary
- Ireland
- Italy
- Latvia
- Lithuania
- Luxembourg
- Malta
- Netherlands
- Norway
- Poland
- Portugal
- Romania
- Russia Federation
- Serbia
- Slovenia
- Slovakia republic
- Sweden

## How to import all countries at once?

```javascript
import { checkVAT, countries } from 'jsvat';
Expand All @@ -125,23 +130,23 @@ import { checkVAT, countries } from 'jsvat';
checkVAT('WD12345678', countries);
```

Extend countries list - add your own country:
----------
## Extend countries list - add your own country:

You can add your own country.
In general `Country` should implement following structure:

```typescript
interface Country {
name: string;
codes: ReadonlyArray<string>;
calcFn: (vat: string, options?: object) => boolean; //options - isn't a mandatory param
rules: {
multipliers: {}; // you can leave it empty
regex: ReadonlyArray<RegExp>;
};
name: string;
codes: ReadonlyArray<string>;
calcFn: (vat: string, options?: object) => boolean; //options - isn't a mandatory param
rules: {
multipliers: {}; // you can leave it empty
regex: ReadonlyArray<RegExp>;
};
}
```

Example:

```javascript
Expand All @@ -159,33 +164,31 @@ export const wonderland = {
};

checkVAT('WD12345678', [wonderland]); // true

```

About modules... ES6 / CommonJS / AMD / UMD / System
----------
## About modules... ES6 / CommonJS / AMD / UMD / System

jsvat build includes `es6`, `commonjs`, `amd`, `umd` and `system` builds at the same time.
jsvat build includes `es6`, `commonjs`, `amd`, `umd` and `system` builds at the same time.

By default you will stick to `es6` version for browsers and build tools (webpack, etc):
which expects you to import it as
which expects you to import it as

```javascript
import { checkVAT, belgium, austria } from 'jsvat';
````
```

Node.js automatically will pick up `CommonJS` version by default.
Means you could import it like:

```jsx harmony
// Modern Frontend and Node
const { checkVAT, belgium, austria } = require('jsvat');
const { checkVAT, belgium, austria } = require('jsvat');

// Node.js
// Node.js
const { checkVAT, belgium, austria } = require('jsvat');

// Legacy Frontend
<script src="whatever/jsvat/lib/umd/index.js"></script>
<script src="whatever/jsvat/lib/umd/index.js"></script>;
```

Alternatively you can specify which module system you do want, e.g.:
Expand All @@ -198,7 +201,7 @@ const { checkVAT, belgium, austria } = require('jsvat/lib/commonjs');
import { checkVAT, belgium, austria } from 'jsvat/lib/es6';

// UMD
<script src="whatever/jsvat/lib/umd/index.js"></script>
<script src="whatever/jsvat/lib/umd/index.js"></script>;

// AMD
const { checkVAT, belgium, austria } = require('jsvat/lib/amd');
Expand All @@ -207,36 +210,32 @@ const { checkVAT, belgium, austria } = require('jsvat/lib/amd');
import { checkVAT, belgium, austria } from 'jsvat/lib/system';
```

How jsvat checks validity?
---------
## How jsvat checks validity?

There is 2-step check:

1. Compare with list of Regexps;

For example regexp for austria is `/^(AT)U(\d{8})$/`.
For example regexp for austria is `/^(AT)U(\d{8})$/`.

Looks like `ATU99999999` is valid (it's satisfy the regexp), but actually it's should be invalid.
Looks like `ATU99999999` is valid (it's satisfy the regexp), but actually it's should be invalid.

2. Some magic mathematical counting;

Here we make some mathematical calculation (different for each country).
After that we may be sure that `ATU99999999`and for example `ATV66889218` isn't valid, but `ATU12011204` is valid.
Here we make some mathematical calculation (different for each country).
After that we may be sure that `ATU99999999`and for example `ATV66889218` isn't valid, but `ATU12011204` is valid.

NOTE:
VAT numbers of some countries should ends up with special characters. Like '01' for Sweden or "L" for Cyprus.
If 100% real VAT doesn't fit, try to add proper appendix.


Browsers Supports
---------
## Browsers Supports

Support only of evergreen browsers.

Legacy versions (below v2.0.0) supports all browsers down to IE9 (including IE9).

LICENSE
-------
## LICENSE

The MIT License (MIT)

Expand All @@ -260,5 +259,5 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

[1]: https://en.wikipedia.org/wiki/VAT_identification_number
[2]: https://se-panfilov.github.io/jsvat
[1]: https://en.wikipedia.org/wiki/VAT_identification_number
[2]: https://se-panfilov.github.io/jsvat
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ export { spain } from './lib/es6/lib/countries/spain';
export { sweden } from './lib/es6/lib/countries/sweden';
export { switzerland } from './lib/es6/lib/countries/switzerland';
export { unitedKingdom } from './lib/es6/lib/countries/unitedKingdom';
export { countries } from './lib/es6';

export { checkVAT } from './lib/es6';
export { checkVAT } from './lib/es6';
3 changes: 1 addition & 2 deletions lib/amd/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
define(["require", "exports", "./lib/countries", "./lib/countries", "./lib/jsvat"], function (require, exports, countries_1, countries_2, jsvat_1) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const countries = [
exports.countries = [
countries_1.andorra,
countries_1.austria,
countries_1.belgium,
Expand Down Expand Up @@ -37,7 +37,6 @@ define(["require", "exports", "./lib/countries", "./lib/countries", "./lib/jsvat
countries_1.switzerland,
countries_1.unitedKingdom
];
exports.countries = countries;
exports.andorra = countries_2.andorra;
exports.austria = countries_2.austria;
exports.belgium = countries_2.belgium;
Expand Down
19 changes: 10 additions & 9 deletions lib/amd/lib/countries/andorra.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ define(["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.andorra = {
name: 'Andorra',
codes: ['AD', 'AND', '020'],
calcFn: (vat) => {
return vat.length === 8;
},
rules: {
regex: [/^(AD)([fealecdgopuFEALECDGOPU]{1}\d{6}[fealecdgopuFEALECDGOPU]{1})$/]
}
name: 'Andorra',
codes: ['AD', 'AND', '020'],
calcFn: (vat) => {
return vat.length === 8;
},
rules: {
multipliers: {},
regex: [/^(AD)([fealecdgopuFEALECDGOPU]{1}\d{6}[fealecdgopuFEALECDGOPU]{1})$/]
}
};
});
});
4 changes: 2 additions & 2 deletions lib/amd/lib/countries/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
define(["require", "exports", "./austria", "./belgium", "./bulgaria", "./croatiat", "./cyprus", "./czechRepublic", "./denmark", "./estonia", "./europe", "./finland", "./france", "./germany", "./greece", "./hungary", "./ireland", "./italy", "./latvia", "./lithuania", "./luxembourg", "./malta", "./netherlands", "./norway", "./poland", "./portugal", "./romania", "./russia", "./serbia", "./slovakiaRepublic", "./slovenia", "./spain", "./sweden", "./switzerland", "./unitedKingdom"], function (require, exports, austria_1, belgium_1, bulgaria_1, croatiat_1, cyprus_1, czechRepublic_1, denmark_1, estonia_1, europe_1, finland_1, france_1, germany_1, greece_1, hungary_1, ireland_1, italy_1, latvia_1, lithuania_1, luxembourg_1, malta_1, netherlands_1, norway_1, poland_1, portugal_1, romania_1, russia_1, serbia_1, slovakiaRepublic_1, slovenia_1, spain_1, sweden_1, switzerland_1, unitedKingdom_1) {
define(["require", "exports", "./andorra", "./austria", "./belgium", "./bulgaria", "./croatiat", "./cyprus", "./czechRepublic", "./denmark", "./estonia", "./europe", "./finland", "./france", "./germany", "./greece", "./hungary", "./ireland", "./italy", "./latvia", "./lithuania", "./luxembourg", "./malta", "./netherlands", "./norway", "./poland", "./portugal", "./romania", "./russia", "./serbia", "./slovakiaRepublic", "./slovenia", "./spain", "./sweden", "./switzerland", "./unitedKingdom"], function (require, exports, andorra_1, austria_1, belgium_1, bulgaria_1, croatiat_1, cyprus_1, czechRepublic_1, denmark_1, estonia_1, europe_1, finland_1, france_1, germany_1, greece_1, hungary_1, ireland_1, italy_1, latvia_1, lithuania_1, luxembourg_1, malta_1, netherlands_1, norway_1, poland_1, portugal_1, romania_1, russia_1, serbia_1, slovakiaRepublic_1, slovenia_1, spain_1, sweden_1, switzerland_1, unitedKingdom_1) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.andorra = austria_1.andorra;
exports.andorra = andorra_1.andorra;
exports.austria = austria_1.austria;
exports.belgium = belgium_1.belgium;
exports.bulgaria = bulgaria_1.bulgaria;
Expand Down
3 changes: 1 addition & 2 deletions lib/commonjs/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var countries_1 = require("./lib/countries");
var countries = [
exports.countries = [
countries_1.andorra,
countries_1.austria,
countries_1.belgium,
Expand Down Expand Up @@ -37,7 +37,6 @@ var countries = [
countries_1.switzerland,
countries_1.unitedKingdom
];
exports.countries = countries;
var countries_2 = require("./lib/countries");
exports.andorra = countries_2.andorra;
exports.austria = countries_2.austria;
Expand Down
Loading

0 comments on commit 8bef79a

Please sign in to comment.