Comparing version 0.1.1 to 0.2.0
{ | ||
"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); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7632
157
59
7