Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,27 @@ VAT, Person and Tax identifiers.

All country validators are in the "namespace" of the ISO country code.

## Bundle size: tree-shakeable imports

The `stdnum` object eagerly references every country, so importing it bundles
all of them (~200KB minified). If you only need a few validators, use either of
these forms instead:

// Named country exports — tree-shakeable, bundlers drop unused countries
import { GB, BR } from 'stdnum';

GB.nino.validate('AB123456C');
BR.cpf.validate('390.533.447-05');

// Or import a single validator via its subpath
import { validate } from 'stdnum/gb/nino';

validate('AB123456C');

Subpaths follow the source layout: `stdnum/<country>/<number>` (e.g.
`stdnum/it/codicefiscale`), and `stdnum/<country>` for a whole country's
validators.

## Supported Countries and Numbers

How you can help! This library currently support about half the countries in the world. It would be great if we can get to 100%. Submit an issue with any reference documentation and I'll do my best to integrate it, bonus points if you can get detailed descriptions of checksums or other validation criteria.
Expand Down
23 changes: 19 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,25 @@
"main": "./lib/cjs/index.js",
"module": "./lib/esm/index.js",
"types": "./lib/cjs/index.d.ts",
"exports_NOT_YET": {
"./": {
"import": "./lib/esm/",
"require": "./lib/cjs/"
"sideEffects": false,
"exports": {
".": {
"types": "./lib/cjs/index.d.ts",
"node": "./lib/cjs/index.js",
"import": "./lib/esm/index.js",
"default": "./lib/cjs/index.js"
},
"./package.json": "./package.json",
"./lib/esm/*.js": "./lib/esm/*.js",
"./lib/esm/*": "./lib/esm/*.js",
"./lib/cjs/*.js": "./lib/cjs/*.js",
"./lib/cjs/*": "./lib/cjs/*.js",
"./src/*": "./src/*",
"./*": {
"types": "./lib/esm/*.d.ts",
"node": "./lib/cjs/*.js",
"import": "./lib/esm/*.js",
"default": "./lib/cjs/*.js"
}
},
"overrides": {
Expand Down
100 changes: 100 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,106 @@ import type { Validator } from './types';

export type { Validator } from './types';

// Named per-country exports. Unlike the `stdnum` object below (which eagerly
// references every country and therefore always bundles all of them), these are
// tree-shakeable: `import { GB } from 'stdnum'` lets a bundler drop the other
// countries entirely.
export {
AD,
AI,
AL,
AO,
AR,
AT,
AU,
AZ,
BA,
BE,
BG,
BO,
BR,
BY,
BZ,
CA,
CH,
CL,
CN,
CO,
CR,
CU,
CY,
CZ,
DE,
DK,
DO,
DZ,
EC,
EE,
EG,
ES,
FI,
FO,
FR,
GB,
GH,
GR,
GN,
GT,
HK,
HR,
HU,
ID,
IE,
IL,
IN,
IS,
IT,
JP,
KE,
KR,
LI,
LT,
LU,
LV,
MA,
MC,
MD,
ME,
MK,
MT,
MU,
MX,
MY,
MZ,
NL,
NO,
NZ,
PE,
PK,
PL,
PT,
PY,
RO,
RS,
RU,
SE,
SG,
SI,
SK,
SM,
SV,
TH,
TN,
TR,
TW,
UA,
US,
UY,
VE,
VN,
ZA,
};

// Live an uppercase world, to prevent keyword collisions
export const stdnum: Record<string, Record<string, Validator>> = {
AD,
Expand Down