@accountable/jsvat

中文文档
1.3.4发布于 2024/07/09MITTypeScriptESM + CJS零依赖近 30 天下载 1.9万

Check the validity of the format of a VAT number

原始使用文档
文档目录9 节
  1. Disclaimer
  2. What is it?
  3. Installation
  4. Getting Started
  5. Return value
  6. List of supported Countries:
  7. Extend countries list - add your own country
  8. About modules... ES6 / CommonJS
  9. How @accountable/jsvat checks validity?

@accountable/jsvat

Check the validity of the format of a VAT number. No dependencies

npm PRs Welcome GitHub license

Disclaimer

This library is a fork of jsvat which in no longer maintained and all new pull requests to it are ignored for long time.

So in Accountable, we thought it would be great to add support for the new VAT number format and keep the library up to date and easily maintained as it is part of our day to day work.

What is it?

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

  • Rewritten into Typescript :new:
  • No dependencies
  • No http calls
  • 2-step checks: math + regexp
  • 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

via npm:

npm i @accountable/jsvat

via yarn:

yarn add @accountable/jsvat

Getting Started

  • check against specific countries
import { checkVAT, belgium, austria } from '@accountable/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
  • check against all supported countries
import { checkVAT } from '@accountable/jsvat';

checkVAT('BE0411905847'); // no need to pass countries as all supported countries will be used as default behavior
  • check against all supported countries except specific ones
import { checkVAT, countriesMap } from '@accountable/jsvat';

const { france, germany, ...countriesToValidate } = countriesMap;

checkVAT('BE0411905847', countriesToValidate);

Return value

checkVAT() returns VatCheckResult object:

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;
    };
  };
}

List of supported Countries:

  • 🇦🇩 Andorra
  • 🇦🇹 Austria
  • 🇧🇪 Belgium
  • 🇧🇷 Brazil
  • 🇧🇬 Bulgaria
  • 🇭🇷 Croatia
  • 🇨🇾 Cyprus
  • 🇨🇿 Czech republic
  • 🇩🇰 Denmark
  • 🇪🇪 Estonia
  • 🇪🇺 Europe
  • 🇫🇮 Finland
  • 🇫🇷 France
  • 🇩🇪 Germany
  • 🇬🇷 Greece
  • 🇭🇺 Hungary
  • 🇮🇪 Ireland
  • 🇮🇹 Italy
  • 🇱🇻 Latvia
  • 🇱🇹 Lithuania
  • 🇱🇺 Luxembourg
  • 🇲🇹 Malta
  • 🇳🇱 Netherlands
  • 🇳🇴 Norway
  • 🇵🇱 Poland
  • 🇵🇹 Portugal
  • 🇷🇴 Romania
  • 🇷🇺 Russia
  • 🇷🇸 Serbia
  • 🇸🇰 Slovakia Republic
  • 🇸🇮 Slovenia
  • 🇪🇸 Spain
  • 🇸🇪 Sweden
  • 🇬🇧 United Kingdom
  • 🇨🇭 Switzerland

Extend countries list - add your own country

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

interface Country {
  name: string;
  codes: ReadonlyArray<string>;
  rules: {
    multipliers: {}; // you can leave it empty
    regex: ReadonlyArray<RegExp>;
  };
} & ({
    calcFn: (vat: string, options?: object) => boolean; // use this if you want to check only format of VAT
  } | {
    calcWithFormatFn: (vat: string, options?: object) => {isValid: boolean, vat: string}; // use this if you want to check format of VAT and re-format it
  })

Example:

import { checkVAT } from '@accountable/jsvat';

export const wonderland = {
  name: 'Wonderland',
  codes: ['WD', 'WDR', '999'], // This codes should follow ISO standards (short, long and numeric), but it's your own business
  calcFn: (vat) => {
    return vat.length === 10;
  },
  rules: {
    regex: [/^(WD)(\d{8})$/],
  },
};

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

About modules... ES6 / CommonJS

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

import { checkVAT, belgium, austria } from '@accountable/jsvat';

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

// Modern Frontend and Node
const { checkVAT, belgium, austria } = require('@accountable/jsvat');

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

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

Alternatively you can specify which module system you do want, e.g.:

// CommonJS (i.g nodejs)
const { checkVAT, belgium, austria } = require('jsvat/lib/commonjs');

// ES6
import { checkVAT, belgium, austria } from 'jsvat/lib/es6';

How @accountable/jsvat checks validity?

There is 2-step check:

  1. Compare with list of Regexps;

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.

  1. Some magic mathematical counting;

Here we make some mathematical calculation (different for each country). After that we may be sure that ATU99999999and 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.

版本与发布记录

@accountable/jsvat 共发布 9 个版本,首次发布于 2023/08/08;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.3.4latest2024/07/092 年前
1.3.32024/05/022 年前
1.3.22024/04/042 年前
1.3.12024/04/042 年前
1.3.02023/08/283 年前
1.2.22023/08/083 年前

关于 @accountable/jsvat 的常见问题

@accountable/jsvat 是什么?

Check the validity of the format of a VAT number

如何安装 @accountable/jsvat?

在项目目录运行 npm install @accountable/jsvat,或使用 pnpm add / yarn add / bun add。

@accountable/jsvat 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@accountable/jsvat 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@accountable/jsvat 可以商用吗?

@accountable/jsvat 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@accountable/jsvat 的下载量有多少?

根据 npm 官方下载统计,@accountable/jsvat 在 2026/10/05 – 2026/11/03 这 30 天内被下载 18,869 次,日均约 629 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@accountable/jsvat 还在维护吗?

最新版本 1.3.4 发布于 2024/07/09(2 年前);至今共发布 9 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/08/08。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。