validator-tax-id 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +57 -0
- package/dist/index.cjs +25 -0
- package/dist/index.d.cts +16 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.global.js +25 -0
- package/dist/index.js +25 -0
- package/package.json +28 -0
package/README.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# Tax ID Validator
|
|
2
|
+
|
|
3
|
+
A lightweight, zero-dependency, and universal TypeScript library to validate Tax IDs (Identification Numbers)
|
|
4
|
+
|
|
5
|
+
It uses precise mathematical algorithms to verify the integrity of the document number and follow guides of the country goberments
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- 🚀 **Lightweight:** Zero external dependencies.
|
|
10
|
+
- 🔒 **Type-Safe:** Written in TypeScript with full type definitions.
|
|
11
|
+
- 🌍 **Universal:** Works in Node.js, React, Vue, Next.js, and Browsers (Legacy Script Tag).
|
|
12
|
+
- ⚡ **Tree-shakeable:** Only import what you need (if using advanced exports).
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install tax-id-validator
|
|
18
|
+
# or
|
|
19
|
+
yarn add tax-id-validator
|
|
20
|
+
# or
|
|
21
|
+
pnpm add tax-id-validator
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
### Basic Usage
|
|
27
|
+
|
|
28
|
+
The main function `validateIdentification` takes two arguments: the **country code** (ISO 3166-1 alpha-2) and the **value** to validate.
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import { validateIdentification } from "tax-id-validator";
|
|
32
|
+
|
|
33
|
+
// 🇪🇸 Spain (ES)
|
|
34
|
+
// Validates DNI (8 digits + letter)
|
|
35
|
+
const isDniValid = validateIdentification("es", "12345678Z"); // true
|
|
36
|
+
|
|
37
|
+
// Validates NIE (X/Y/Z + 7 digits + letter)
|
|
38
|
+
const isNieValid = validateIdentification("es", "X1234567L"); // true
|
|
39
|
+
|
|
40
|
+
// 🇵🇹 Portugal (PT)
|
|
41
|
+
// Validates NIF (9 digits with checksum)
|
|
42
|
+
const isNifValid = validateIdentification("pt", "232013969"); // true
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## API Reference
|
|
46
|
+
|
|
47
|
+
### `validateIdentification(country, value)`
|
|
48
|
+
|
|
49
|
+
- **country**: `CountryCode` ('es' | 'pt') - The ISO code of the country.
|
|
50
|
+
- **value**: `string` | `any` - The document string to validate.
|
|
51
|
+
- **Returns**: `boolean` (`true` if valid, `false` otherwise).
|
|
52
|
+
|
|
53
|
+
_Note: The validator sanitizes the input automatically (removes spaces, hyphens, and is case-insensitive)._
|
|
54
|
+
|
|
55
|
+
## License
|
|
56
|
+
|
|
57
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";var s=Object.defineProperty;var d=Object.getOwnPropertyDescriptor;var u=Object.getOwnPropertyNames;var y=Object.prototype.hasOwnProperty;var m=(e,t)=>{for(var o in t)s(e,o,{get:t[o],enumerable:!0})},g=(e,t,o,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of u(t))!y.call(e,r)&&r!==o&&s(e,r,{get:()=>t[r],enumerable:!(n=d(t,r))||n.enumerable});return e};var x=e=>g(s({},"__esModule",{value:!0}),e);var T={};m(T,{validateIdentification:()=>I});module.exports=x(T);var C="TRWAGMYFPDXBNJZSQVHLCKE";var l=e=>{if(typeof e!="string")return!1;let t=e.toUpperCase().replace(/\s|-/g,"");if(!/^([0-9]{8}|[XYZ][0-9]{7})[A-Z]$/.test(t))return!1;let n=t.slice(0,-1),r=t.slice(-1);n=n.replace("X","0").replace("Y","1").replace("Z","2");let a=parseInt(n,10);return C[a%23]===r};var i=e=>{if(typeof e!="string")return!1;let t=e.replace(/\s|-/g,"");if(!/^[0-9]{9}$/.test(t))return!1;let r=11-t.slice(0,8).split("").reduce((a,c,p)=>{let f=9-p;return a+parseInt(c,10)*f},0)%11;return r>=10&&(r=0),r===parseInt(t[8],10)};var v={es:l,pt:i};var I=(e,t)=>{let o=v[e];return o?o(t):(console.warn(`[TaxIDValidator] Country '${e}' not supported yet.`),!1)};0&&(module.exports={validateIdentification});
|
|
2
|
+
/**
|
|
3
|
+
* Validate Spanish DNI/NIE numbers.
|
|
4
|
+
* @param value - The DNI or NIE number to validate
|
|
5
|
+
* @returns boolean indicating whether the DNI/NIE is valid (true) or not (false)
|
|
6
|
+
* @author AngelBlanco97
|
|
7
|
+
* @license MIT
|
|
8
|
+
* @documentation https://www.interior.gob.es/opencms/es/servicios-al-ciudadano/tramites-y-gestiones/dni/calculo-del-digito-de-control-del-nif-nie/
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Validates a Portuguese NIF (Número de Identificação Fiscal).
|
|
12
|
+
* @param value - The NIF number to validate
|
|
13
|
+
* @returns boolean indicating whether the NIF is valid (true) or not (false)
|
|
14
|
+
* @author AngelBlanco97
|
|
15
|
+
* @license MIT
|
|
16
|
+
* @documentation https://en.wikipedia.org/wiki/Tax_identification_number#Portugal
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* Function to validate identification numbers based on country/type.
|
|
20
|
+
* @param Country - The country code representing the type of identification number (e.g., 'es' for Spain, 'pt' for Portugal)
|
|
21
|
+
* @param value - The identification number to validate
|
|
22
|
+
* @returns boolean indicating whether the identification number is valid (true) or not (false)
|
|
23
|
+
* @author AngelBlanco97
|
|
24
|
+
* @license MIT
|
|
25
|
+
*/
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
declare const validators: {
|
|
2
|
+
es: (value: any) => boolean;
|
|
3
|
+
pt: (value: any) => boolean;
|
|
4
|
+
};
|
|
5
|
+
type CountryCode = keyof typeof validators;
|
|
6
|
+
/**
|
|
7
|
+
* Function to validate identification numbers based on country/type.
|
|
8
|
+
* @param Country - The country code representing the type of identification number (e.g., 'es' for Spain, 'pt' for Portugal)
|
|
9
|
+
* @param value - The identification number to validate
|
|
10
|
+
* @returns boolean indicating whether the identification number is valid (true) or not (false)
|
|
11
|
+
* @author AngelBlanco97
|
|
12
|
+
* @license MIT
|
|
13
|
+
*/
|
|
14
|
+
declare const validateIdentification: (country: CountryCode, value: any) => boolean;
|
|
15
|
+
|
|
16
|
+
export { type CountryCode, validateIdentification };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
declare const validators: {
|
|
2
|
+
es: (value: any) => boolean;
|
|
3
|
+
pt: (value: any) => boolean;
|
|
4
|
+
};
|
|
5
|
+
type CountryCode = keyof typeof validators;
|
|
6
|
+
/**
|
|
7
|
+
* Function to validate identification numbers based on country/type.
|
|
8
|
+
* @param Country - The country code representing the type of identification number (e.g., 'es' for Spain, 'pt' for Portugal)
|
|
9
|
+
* @param value - The identification number to validate
|
|
10
|
+
* @returns boolean indicating whether the identification number is valid (true) or not (false)
|
|
11
|
+
* @author AngelBlanco97
|
|
12
|
+
* @license MIT
|
|
13
|
+
*/
|
|
14
|
+
declare const validateIdentification: (country: CountryCode, value: any) => boolean;
|
|
15
|
+
|
|
16
|
+
export { type CountryCode, validateIdentification };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";var TaxIdValidator=(()=>{var s=Object.defineProperty;var d=Object.getOwnPropertyDescriptor;var u=Object.getOwnPropertyNames;var y=Object.prototype.hasOwnProperty;var m=(e,t)=>{for(var o in t)s(e,o,{get:t[o],enumerable:!0})},g=(e,t,o,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of u(t))!y.call(e,r)&&r!==o&&s(e,r,{get:()=>t[r],enumerable:!(n=d(t,r))||n.enumerable});return e};var x=e=>g(s({},"__esModule",{value:!0}),e);var T={};m(T,{validateIdentification:()=>I});var C="TRWAGMYFPDXBNJZSQVHLCKE";var l=e=>{if(typeof e!="string")return!1;let t=e.toUpperCase().replace(/\s|-/g,"");if(!/^([0-9]{8}|[XYZ][0-9]{7})[A-Z]$/.test(t))return!1;let n=t.slice(0,-1),r=t.slice(-1);n=n.replace("X","0").replace("Y","1").replace("Z","2");let a=parseInt(n,10);return C[a%23]===r};var i=e=>{if(typeof e!="string")return!1;let t=e.replace(/\s|-/g,"");if(!/^[0-9]{9}$/.test(t))return!1;let r=11-t.slice(0,8).split("").reduce((a,c,p)=>{let f=9-p;return a+parseInt(c,10)*f},0)%11;return r>=10&&(r=0),r===parseInt(t[8],10)};var v={es:l,pt:i};var I=(e,t)=>{let o=v[e];return o?o(t):(console.warn(`[TaxIDValidator] Country '${e}' not supported yet.`),!1)};return x(T);})();
|
|
2
|
+
/**
|
|
3
|
+
* Validate Spanish DNI/NIE numbers.
|
|
4
|
+
* @param value - The DNI or NIE number to validate
|
|
5
|
+
* @returns boolean indicating whether the DNI/NIE is valid (true) or not (false)
|
|
6
|
+
* @author AngelBlanco97
|
|
7
|
+
* @license MIT
|
|
8
|
+
* @documentation https://www.interior.gob.es/opencms/es/servicios-al-ciudadano/tramites-y-gestiones/dni/calculo-del-digito-de-control-del-nif-nie/
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Validates a Portuguese NIF (Número de Identificação Fiscal).
|
|
12
|
+
* @param value - The NIF number to validate
|
|
13
|
+
* @returns boolean indicating whether the NIF is valid (true) or not (false)
|
|
14
|
+
* @author AngelBlanco97
|
|
15
|
+
* @license MIT
|
|
16
|
+
* @documentation https://en.wikipedia.org/wiki/Tax_identification_number#Portugal
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* Function to validate identification numbers based on country/type.
|
|
20
|
+
* @param Country - The country code representing the type of identification number (e.g., 'es' for Spain, 'pt' for Portugal)
|
|
21
|
+
* @param value - The identification number to validate
|
|
22
|
+
* @returns boolean indicating whether the identification number is valid (true) or not (false)
|
|
23
|
+
* @author AngelBlanco97
|
|
24
|
+
* @license MIT
|
|
25
|
+
*/
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
var f="TRWAGMYFPDXBNJZSQVHLCKE";var s=e=>{if(typeof e!="string")return!1;let t=e.toUpperCase().replace(/\s|-/g,"");if(!/^([0-9]{8}|[XYZ][0-9]{7})[A-Z]$/.test(t))return!1;let n=t.slice(0,-1),r=t.slice(-1);n=n.replace("X","0").replace("Y","1").replace("Z","2");let a=parseInt(n,10);return f[a%23]===r};var l=e=>{if(typeof e!="string")return!1;let t=e.replace(/\s|-/g,"");if(!/^[0-9]{9}$/.test(t))return!1;let r=11-t.slice(0,8).split("").reduce((a,i,c)=>{let p=9-c;return a+parseInt(i,10)*p},0)%11;return r>=10&&(r=0),r===parseInt(t[8],10)};var d={es:s,pt:l};var x=(e,t)=>{let o=d[e];return o?o(t):(console.warn(`[TaxIDValidator] Country '${e}' not supported yet.`),!1)};export{x as validateIdentification};
|
|
2
|
+
/**
|
|
3
|
+
* Validate Spanish DNI/NIE numbers.
|
|
4
|
+
* @param value - The DNI or NIE number to validate
|
|
5
|
+
* @returns boolean indicating whether the DNI/NIE is valid (true) or not (false)
|
|
6
|
+
* @author AngelBlanco97
|
|
7
|
+
* @license MIT
|
|
8
|
+
* @documentation https://www.interior.gob.es/opencms/es/servicios-al-ciudadano/tramites-y-gestiones/dni/calculo-del-digito-de-control-del-nif-nie/
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Validates a Portuguese NIF (Número de Identificação Fiscal).
|
|
12
|
+
* @param value - The NIF number to validate
|
|
13
|
+
* @returns boolean indicating whether the NIF is valid (true) or not (false)
|
|
14
|
+
* @author AngelBlanco97
|
|
15
|
+
* @license MIT
|
|
16
|
+
* @documentation https://en.wikipedia.org/wiki/Tax_identification_number#Portugal
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* Function to validate identification numbers based on country/type.
|
|
20
|
+
* @param Country - The country code representing the type of identification number (e.g., 'es' for Spain, 'pt' for Portugal)
|
|
21
|
+
* @param value - The identification number to validate
|
|
22
|
+
* @returns boolean indicating whether the identification number is valid (true) or not (false)
|
|
23
|
+
* @author AngelBlanco97
|
|
24
|
+
* @license MIT
|
|
25
|
+
*/
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "validator-tax-id",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.cjs",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"require": "./dist/index.cjs"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsup",
|
|
20
|
+
"dev": "tsup --watch",
|
|
21
|
+
"test": "vitest"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"tsup": "^8.0.0",
|
|
25
|
+
"typescript": "^5.0.0",
|
|
26
|
+
"vitest": "^4.0.17"
|
|
27
|
+
}
|
|
28
|
+
}
|