Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

dni-js

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dni-js - npm Package Compare versions

Comparing version 0.1.1 to 0.2.0

6

package.json
{
"name": "dni-js",
"version": "0.1.1",
"version": "0.2.0",
"description": "Compute and validate a Spanish DNI/NIE number",

@@ -27,5 +27,5 @@ "main": "src/index.js",

"devDependencies": {
"jest": "^20.0.4",
"unexpected": "^10.32.1"
"jest": "^24.8.0",
"unexpected": "^11.6.0"
}
}
# dni-js
Compute and validate a Spanish DNI/NIE numbers as described [here](http://www.interior.gob.es/web/servicios-al-ciudadano/dni/calculo-del-digito-de-control-del-nif-nie).

@@ -47,4 +48,12 @@

#### `.normalize (string)`
Given a string input, it returns a normalized valid DNI. When input is either not a string, or invalid, it returns null. For example:
```js
dni.normalize(' 12 34 56 7 8-z'); // 12345678-Z
```
## License
MIT
MIT

@@ -19,3 +19,6 @@ const LETTERS = require('./letterMap');

const digits = NIE_NUMBER_REGEXP.test(input)
? String(input).replace('X', 0).replace('Y', 1).replace('Z', 2)
? String(input)
.replace('X', 0)
.replace('Y', 1)
.replace('Z', 2)
: input;

@@ -29,3 +32,5 @@

? DNI_REGEXP
: NIE_REGEXP.test(dni) ? NIE_REGEXP : null;
: NIE_REGEXP.test(dni)
? NIE_REGEXP
: null;

@@ -40,5 +45,13 @@ if (!matcher) {

const normalize = (input = '') => {
if (!input || typeof input !== 'string') return null;
input = input.replace(/\s/g, '').toUpperCase();
return isValid(input) ? input : null;
};
module.exports = {
dni,
nie: dni,
normalize,
getControlDigit,

@@ -45,0 +58,0 @@ getLetter: getControlDigit,

const unexpected = require('unexpected');
const { dni, nie, isNIE, getControlDigit, isValid } = require('.');
const { dni, nie, getControlDigit, isValid, normalize } = require('.');

@@ -84,2 +84,18 @@ const expect = unexpected

});
describe('normalize', () => {
describe('for a valid input', () => {
it('returns a normalized string', () => {
expect(normalize(' 12 34 56 7 8-z'), 'to equal', '12345678-Z');
});
});
describe('for an invalid input', () => {
[' 12 34 56 7 8-b', null, undefined, ' ', '', 1].forEach(input => {
it(`returns null for input "${input}"`, () => {
expect(normalize(input), 'to equal', null);
});
});
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc