@maistik/validate-nif 2.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Maistik Studio
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,152 @@
1
+ # @maistik/validate-nif
2
+
3
+ [![CI](https://github.com/Maistik-Studio/validate-nif/actions/workflows/ci.yml/badge.svg)](https://github.com/Maistik-Studio/validate-nif/actions/workflows/ci.yml)
4
+ [![npm version](https://img.shields.io/npm/v/@maistik/validate-nif.svg)](https://www.npmjs.com/package/@maistik/validate-nif)
5
+ [![license](https://img.shields.io/npm/l/@maistik/validate-nif.svg)](./LICENSE)
6
+
7
+ Zero-dependency, **isomorphic** (Node.js & browser) validator for Spanish fiscal
8
+ identifiers, written in TypeScript with full type definitions included.
9
+
10
+ - ✅ **DNI / NIF** — natural persons, residents (`12345678Z`)
11
+ - ✅ **NIE** — natural persons, foreigners (`X1234567L`)
12
+ - ✅ **CIF** — legal entities / organizations (`A58818501`)
13
+ - 🧮 Official AEAT control-character algorithms
14
+ - 🪶 No dependencies, tree-shakeable, ESM + CommonJS builds
15
+ - 🛡️ Never throws — any input (including `null`/`undefined`/numbers) is handled
16
+ - 🧪 100% test coverage
17
+
18
+ ## Installation
19
+
20
+ ```bash
21
+ # npm
22
+ npm install @maistik/validate-nif
23
+
24
+ # pnpm
25
+ pnpm add @maistik/validate-nif
26
+
27
+ # yarn
28
+ yarn add @maistik/validate-nif
29
+ ```
30
+
31
+ ## Usage
32
+
33
+ ### ESM / TypeScript
34
+
35
+ ```ts
36
+ import { isValid, isValidDNI, isValidNIE, isValidCIF, getType } from '@maistik/validate-nif'
37
+
38
+ isValidDNI('12345678Z') // true
39
+ isValidNIE('X1234567L') // true
40
+ isValidCIF('A58818501') // true
41
+
42
+ isValid('12345678Z') // true (any supported type)
43
+ getType('X1234567L') // 'NIE'
44
+
45
+ // Loosely formatted input is normalized automatically.
46
+ isValid(' 12.345.678-z ') // true
47
+ ```
48
+
49
+ ### CommonJS
50
+
51
+ ```js
52
+ const { validateNif, isValid } = require('@maistik/validate-nif')
53
+
54
+ isValid('A58818501') // true
55
+ ```
56
+
57
+ ### Browser
58
+
59
+ The package ships an ESM build with no Node.js dependencies, so it works
60
+ directly in the browser via any bundler (Vite, webpack, Rollup, esbuild) or
61
+ straight from a CDN:
62
+
63
+ ```html
64
+ <script type="module">
65
+ import { isValid } from 'https://esm.sh/@maistik/validate-nif'
66
+ console.log(isValid('12345678Z')) // true
67
+ </script>
68
+ ```
69
+
70
+ ## API
71
+
72
+ All functions accept `unknown` input and never throw.
73
+
74
+ | Function | Returns | Description |
75
+ | --- | --- | --- |
76
+ | `isValid(value)` | `boolean` | `true` for a valid DNI, NIE **or** CIF. |
77
+ | `isValidNIF(value)` | `boolean` | `true` for a valid DNI **or** NIE (natural person). |
78
+ | `isValidDNI(value)` | `boolean` | `true` for a valid DNI / NIF. |
79
+ | `isValidNIE(value)` | `boolean` | `true` for a valid NIE. |
80
+ | `isValidCIF(value)` | `boolean` | `true` for a valid CIF. |
81
+ | `getType(value)` | `'DNI' \| 'NIE' \| 'CIF' \| null` | The detected type, or `null` if invalid. |
82
+ | `getCifOrganizationType(value)` | `{ code, description } \| null` | The organization type for a valid CIF, or `null`. |
83
+ | `parse(value)` | `NifInfo` | Structured info: `{ valid, type, normalized, organization }`. |
84
+ | `format(value, options?)` | `string` | Canonical form; `options.separator` inserts a separator before the control char. |
85
+ | `normalize(value)` | `string` | Upper-cases and strips spaces, dots, hyphens and underscores. |
86
+ | `controlLetterFor(num)` | `string` | The DNI/NIE control letter for an 8-digit number. |
87
+ | `validateNif(value)` | `number` | Legacy numeric code (see below). |
88
+
89
+ ### Parsing & organization types
90
+
91
+ ```ts
92
+ import { parse, getCifOrganizationType, format } from '@maistik/validate-nif'
93
+
94
+ parse('a-58818501')
95
+ // {
96
+ // valid: true,
97
+ // type: 'CIF',
98
+ // normalized: 'A58818501',
99
+ // organization: { code: 'A', description: 'Sociedad anónima' },
100
+ // }
101
+
102
+ getCifOrganizationType('G12345678') // { code: 'G', description: 'Asociación o fundación' }
103
+
104
+ format('12345678z', { separator: '-' }) // '12345678-Z'
105
+ ```
106
+
107
+ A CIF's leading letter is mapped to its Spanish organization type (Sociedad
108
+ anónima, Sociedad de responsabilidad limitada, Cooperativa, Asociación,
109
+ Corporación local, and so on — the full AEAT set is supported).
110
+
111
+ ### `validateNif` result codes
112
+
113
+ `validateNif` is kept for backwards compatibility. It returns a numeric code:
114
+ negative means **invalid**, `>= 0` means **valid**.
115
+
116
+ | Code | Constant | Meaning |
117
+ | --- | --- | --- |
118
+ | `-1` | `ValidationResult.ERROR` | Invalid / unknown value |
119
+ | `1` | `ValidationResult.DNI` | Valid DNI / NIF |
120
+ | `4` | `ValidationResult.NIE` | Valid NIE |
121
+ | `20` | `ValidationResult.CIF` | Valid CIF |
122
+
123
+ ```ts
124
+ import { validateNif, ValidationResult } from '@maistik/validate-nif'
125
+
126
+ validateNif('62805436A') // 1 (>= 0 → valid)
127
+ validateNif('62805436X') // -1 (< 0 → invalid)
128
+ validateNif('A58818501') === ValidationResult.CIF // true
129
+ ```
130
+
131
+ ## Types
132
+
133
+ Type declarations (`.d.ts`) are bundled and exposed automatically — no
134
+ `@types/*` package is required. The package also exports the `NifType`,
135
+ `NifInfo`, `CifOrganizationType`, `FormatOptions` and `ValidationResultCode`
136
+ helper types.
137
+
138
+ ## Development
139
+
140
+ ```bash
141
+ pnpm install
142
+ pnpm test:coverage # tests with 100% coverage enforcement
143
+ pnpm lint # eslint
144
+ pnpm typecheck # tsc --noEmit
145
+ pnpm build # build dist/ (ESM + CJS + .d.ts)
146
+ ```
147
+
148
+ See [CONTRIBUTING.md](./CONTRIBUTING.md) for the full contribution guide.
149
+
150
+ ## License
151
+
152
+ [MIT](./LICENSE) © Maistik Studio
package/dist/index.cjs ADDED
@@ -0,0 +1,166 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ const ValidationResult = {
6
+ /** Unknown or malformed value. */
7
+ ERROR: -1,
8
+ /** Valid DNI / NIF (Spanish resident). */
9
+ DNI: 1,
10
+ /** Valid NIE (foreigner). */
11
+ NIE: 4,
12
+ /** Valid CIF (organization). */
13
+ CIF: 20
14
+ };
15
+ const DNI_LETTERS = "TRWAGMYFPDXBNJZSQVHLCKE";
16
+ const CIF_LETTERS = "JABCDEFGHI";
17
+ const CIF_LETTER_CONTROL = "NPQRSW";
18
+ const CIF_DIGIT_CONTROL = "ABEH";
19
+ const NIE_PREFIX = { X: "0", Y: "1", Z: "2" };
20
+ const CIF_ORGANIZATION_TYPES = {
21
+ A: "Sociedad an\xF3nima",
22
+ B: "Sociedad de responsabilidad limitada",
23
+ C: "Sociedad colectiva",
24
+ D: "Sociedad comanditaria",
25
+ E: "Comunidad de bienes y herencias yacentes",
26
+ F: "Sociedad cooperativa",
27
+ G: "Asociaci\xF3n o fundaci\xF3n",
28
+ H: "Comunidad de propietarios en r\xE9gimen de propiedad horizontal",
29
+ J: "Sociedad civil",
30
+ N: "Entidad extranjera",
31
+ P: "Corporaci\xF3n local",
32
+ Q: "Organismo p\xFAblico",
33
+ R: "Congregaci\xF3n o instituci\xF3n religiosa",
34
+ S: "\xD3rgano de la Administraci\xF3n del Estado o comunidad aut\xF3noma",
35
+ U: "Uni\xF3n temporal de empresas",
36
+ V: "Otro tipo de entidad sin personalidad jur\xEDdica",
37
+ W: "Establecimiento permanente de entidad no residente"
38
+ };
39
+ const DNI_RE = /^\d{8}[A-Z]$/;
40
+ const NIE_RE = /^[XYZ]\d{7}[A-Z]$/;
41
+ const CIF_RE = /^[ABCDEFGHJNPQRSUVW]\d{7}[0-9A-J]$/;
42
+ function normalize(value) {
43
+ if (value === null || value === void 0) {
44
+ return "";
45
+ }
46
+ const str = typeof value === "string" ? value : String(value);
47
+ return str.toUpperCase().replace(/[\s.\-_]/g, "");
48
+ }
49
+ function controlLetterFor(num) {
50
+ return DNI_LETTERS[num % 23];
51
+ }
52
+ function isValidDNI(value) {
53
+ const v = normalize(value);
54
+ if (!DNI_RE.test(v)) {
55
+ return false;
56
+ }
57
+ const num = parseInt(v.slice(0, 8), 10);
58
+ return v[8] === controlLetterFor(num);
59
+ }
60
+ function isValidNIE(value) {
61
+ const v = normalize(value);
62
+ if (!NIE_RE.test(v)) {
63
+ return false;
64
+ }
65
+ const num = parseInt(NIE_PREFIX[v[0]] + v.slice(1, 8), 10);
66
+ return v[8] === controlLetterFor(num);
67
+ }
68
+ function isValidCIF(value) {
69
+ const v = normalize(value);
70
+ if (!CIF_RE.test(v)) {
71
+ return false;
72
+ }
73
+ const firstLetter = v[0];
74
+ const digits = v.slice(1, 8);
75
+ const control = v[8];
76
+ let sum = 0;
77
+ for (let i = 0; i < digits.length; i += 1) {
78
+ let n = parseInt(digits[i], 10);
79
+ if (i % 2 === 0) {
80
+ n *= 2;
81
+ if (n > 9) {
82
+ n -= 9;
83
+ }
84
+ }
85
+ sum += n;
86
+ }
87
+ const controlDigit = (10 - sum % 10) % 10;
88
+ const controlLetter = CIF_LETTERS[controlDigit];
89
+ if (CIF_LETTER_CONTROL.includes(firstLetter)) {
90
+ return control === controlLetter;
91
+ }
92
+ if (CIF_DIGIT_CONTROL.includes(firstLetter)) {
93
+ return control === String(controlDigit);
94
+ }
95
+ return control === String(controlDigit) || control === controlLetter;
96
+ }
97
+ function isValidNIF(value) {
98
+ return isValidDNI(value) || isValidNIE(value);
99
+ }
100
+ function isValid(value) {
101
+ return isValidDNI(value) || isValidNIE(value) || isValidCIF(value);
102
+ }
103
+ function getType(value) {
104
+ if (isValidDNI(value)) {
105
+ return "DNI";
106
+ }
107
+ if (isValidNIE(value)) {
108
+ return "NIE";
109
+ }
110
+ if (isValidCIF(value)) {
111
+ return "CIF";
112
+ }
113
+ return null;
114
+ }
115
+ function getCifOrganizationType(value) {
116
+ if (!isValidCIF(value)) {
117
+ return null;
118
+ }
119
+ const code = normalize(value)[0];
120
+ return { code, description: CIF_ORGANIZATION_TYPES[code] };
121
+ }
122
+ function parse(value) {
123
+ const normalized = normalize(value);
124
+ const type = getType(value);
125
+ return {
126
+ valid: type !== null,
127
+ type,
128
+ normalized,
129
+ organization: type === "CIF" ? getCifOrganizationType(value) : null
130
+ };
131
+ }
132
+ function format(value, options = {}) {
133
+ const v = normalize(value);
134
+ const separator = options.separator ?? "";
135
+ if (!separator || !isValid(v)) {
136
+ return v;
137
+ }
138
+ return `${v.slice(0, -1)}${separator}${v.slice(-1)}`;
139
+ }
140
+ function validateNif(value) {
141
+ if (isValidDNI(value)) {
142
+ return ValidationResult.DNI;
143
+ }
144
+ if (isValidNIE(value)) {
145
+ return ValidationResult.NIE;
146
+ }
147
+ if (isValidCIF(value)) {
148
+ return ValidationResult.CIF;
149
+ }
150
+ return ValidationResult.ERROR;
151
+ }
152
+
153
+ exports.ValidationResult = ValidationResult;
154
+ exports.controlLetterFor = controlLetterFor;
155
+ exports.default = validateNif;
156
+ exports.format = format;
157
+ exports.getCifOrganizationType = getCifOrganizationType;
158
+ exports.getType = getType;
159
+ exports.isValid = isValid;
160
+ exports.isValidCIF = isValidCIF;
161
+ exports.isValidDNI = isValidDNI;
162
+ exports.isValidNIE = isValidNIE;
163
+ exports.isValidNIF = isValidNIF;
164
+ exports.normalize = normalize;
165
+ exports.parse = parse;
166
+ exports.validateNif = validateNif;
@@ -0,0 +1,99 @@
1
+ /**
2
+ * validate-nif
3
+ *
4
+ * Zero-dependency, isomorphic (Node & browser) validator for Spanish fiscal
5
+ * identifiers:
6
+ *
7
+ * - DNI / NIF — natural persons, residents (8 digits + control letter)
8
+ * - NIE — natural persons, foreigners (X/Y/Z + 7 digits + control letter)
9
+ * - CIF — legal entities / organizations (letter + 7 digits + control)
10
+ *
11
+ * The control-character algorithms follow the official AEAT specification.
12
+ */
13
+ /** The kind of a valid Spanish fiscal identifier. */
14
+ type NifType = 'DNI' | 'NIE' | 'CIF';
15
+ /** The organization type encoded in the first letter of a CIF. */
16
+ interface CifOrganizationType {
17
+ /** The CIF leading letter (e.g. `'A'`). */
18
+ code: string;
19
+ /** Human-readable description of the organization type (in Spanish). */
20
+ description: string;
21
+ }
22
+ /** Structured result describing a parsed identifier. */
23
+ interface NifInfo {
24
+ /** Whether the value is a valid DNI, NIE or CIF. */
25
+ valid: boolean;
26
+ /** The detected type, or `null` when invalid. */
27
+ type: NifType | null;
28
+ /** The normalized (upper-cased, separator-free) value. */
29
+ normalized: string;
30
+ /** The organization details, only present for a valid CIF. */
31
+ organization: CifOrganizationType | null;
32
+ }
33
+ /** Options for {@link format}. */
34
+ interface FormatOptions {
35
+ /** Separator inserted before the control character (e.g. `'-'`). */
36
+ separator?: string;
37
+ }
38
+ /**
39
+ * Numeric result codes returned by {@link validateNif}.
40
+ * Negative values mean invalid; values `>= 0` mean valid.
41
+ */
42
+ declare const ValidationResult: {
43
+ /** Unknown or malformed value. */
44
+ readonly ERROR: -1;
45
+ /** Valid DNI / NIF (Spanish resident). */
46
+ readonly DNI: 1;
47
+ /** Valid NIE (foreigner). */
48
+ readonly NIE: 4;
49
+ /** Valid CIF (organization). */
50
+ readonly CIF: 20;
51
+ };
52
+ type ValidationResultCode = (typeof ValidationResult)[keyof typeof ValidationResult];
53
+ /**
54
+ * Normalize any input into an upper-cased string with the common separators
55
+ * (spaces, dots, hyphens, underscores) removed. Non-string and nullish values
56
+ * are coerced safely, so the public API never throws.
57
+ */
58
+ declare function normalize(value: unknown): string;
59
+ /** Compute the control letter for an 8-digit DNI / NIE number. */
60
+ declare function controlLetterFor(num: number): string;
61
+ /** Validate a Spanish DNI / NIF (8 digits + control letter). */
62
+ declare function isValidDNI(value: unknown): boolean;
63
+ /** Validate a Spanish NIE (X/Y/Z + 7 digits + control letter). */
64
+ declare function isValidNIE(value: unknown): boolean;
65
+ /** Validate a Spanish CIF (letter + 7 digits + control digit/letter). */
66
+ declare function isValidCIF(value: unknown): boolean;
67
+ /** Validate a NIF for a natural person (DNI or NIE). */
68
+ declare function isValidNIF(value: unknown): boolean;
69
+ /** Validate any supported Spanish fiscal identifier (DNI, NIE or CIF). */
70
+ declare function isValid(value: unknown): boolean;
71
+ /** Detect the type of a valid identifier, or `null` when it is invalid. */
72
+ declare function getType(value: unknown): NifType | null;
73
+ /**
74
+ * Resolve the organization type for a valid CIF from its leading letter, or
75
+ * `null` when the value is not a valid CIF.
76
+ */
77
+ declare function getCifOrganizationType(value: unknown): CifOrganizationType | null;
78
+ /**
79
+ * Parse a value into a structured {@link NifInfo} object describing its
80
+ * validity, type, normalized form and (for CIFs) organization details.
81
+ */
82
+ declare function parse(value: unknown): NifInfo;
83
+ /**
84
+ * Return the canonical (normalized, upper-cased) representation of an
85
+ * identifier. When a `separator` is provided it is inserted before the control
86
+ * character — e.g. `format('12345678z', { separator: '-' })` → `'12345678-Z'`.
87
+ * Invalid values are returned normalized without a separator.
88
+ */
89
+ declare function format(value: unknown, options?: FormatOptions): string;
90
+ /**
91
+ * Backwards-compatible validator returning a numeric {@link ValidationResult}
92
+ * code. Negative values mean invalid; values `>= 0` mean valid.
93
+ */
94
+ declare function validateNif(value: unknown): ValidationResultCode;
95
+
96
+ // @ts-ignore
97
+ export = validateNif;
98
+ export { ValidationResult, controlLetterFor, format, getCifOrganizationType, getType, isValid, isValidCIF, isValidDNI, isValidNIE, isValidNIF, normalize, parse, validateNif };
99
+ export type { CifOrganizationType, FormatOptions, NifInfo, NifType, ValidationResultCode };
@@ -0,0 +1,97 @@
1
+ /**
2
+ * validate-nif
3
+ *
4
+ * Zero-dependency, isomorphic (Node & browser) validator for Spanish fiscal
5
+ * identifiers:
6
+ *
7
+ * - DNI / NIF — natural persons, residents (8 digits + control letter)
8
+ * - NIE — natural persons, foreigners (X/Y/Z + 7 digits + control letter)
9
+ * - CIF — legal entities / organizations (letter + 7 digits + control)
10
+ *
11
+ * The control-character algorithms follow the official AEAT specification.
12
+ */
13
+ /** The kind of a valid Spanish fiscal identifier. */
14
+ type NifType = 'DNI' | 'NIE' | 'CIF';
15
+ /** The organization type encoded in the first letter of a CIF. */
16
+ interface CifOrganizationType {
17
+ /** The CIF leading letter (e.g. `'A'`). */
18
+ code: string;
19
+ /** Human-readable description of the organization type (in Spanish). */
20
+ description: string;
21
+ }
22
+ /** Structured result describing a parsed identifier. */
23
+ interface NifInfo {
24
+ /** Whether the value is a valid DNI, NIE or CIF. */
25
+ valid: boolean;
26
+ /** The detected type, or `null` when invalid. */
27
+ type: NifType | null;
28
+ /** The normalized (upper-cased, separator-free) value. */
29
+ normalized: string;
30
+ /** The organization details, only present for a valid CIF. */
31
+ organization: CifOrganizationType | null;
32
+ }
33
+ /** Options for {@link format}. */
34
+ interface FormatOptions {
35
+ /** Separator inserted before the control character (e.g. `'-'`). */
36
+ separator?: string;
37
+ }
38
+ /**
39
+ * Numeric result codes returned by {@link validateNif}.
40
+ * Negative values mean invalid; values `>= 0` mean valid.
41
+ */
42
+ declare const ValidationResult: {
43
+ /** Unknown or malformed value. */
44
+ readonly ERROR: -1;
45
+ /** Valid DNI / NIF (Spanish resident). */
46
+ readonly DNI: 1;
47
+ /** Valid NIE (foreigner). */
48
+ readonly NIE: 4;
49
+ /** Valid CIF (organization). */
50
+ readonly CIF: 20;
51
+ };
52
+ type ValidationResultCode = (typeof ValidationResult)[keyof typeof ValidationResult];
53
+ /**
54
+ * Normalize any input into an upper-cased string with the common separators
55
+ * (spaces, dots, hyphens, underscores) removed. Non-string and nullish values
56
+ * are coerced safely, so the public API never throws.
57
+ */
58
+ declare function normalize(value: unknown): string;
59
+ /** Compute the control letter for an 8-digit DNI / NIE number. */
60
+ declare function controlLetterFor(num: number): string;
61
+ /** Validate a Spanish DNI / NIF (8 digits + control letter). */
62
+ declare function isValidDNI(value: unknown): boolean;
63
+ /** Validate a Spanish NIE (X/Y/Z + 7 digits + control letter). */
64
+ declare function isValidNIE(value: unknown): boolean;
65
+ /** Validate a Spanish CIF (letter + 7 digits + control digit/letter). */
66
+ declare function isValidCIF(value: unknown): boolean;
67
+ /** Validate a NIF for a natural person (DNI or NIE). */
68
+ declare function isValidNIF(value: unknown): boolean;
69
+ /** Validate any supported Spanish fiscal identifier (DNI, NIE or CIF). */
70
+ declare function isValid(value: unknown): boolean;
71
+ /** Detect the type of a valid identifier, or `null` when it is invalid. */
72
+ declare function getType(value: unknown): NifType | null;
73
+ /**
74
+ * Resolve the organization type for a valid CIF from its leading letter, or
75
+ * `null` when the value is not a valid CIF.
76
+ */
77
+ declare function getCifOrganizationType(value: unknown): CifOrganizationType | null;
78
+ /**
79
+ * Parse a value into a structured {@link NifInfo} object describing its
80
+ * validity, type, normalized form and (for CIFs) organization details.
81
+ */
82
+ declare function parse(value: unknown): NifInfo;
83
+ /**
84
+ * Return the canonical (normalized, upper-cased) representation of an
85
+ * identifier. When a `separator` is provided it is inserted before the control
86
+ * character — e.g. `format('12345678z', { separator: '-' })` → `'12345678-Z'`.
87
+ * Invalid values are returned normalized without a separator.
88
+ */
89
+ declare function format(value: unknown, options?: FormatOptions): string;
90
+ /**
91
+ * Backwards-compatible validator returning a numeric {@link ValidationResult}
92
+ * code. Negative values mean invalid; values `>= 0` mean valid.
93
+ */
94
+ declare function validateNif(value: unknown): ValidationResultCode;
95
+
96
+ export { ValidationResult, controlLetterFor, validateNif as default, format, getCifOrganizationType, getType, isValid, isValidCIF, isValidDNI, isValidNIE, isValidNIF, normalize, parse, validateNif };
97
+ export type { CifOrganizationType, FormatOptions, NifInfo, NifType, ValidationResultCode };
@@ -0,0 +1,99 @@
1
+ /**
2
+ * validate-nif
3
+ *
4
+ * Zero-dependency, isomorphic (Node & browser) validator for Spanish fiscal
5
+ * identifiers:
6
+ *
7
+ * - DNI / NIF — natural persons, residents (8 digits + control letter)
8
+ * - NIE — natural persons, foreigners (X/Y/Z + 7 digits + control letter)
9
+ * - CIF — legal entities / organizations (letter + 7 digits + control)
10
+ *
11
+ * The control-character algorithms follow the official AEAT specification.
12
+ */
13
+ /** The kind of a valid Spanish fiscal identifier. */
14
+ type NifType = 'DNI' | 'NIE' | 'CIF';
15
+ /** The organization type encoded in the first letter of a CIF. */
16
+ interface CifOrganizationType {
17
+ /** The CIF leading letter (e.g. `'A'`). */
18
+ code: string;
19
+ /** Human-readable description of the organization type (in Spanish). */
20
+ description: string;
21
+ }
22
+ /** Structured result describing a parsed identifier. */
23
+ interface NifInfo {
24
+ /** Whether the value is a valid DNI, NIE or CIF. */
25
+ valid: boolean;
26
+ /** The detected type, or `null` when invalid. */
27
+ type: NifType | null;
28
+ /** The normalized (upper-cased, separator-free) value. */
29
+ normalized: string;
30
+ /** The organization details, only present for a valid CIF. */
31
+ organization: CifOrganizationType | null;
32
+ }
33
+ /** Options for {@link format}. */
34
+ interface FormatOptions {
35
+ /** Separator inserted before the control character (e.g. `'-'`). */
36
+ separator?: string;
37
+ }
38
+ /**
39
+ * Numeric result codes returned by {@link validateNif}.
40
+ * Negative values mean invalid; values `>= 0` mean valid.
41
+ */
42
+ declare const ValidationResult: {
43
+ /** Unknown or malformed value. */
44
+ readonly ERROR: -1;
45
+ /** Valid DNI / NIF (Spanish resident). */
46
+ readonly DNI: 1;
47
+ /** Valid NIE (foreigner). */
48
+ readonly NIE: 4;
49
+ /** Valid CIF (organization). */
50
+ readonly CIF: 20;
51
+ };
52
+ type ValidationResultCode = (typeof ValidationResult)[keyof typeof ValidationResult];
53
+ /**
54
+ * Normalize any input into an upper-cased string with the common separators
55
+ * (spaces, dots, hyphens, underscores) removed. Non-string and nullish values
56
+ * are coerced safely, so the public API never throws.
57
+ */
58
+ declare function normalize(value: unknown): string;
59
+ /** Compute the control letter for an 8-digit DNI / NIE number. */
60
+ declare function controlLetterFor(num: number): string;
61
+ /** Validate a Spanish DNI / NIF (8 digits + control letter). */
62
+ declare function isValidDNI(value: unknown): boolean;
63
+ /** Validate a Spanish NIE (X/Y/Z + 7 digits + control letter). */
64
+ declare function isValidNIE(value: unknown): boolean;
65
+ /** Validate a Spanish CIF (letter + 7 digits + control digit/letter). */
66
+ declare function isValidCIF(value: unknown): boolean;
67
+ /** Validate a NIF for a natural person (DNI or NIE). */
68
+ declare function isValidNIF(value: unknown): boolean;
69
+ /** Validate any supported Spanish fiscal identifier (DNI, NIE or CIF). */
70
+ declare function isValid(value: unknown): boolean;
71
+ /** Detect the type of a valid identifier, or `null` when it is invalid. */
72
+ declare function getType(value: unknown): NifType | null;
73
+ /**
74
+ * Resolve the organization type for a valid CIF from its leading letter, or
75
+ * `null` when the value is not a valid CIF.
76
+ */
77
+ declare function getCifOrganizationType(value: unknown): CifOrganizationType | null;
78
+ /**
79
+ * Parse a value into a structured {@link NifInfo} object describing its
80
+ * validity, type, normalized form and (for CIFs) organization details.
81
+ */
82
+ declare function parse(value: unknown): NifInfo;
83
+ /**
84
+ * Return the canonical (normalized, upper-cased) representation of an
85
+ * identifier. When a `separator` is provided it is inserted before the control
86
+ * character — e.g. `format('12345678z', { separator: '-' })` → `'12345678-Z'`.
87
+ * Invalid values are returned normalized without a separator.
88
+ */
89
+ declare function format(value: unknown, options?: FormatOptions): string;
90
+ /**
91
+ * Backwards-compatible validator returning a numeric {@link ValidationResult}
92
+ * code. Negative values mean invalid; values `>= 0` mean valid.
93
+ */
94
+ declare function validateNif(value: unknown): ValidationResultCode;
95
+
96
+ // @ts-ignore
97
+ export = validateNif;
98
+ export { ValidationResult, controlLetterFor, format, getCifOrganizationType, getType, isValid, isValidCIF, isValidDNI, isValidNIE, isValidNIF, normalize, parse, validateNif };
99
+ export type { CifOrganizationType, FormatOptions, NifInfo, NifType, ValidationResultCode };
package/dist/index.mjs ADDED
@@ -0,0 +1,149 @@
1
+ const ValidationResult = {
2
+ /** Unknown or malformed value. */
3
+ ERROR: -1,
4
+ /** Valid DNI / NIF (Spanish resident). */
5
+ DNI: 1,
6
+ /** Valid NIE (foreigner). */
7
+ NIE: 4,
8
+ /** Valid CIF (organization). */
9
+ CIF: 20
10
+ };
11
+ const DNI_LETTERS = "TRWAGMYFPDXBNJZSQVHLCKE";
12
+ const CIF_LETTERS = "JABCDEFGHI";
13
+ const CIF_LETTER_CONTROL = "NPQRSW";
14
+ const CIF_DIGIT_CONTROL = "ABEH";
15
+ const NIE_PREFIX = { X: "0", Y: "1", Z: "2" };
16
+ const CIF_ORGANIZATION_TYPES = {
17
+ A: "Sociedad an\xF3nima",
18
+ B: "Sociedad de responsabilidad limitada",
19
+ C: "Sociedad colectiva",
20
+ D: "Sociedad comanditaria",
21
+ E: "Comunidad de bienes y herencias yacentes",
22
+ F: "Sociedad cooperativa",
23
+ G: "Asociaci\xF3n o fundaci\xF3n",
24
+ H: "Comunidad de propietarios en r\xE9gimen de propiedad horizontal",
25
+ J: "Sociedad civil",
26
+ N: "Entidad extranjera",
27
+ P: "Corporaci\xF3n local",
28
+ Q: "Organismo p\xFAblico",
29
+ R: "Congregaci\xF3n o instituci\xF3n religiosa",
30
+ S: "\xD3rgano de la Administraci\xF3n del Estado o comunidad aut\xF3noma",
31
+ U: "Uni\xF3n temporal de empresas",
32
+ V: "Otro tipo de entidad sin personalidad jur\xEDdica",
33
+ W: "Establecimiento permanente de entidad no residente"
34
+ };
35
+ const DNI_RE = /^\d{8}[A-Z]$/;
36
+ const NIE_RE = /^[XYZ]\d{7}[A-Z]$/;
37
+ const CIF_RE = /^[ABCDEFGHJNPQRSUVW]\d{7}[0-9A-J]$/;
38
+ function normalize(value) {
39
+ if (value === null || value === void 0) {
40
+ return "";
41
+ }
42
+ const str = typeof value === "string" ? value : String(value);
43
+ return str.toUpperCase().replace(/[\s.\-_]/g, "");
44
+ }
45
+ function controlLetterFor(num) {
46
+ return DNI_LETTERS[num % 23];
47
+ }
48
+ function isValidDNI(value) {
49
+ const v = normalize(value);
50
+ if (!DNI_RE.test(v)) {
51
+ return false;
52
+ }
53
+ const num = parseInt(v.slice(0, 8), 10);
54
+ return v[8] === controlLetterFor(num);
55
+ }
56
+ function isValidNIE(value) {
57
+ const v = normalize(value);
58
+ if (!NIE_RE.test(v)) {
59
+ return false;
60
+ }
61
+ const num = parseInt(NIE_PREFIX[v[0]] + v.slice(1, 8), 10);
62
+ return v[8] === controlLetterFor(num);
63
+ }
64
+ function isValidCIF(value) {
65
+ const v = normalize(value);
66
+ if (!CIF_RE.test(v)) {
67
+ return false;
68
+ }
69
+ const firstLetter = v[0];
70
+ const digits = v.slice(1, 8);
71
+ const control = v[8];
72
+ let sum = 0;
73
+ for (let i = 0; i < digits.length; i += 1) {
74
+ let n = parseInt(digits[i], 10);
75
+ if (i % 2 === 0) {
76
+ n *= 2;
77
+ if (n > 9) {
78
+ n -= 9;
79
+ }
80
+ }
81
+ sum += n;
82
+ }
83
+ const controlDigit = (10 - sum % 10) % 10;
84
+ const controlLetter = CIF_LETTERS[controlDigit];
85
+ if (CIF_LETTER_CONTROL.includes(firstLetter)) {
86
+ return control === controlLetter;
87
+ }
88
+ if (CIF_DIGIT_CONTROL.includes(firstLetter)) {
89
+ return control === String(controlDigit);
90
+ }
91
+ return control === String(controlDigit) || control === controlLetter;
92
+ }
93
+ function isValidNIF(value) {
94
+ return isValidDNI(value) || isValidNIE(value);
95
+ }
96
+ function isValid(value) {
97
+ return isValidDNI(value) || isValidNIE(value) || isValidCIF(value);
98
+ }
99
+ function getType(value) {
100
+ if (isValidDNI(value)) {
101
+ return "DNI";
102
+ }
103
+ if (isValidNIE(value)) {
104
+ return "NIE";
105
+ }
106
+ if (isValidCIF(value)) {
107
+ return "CIF";
108
+ }
109
+ return null;
110
+ }
111
+ function getCifOrganizationType(value) {
112
+ if (!isValidCIF(value)) {
113
+ return null;
114
+ }
115
+ const code = normalize(value)[0];
116
+ return { code, description: CIF_ORGANIZATION_TYPES[code] };
117
+ }
118
+ function parse(value) {
119
+ const normalized = normalize(value);
120
+ const type = getType(value);
121
+ return {
122
+ valid: type !== null,
123
+ type,
124
+ normalized,
125
+ organization: type === "CIF" ? getCifOrganizationType(value) : null
126
+ };
127
+ }
128
+ function format(value, options = {}) {
129
+ const v = normalize(value);
130
+ const separator = options.separator ?? "";
131
+ if (!separator || !isValid(v)) {
132
+ return v;
133
+ }
134
+ return `${v.slice(0, -1)}${separator}${v.slice(-1)}`;
135
+ }
136
+ function validateNif(value) {
137
+ if (isValidDNI(value)) {
138
+ return ValidationResult.DNI;
139
+ }
140
+ if (isValidNIE(value)) {
141
+ return ValidationResult.NIE;
142
+ }
143
+ if (isValidCIF(value)) {
144
+ return ValidationResult.CIF;
145
+ }
146
+ return ValidationResult.ERROR;
147
+ }
148
+
149
+ export { ValidationResult, controlLetterFor, validateNif as default, format, getCifOrganizationType, getType, isValid, isValidCIF, isValidDNI, isValidNIE, isValidNIF, normalize, parse, validateNif };
package/package.json ADDED
@@ -0,0 +1,73 @@
1
+ {
2
+ "name": "@maistik/validate-nif",
3
+ "version": "2.0.1",
4
+ "description": "Zero-dependency, isomorphic validator for Spanish fiscal identifiers (DNI/NIF, NIE and CIF)",
5
+ "type": "module",
6
+ "private": false,
7
+ "main": "./dist/index.cjs",
8
+ "module": "./dist/index.mjs",
9
+ "types": "./dist/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.mjs",
14
+ "require": "./dist/index.cjs"
15
+ }
16
+ },
17
+ "sideEffects": false,
18
+ "files": [
19
+ "dist"
20
+ ],
21
+ "engines": {
22
+ "node": ">=18.0.0"
23
+ },
24
+ "scripts": {
25
+ "build": "unbuild",
26
+ "stub": "unbuild --stub",
27
+ "lint": "eslint .",
28
+ "lint:fix": "eslint . --fix",
29
+ "typecheck": "tsc --noEmit",
30
+ "test": "vitest run",
31
+ "test:watch": "vitest watch",
32
+ "test:coverage": "vitest run --coverage",
33
+ "test:examples": "node examples/index.mjs",
34
+ "prepack": "unbuild",
35
+ "release": "npm run lint && npm run test:coverage && npm run build && changelogen --release && npm publish --access public && git push --follow-tags"
36
+ },
37
+ "keywords": [
38
+ "validate",
39
+ "validation",
40
+ "nif",
41
+ "nie",
42
+ "dni",
43
+ "cif",
44
+ "spain",
45
+ "spanish",
46
+ "fiscal",
47
+ "tax-id",
48
+ "isomorphic",
49
+ "typescript"
50
+ ],
51
+ "author": "Maistik Studio",
52
+ "license": "MIT",
53
+ "repository": {
54
+ "type": "git",
55
+ "url": "git+https://github.com/Maistik-Studio/validate-nif.git"
56
+ },
57
+ "bugs": {
58
+ "url": "https://github.com/Maistik-Studio/validate-nif/issues"
59
+ },
60
+ "homepage": "https://github.com/Maistik-Studio/validate-nif#readme",
61
+ "devDependencies": {
62
+ "@eslint/js": "^10.0.1",
63
+ "@vitest/coverage-v8": "^4.1.8",
64
+ "changelogen": "^0.6.2",
65
+ "eslint": "^10.5.0",
66
+ "typescript": "^6.0.3",
67
+ "typescript-eslint": "^8.61.0",
68
+ "unbuild": "^3.6.1",
69
+ "vite": "^7.3.5",
70
+ "vitest": "^4.1.8"
71
+ },
72
+ "packageManager": "pnpm@11.6.0"
73
+ }