npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

better-dni

v5.3.0

Published

The fastest Spanish DNI (NIE / NIF) validation out there.

Readme

npm Codacy grade Coveralls github Known Vulnerabilities npm bundle size (minified)

Better DNI

The fastest Spanish DNI (NIE / NIF) validation out there — And lighter: 897 bytes (minified + brotli).

Installation

// npm
npm i better-dni

// pnpm
pnpm add better-dni

// yarn
yarn add better-dni

Upgrading to v5

  • Requires Node.js 20 or newer.
  • Ships both ES modules (import) and CommonJS (require), with bundled TypeScript types.
  • Only the package root (better-dni) can be imported; deep imports such as better-dni/dist/... are no longer supported.

The API is unchanged.

Usage

isValid / isNIF / isNIE / ctrlChar

import { isValid, isNIF, isNIE, ctrlChar } from "better-dni";

// validates any type of DNI (NIE or NIF)
isValid("Z7662566Y"); //=> true

// checks if it is a valid NIF
isNIF("06672804K"); //=> true
isNIF("K1111111G"); //=> true (NIF with K, L or M)

// checks if it is a valid NIE
isNIE("X1302311M"); //=> true

// returns the control letter in upper case
ctrlChar("X1302311M"); //=> 'M'
ctrlChar("X1302311"); //=> 'M'

Generators

NIF

import { randomNIF, randomNIFWith } from "better-dni";

randomNIF(); //=> '31719111H'

// returns a valid random NIF given the control letter
randomNIFWith("C"); // => '95652190C'

// a seed (Number) can be provided and it will always return the same result
randomNIFWith("G", 1); //=> '95652174G'
randomNIFWith("G", 1); //=> '95652174G'

NIE

import { randomNIE, randomNIEWith } from "better-dni";

randomNIE(); //=> 'X1120409X'

// returns a valid random NIE given the first and control letter
randomNIEWith("Y", "C"); //=> 'Y2098020C'
randomNIEWith("Z", "G"); //=> 'Z5670557G'

// a seed (Number) can be provided and it will always return the same result
randomNIEWith("X", "E", 1); //=> 'X2080280E'
randomNIEWith("X", "E", 1); //=> 'X2080280E'

Normalize

// Returns a "clean" dni string removing spaces, dashes, etc.
normalize(" x-9464186_p   "); // => "X9464186P"

Benchmark

better-dni does a similar job as other libraries like dni-js, dni-js-validator, and @willowi/validate-nif but better-dni is built with optimization and speed in mind. Take a look at these benchmark results:

pnpm --filter better-dni benchmark

isValid

| lib | method | time | diff | | --------------------- | ------------ | ------- | ------------- | | better-dni | #isValid | 84 ms | | | dni-js | #isValid | 859 ms | x10.23 slower | | dni-js-validator | #isValid | 980 ms | x11.67 slower | | @willowi/validate-nif | #validateNif | 4.24 s | x50.48 slower |

ctrlChar / getControlDigit

| lib | method | time | diff | | | --------------------- | ---------------- | ------ | ------------ | -- | | better-dni | #ctrlChar | 355 ms | | | | dni-js | #getControlDigit | 313 ms | x1.13 faster | 😲 | | dni-js-validator | no method | - | | | | @willowi/validate-nif | no method | - | | |

10,000,000 calls per method, median of 3 runs. MacBook Pro M3 Pro (14-inch, 2023) - 36 GB, Node v22.22.0

Demo

https://better-dni.singuerinc.com/

Related

Reference

https://www.interior.gob.es/opencms/ca/servicios-al-ciudadano/tramites-y-gestiones/dni/calculo-del-digito-de-control-del-nif-nie/