{"id":22994570,"url":"https://github.com/singuerinc/better-dni","last_synced_at":"2025-08-20T03:48:24.981Z","repository":{"id":50839740,"uuid":"133929200","full_name":"singuerinc/better-dni","owner":"singuerinc","description":"The fastest Spanish DNI (NIE / NIF) validation out there.","archived":false,"fork":false,"pushed_at":"2023-11-30T13:57:07.000Z","size":1355,"stargazers_count":31,"open_issues_count":2,"forks_count":4,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-12-05T03:15:46.440Z","etag":null,"topics":["dni","javascript","nie","nif","spain","validation"],"latest_commit_sha":null,"homepage":"https://better-dni.singuerinc.com/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/singuerinc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2018-05-18T08:54:47.000Z","updated_at":"2024-07-22T09:09:47.000Z","dependencies_parsed_at":"2023-12-22T10:13:38.968Z","dependency_job_id":"9e86a108-bfff-4eb5-967f-d4422dfba00a","html_url":"https://github.com/singuerinc/better-dni","commit_stats":{"total_commits":190,"total_committers":7,"mean_commits":"27.142857142857142","dds":"0.13157894736842102","last_synced_commit":"fea659fb434747e096ed9110d0d21720ba2650aa"},"previous_names":[],"tags_count":57,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/singuerinc%2Fbetter-dni","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/singuerinc%2Fbetter-dni/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/singuerinc%2Fbetter-dni/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/singuerinc%2Fbetter-dni/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/singuerinc","download_url":"https://codeload.github.com/singuerinc/better-dni/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":229784376,"owners_count":18123617,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dni","javascript","nie","nif","spain","validation"],"created_at":"2024-12-15T05:19:23.774Z","updated_at":"2024-12-15T05:19:24.406Z","avatar_url":"https://github.com/singuerinc.png","language":"TypeScript","funding_links":[],"categories":["Validación de Documentos"],"sub_categories":["Metro y Cercanías"],"readme":"[![npm](https://img.shields.io/npm/v/better-dni.svg)](http://npm.im/better-dni)\n[![Codacy grade](https://img.shields.io/codacy/grade/83d00fabfa424b0dbba64735f64ff74c.svg?style=flat-square)](https://app.codacy.com/app/nahuel.scotti/better-dni)\n[![Coveralls github](https://img.shields.io/coveralls/github/singuerinc/better-dni.svg?style=flat-square)](https://coveralls.io/github/singuerinc/better-dni?branch=master)\n[![Known Vulnerabilities](https://snyk.io/test/github/singuerinc/better-dni/badge.svg?style=flat-square)](https://snyk.io/test/github/singuerinc/better-dni)\n![npm bundle size (minified)](https://img.shields.io/bundlephobia/min/better-dni.svg?style=flat-square)\n\n![Better DNI](logo.png)\n\nThe fastest Spanish DNI (NIE / NIF) validation out there — And lighter: 660 bytes.\n\n## Installation\n\n```js\n// npm\nnpm i better-dni\n\n// yarn\nyarn add better-dni\n```\n\n## Usage\n\n### isValid / isNIF / isNIE / ctrlChar\n\n```js\nimport { isValid, isNIF, isNIE, ctrlChar } from \"better-dni\";\n\n// validates any type of DNI (NIE or NIF)\nisValid(\"Z7662566Y\"); //=\u003e true\n\n// checks if it is a valid NIF\nisNIF(\"06672804K\"); //=\u003e true\n\n// checks if it is a valid NIE\nisNIE(\"X1302311M\"); //=\u003e true\n\n// returns the control letter in upper case\nctrlChar(\"X1302311M\"); //=\u003e 'M'\nctrlChar(\"X1302311\"); //=\u003e 'M'\n```\n\n### Generators\n\n#### NIF\n\n```js\nimport { randomNIF, randomNIFWith } from \"better-dni\";\n\nrandomNIF(); //=\u003e '31719111H'\n\n// returns a valid random NIF given the control letter\nrandomNIFWith(\"C\"); // =\u003e '95652190C'\n\n// a seed (Number) can be provided and it will always return the same result\nrandomNIFWith(\"G\", 1); //=\u003e '95652174G'\nrandomNIFWith(\"G\", 1); //=\u003e '95652174G'\n```\n\n#### NIE\n\n```js\nimport { randomNIE, randomNIEWith } from \"better-dni\";\n\nrandomNIE(); //=\u003e 'X1120409X'\n\n// returns a valid random NIE given the first and control letter\nrandomNIEWith(\"Y\", \"C\"); //=\u003e 'Y2098020C'\nrandomNIEWith(\"Z\", \"G\"); //=\u003e 'Z5670557G'\n\n// a seed (Number) can be provided and it will always return the same result\nrandomNIEWith(\"X\", \"E\", 1); //=\u003e 'X2080280E'\nrandomNIEWith(\"X\", \"E\", 1); //=\u003e 'X2080280E'\n```\n\n#### Normalize\n\n```js\n// Returns a \"clean\" dni string removing spaces, dashes, etc.\nnormalize(\" x-9464186_p   \"); // =\u003e \"X9464186P\"\n```\n\n## Benchmark\n\n`better-dni` does a similar job as other libraries like [dni-js](https://github.com/albertfdp/dni-js/), [dni-js-validator](https://github.com/idirouhab/dni-js-validator), and [@willowi/validate-nif](https://github.com/WillowiDev/validate-nif) but `better-dni` is built with optimization and speed in mind. Take a look at these benchmark results:\n\n```sh\nyarn run benchmark\n```\n\n### isValid\n\n| lib                   | method       | time               | diff          |\n| --------------------- | ------------ | ------------------ | ------------- |\n| better-dni            | #isValid     | 0 s + 365932959 ns |               |\n| dni-js-validator      | #isValid     | 1 s + 154193500 ns | x3.15 slower  |\n| dni-js                | #isValid     | 1 s + 188613500 ns | x3.24 slower  |\n| @willowi/validate-nif | #validateNif | 4 s + 986185000 ns | x13.62 slower |\n\n### ctrlChar / getLetter\n\n| lib                   | method     | time               | diff         |    |\n| --------------------- | ---------- | ------------------ | ------------ |----|\n| better-dni            | #ctrlChar  | 0 s + 423324167 ns |              |    |\n| dni-js                | #getLetter | 0 s + 354445959 ns | x1.16 faster | 😲 |\n| dni-js-validator      | no method  | -                  |              |    |\n| @willowi/validate-nif | no method  | -                  |              |    |\n\n\u003e Benchmark on a MacBook M2 Pro (16-inch, 2023) - 16 GB Node v20.7.0\n\n## Demo\n\n[https://better-dni.singuerinc.com/](https://better-dni.singuerinc.com/)\n\n## Related\n\n- [spain-phone](https://github.com/singuerinc/spain-phone) - Spanish phone number validation\n\n## Reference\n\n[https://www.interior.gob.es/opencms/ca/servicios-al-ciudadano/tramites-y-gestiones/dni/calculo-del-digito-de-control-del-nif-nie/](https://www.interior.gob.es/opencms/ca/servicios-al-ciudadano/tramites-y-gestiones/dni/calculo-del-digito-de-control-del-nif-nie/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsinguerinc%2Fbetter-dni","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsinguerinc%2Fbetter-dni","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsinguerinc%2Fbetter-dni/lists"}