diff --git a/README.md b/README.md index e543cf7..f6475e7 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,10 @@ randomNIEWith('X', 'E', 1); //=> 'X2080280E' `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: +```js +yarn build && yarn benchmark +``` + ### isValid | lib | method | operations/sec | | @@ -87,8 +91,8 @@ randomNIEWith('X', 'E', 1); //=> 'X2080280E' | lib | method | operations/sec | | | --------------------- | ---------- | -------------- | ----------- | -| better-dni | #ctrlChar | **9,328,614** | 2.3x faster | -| dni-js | #getLetter | 3,947,197 | | +| better-dni | #ctrlChar | **10,874,568** | 5.3x faster | +| dni-js | #getLetter | 2,032,845 | | | dni-js-validator | no method | - | | | @willowi/validate-nif | no method | - | | diff --git a/benchmark.js b/benchmark.js index a538c86..9a07af3 100644 --- a/benchmark.js +++ b/benchmark.js @@ -96,7 +96,7 @@ benches.push( }) ); -Benchmark.invoke([benches[5]], { +Benchmark.invoke([...benches], { name: 'run', args: true, queued: true diff --git a/dist/index.js b/dist/index.js index c603dd4..0e1d243 100644 --- a/dist/index.js +++ b/dist/index.js @@ -45,16 +45,17 @@ // _Random :: https://gist.github.com/blixt/f17b47c62508be59987b#file-prng-js - const LETTERS = 'TRWAGMYFPDXBNJZSQVHLCKE'; - const _upper = x => x.toUpperCase(); - - const _letter = x => LETTERS[+x % 23]; - const _randStrLimit = limit => ('' + Math.random()).substr(-limit); + const _letter = x => 'trwagmyfpdxbnjzsqvhlcke'[+x % 23]; + const _randStrLimit = limit => `${Math.random()}`.slice(-limit); const _randFloat = seed => (new _Random(seed).next() - 1) / 2147483646; const _char = y => { - const f = { X: '0', Y: '1', Z: '2' }[y[0]] || y[0]; - const i = f + '' + y.substr(1, 7); + // Get a number from 0 - 2 when `y` is a NIE + let f = 'xyzXYZ'.indexOf(y[0]) % 3; + // Otherwise default to the number (NIF case only) + if (f === -1) f = y[0]; + // Strip the letters + const i = `${f}${y.slice(1, 8)}`; return _letter(i); }; @@ -70,7 +71,7 @@ * ctrlChar("03118880B"); // => 'B' * ctrlChar("03118880"); // => 'B' */ - const ctrlChar = x => _char(x.toUpperCase()); + const ctrlChar = x => _char(x).toUpperCase(); /** * Returns true if the string is a NIE @@ -81,7 +82,9 @@ * isNIE("X4108613P"); // => true */ const isNIE = value => { - return !!value && value.length === 9 && _isNIE(value) && ctrlChar(value) === _upper(value[8]); + return ( + !!value && value.length === 9 && _isNIE(value) && ctrlChar(value) === value[8].toUpperCase() + ); }; /** @@ -93,7 +96,9 @@ * isNIF("93375221M"); // => true */ const isNIF = value => { - return !!value && value.length === 9 && _isNIF(value) && ctrlChar(value) === _upper(value[8]); + return ( + !!value && value.length === 9 && _isNIF(value) && ctrlChar(value) === value[8].toUpperCase() + ); }; /** @@ -118,7 +123,8 @@ const randomNIE = () => { const r = Math.floor(Math.random() * 3); const nn = _randStrLimit(7); - return ['X', 'Y', 'Z'][r] + nn + _letter(+(r + '' + nn)); + const l = _letter(+`${r}${nn}`); + return `${'XYZ'[r]}${nn}${l}`; }; /** @@ -203,3 +209,4 @@ Object.defineProperty(exports, '__esModule', { value: true }); }))); +//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map new file mode 100644 index 0000000..4d2b7f8 --- /dev/null +++ b/dist/index.js.map @@ -0,0 +1 @@ +{"version":3,"file":"index.js","sources":["../src/internal/_isNIE.js","../src/internal/_isNIF.js","../src/isValid.js","../src/internal/_utils.js","../src/internal/_char.js","../src/ctrlChar.js","../src/isNIE.js","../src/isNIF.js","../src/randomNIF.js","../src/randomNIE.js","../src/randomNIFWith.js","../src/randomNIEWith.js"],"sourcesContent":["export const _isNIE = v => /^[XYZ]{1}[0-9]{7}[TRWAGMYFPDXBNJZSQVHLCKE]{1}$/i.test(v);\n","export const _isNIF = v => /^[0-9]{8}[TRWAGMYFPDXBNJZSQVHLCKE]{1}$/i.test(v);\n","import { _isNIE } from './internal/_isNIE';\nimport { _isNIF } from './internal/_isNIF';\n\n/**\n * Returns true if the string is a valid DNI (NIF or NIE)\n * @param {string} value\n * @returns {boolean}\n * @since 1.1.0\n * @example\n * isValid(\"X9464186P\"); // => true\n * isValid(\"03118880B\"); // => true\n */\nconst isValid = value => {\n const dni = (!value ? '' : value).toLowerCase(); // lowercase is faster\n\n if (dni.length !== 9 && !_isNIE(dni) && !_isNIF(dni)) return false;\n\n const f = { x: '0', y: '1', z: '2' }[dni[0]] || dni[0];\n const dni_1_to_7 = dni.substr(1, 7);\n const i = +(f + dni_1_to_7) % 23;\n\n return 'trwagmyfpdxbnjzsqvhlcket'[i] === dni[8];\n};\n\nexport { isValid };\n","function _Random(seed) {\n this._seed = seed % 2147483647;\n if (this._seed <= 0) this._seed += 2147483646;\n}\n\n_Random.prototype.next = function() {\n return (this._seed = (this._seed * 16807) % 2147483647);\n};\n\n// _Random :: https://gist.github.com/blixt/f17b47c62508be59987b#file-prng-js\n\nconst _letter = x => 'trwagmyfpdxbnjzsqvhlcke'[+x % 23];\nconst _randStrLimit = limit => `${Math.random()}`.slice(-limit);\nconst _randFloat = seed => (new _Random(seed).next() - 1) / 2147483646;\n\nexport { _letter, _randStrLimit, _randFloat };\n","import { _letter } from './_utils';\n\nconst _char = y => {\n // Get a number from 0 - 2 when `y` is a NIE\n let f = 'xyzXYZ'.indexOf(y[0]) % 3;\n // Otherwise default to the number (NIF case only)\n if (f === -1) f = y[0];\n // Strip the letters\n const i = `${f}${y.slice(1, 8)}`;\n return _letter(i);\n};\n\nexport { _char };\n","import { _char } from './internal/_char';\n\n/**\n * Returns the control letter in upper case\n * for a NIF or NIE with or without control letter\n * @param {string} value\n * @returns {string}\n * @since 1.9.1\n * @example\n * ctrlChar(\"X9464186P\"); // => 'P'\n * ctrlChar(\"X9464186\"); // => 'P'\n * ctrlChar(\"03118880B\"); // => 'B'\n * ctrlChar(\"03118880\"); // => 'B'\n */\nconst ctrlChar = x => _char(x).toUpperCase();\n\nexport { ctrlChar };\n","import { _isNIE } from './internal/_isNIE';\nimport { ctrlChar } from './ctrlChar';\n\n/**\n * Returns true if the string is a NIE\n * @param {string} value\n * @returns {boolean}\n * @since 1.0.1\n * @example\n * isNIE(\"X4108613P\"); // => true\n */\nconst isNIE = value => {\n return (\n !!value && value.length === 9 && _isNIE(value) && ctrlChar(value) === value[8].toUpperCase()\n );\n};\n\nexport { isNIE };\n","import { _isNIF } from './internal/_isNIF';\nimport { ctrlChar } from './ctrlChar';\n\n/**\n * Returns true if the string is a NIF\n * @param {string} value\n * @returns {boolean}\n * @since 1.0.1\n * @example\n * isNIF(\"93375221M\"); // => true\n */\nconst isNIF = value => {\n return (\n !!value && value.length === 9 && _isNIF(value) && ctrlChar(value) === value[8].toUpperCase()\n );\n};\n\nexport { isNIF };\n","import { _randStrLimit, _letter } from './internal/_utils';\n\n/**\n * Returns a valid NIF string\n * @returns {string}\n * @since 1.8.1\n * @example\n * randomNIF() // => \"93375221M\"\n */\nconst randomNIF = () => {\n const nn = _randStrLimit(8);\n return nn + _letter(nn);\n};\n\nexport { randomNIF };\n","import { _randStrLimit, _letter } from './internal/_utils';\n\n/**\n * Returns a valid NIE string\n * @returns {string}\n * @since 1.8.1\n * @example\n * randomNIE() // => \"X4108613P\"\n */\nconst randomNIE = () => {\n const r = Math.floor(Math.random() * 3);\n const nn = _randStrLimit(7);\n const l = _letter(+`${r}${nn}`);\n return `${'XYZ'[r]}${nn}${l}`;\n};\n\nexport { randomNIE };\n","import { _randFloat } from './internal/_utils';\n\n/**\n * Returns a random NIF with a specific letter\n * A seed can be passed as a second parameter and\n * it will always return the same value\n * @returns {string | null}\n * @since 1.11.0\n * @example\n * with random seed\n * randomNIFWith('C'); //=> '95652190C'\n * randomNIFWith('G'); //=> '60869550G'\n *\n * with the same seed\n * randomNIFWith('G', 1); //=> '95652174G'\n * randomNIFWith('G', 1); //=> '95652174G'\n */\nconst randomNIFWith = (char, seed = 100000000 * Math.random()) => {\n const lastNum = 'trwagmyfpdxbnjzsqvhlckeTRWAGMYFPDXBNJZSQVHLCKE'.indexOf(char) % 23;\n\n if (lastNum === -1) return null;\n\n // TODO: Better calculation\n const n = 99999998 - 4347826 * (Math.floor(_randFloat(seed) * 22) + 1);\n\n const d = Math.max(0, n) % 23;\n const h = n + (lastNum - d);\n const s = `0${h}`.slice(-8);\n\n return `${s}${char}`.toUpperCase();\n};\n\nexport { randomNIFWith };\n","import { _randFloat } from './internal/_utils';\nimport { ctrlChar } from './ctrlChar';\n\n/**\n * Returns a random NIE with a specific letter\n * A seed can be passed as a third parameter and\n * it will always return the same value\n * @param head {string}\n * @param char {string}\n * @returns {string | null}\n * @since 1.12.0\n * @example\n * with random seed\n * randomNIEWith('Y', 'C'); //=> 'Y2098020C'\n * randomNIEWith('Z', 'G'); //=> 'Z5670557G'\n *\n * with the same seed\n * randomNIEWith('X', 'E', 1); //=> 'X2080280E'\n * randomNIEWith('X', 'E', 1); //=> 'X2080280E'\n */\nconst randomNIEWith = (xyz, l, seed = 100000000 * Math.random()) => {\n const headNum = 'xyzXYZ'.indexOf(xyz) % 3;\n\n if (headNum === -1) return null;\n\n const lastNum = 'trwagmyfpdxbnjzsqvhlckeTRWAGMYFPDXBNJZSQVHLCKE'.indexOf(l) % 23;\n\n if (lastNum === -1) return null;\n\n const headOne = headNum + 1;\n\n // TODO: Better calculation\n const num = Math.floor(1000000 * headOne + (9999999 - 1000000 * headOne - 23) * _randFloat(seed));\n\n const b = +`${headNum}${num}`;\n const rest = b % 23;\n const h = b - rest + lastNum;\n\n const s = `0${h}${l}`.slice(-8);\n\n return `${xyz}${s}`.toUpperCase();\n};\n\nexport { randomNIEWith };\n"],"names":[],"mappings":";;;;;;;;;;;EAAO,MAAM,MAAM,GAAG,CAAC,IAAI,iDAAiD,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;;ECA9E,MAAM,MAAM,GAAG,CAAC,IAAI,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AI,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC;;EAEpG,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC,EAAE,OAAO,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC;EAChC,EAAE,MAAM,IAAI,GAAG,CAAC,GAAG,EAAE,CAAC;EACtB,EAAE,MAAM,CAAC,GAAG,CAAC,GAAG,IAAI,GAAG,OAAO,CAAC;;EAE/B,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;;EAElC,EAAE,OAAO,CAAC,EAAE,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC;EACpC,CAAC;;;;;;;;;;;;;;;;;;;"} \ No newline at end of file diff --git a/package.json b/package.json index 6aaa8c8..b7fb103 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "types": "types/index.d.ts", "files": [ "dist/index.js", + "dist/index.js.map", "types/index.d.ts" ] } diff --git a/rollup.config.js b/rollup.config.js index 1640983..88460a2 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -11,6 +11,7 @@ const banner = '// Better DNI may be freely distributed under the MIT license.\n'; export default { + sourcemap: true, input: 'src/index.js', output: { banner, diff --git a/src/ctrlChar.js b/src/ctrlChar.js index b4e384e..be85170 100644 --- a/src/ctrlChar.js +++ b/src/ctrlChar.js @@ -12,6 +12,6 @@ import { _char } from './internal/_char'; * ctrlChar("03118880B"); // => 'B' * ctrlChar("03118880"); // => 'B' */ -const ctrlChar = x => _char(x.toUpperCase()); +const ctrlChar = x => _char(x).toUpperCase(); export { ctrlChar }; diff --git a/src/internal/_char.js b/src/internal/_char.js index 5956af2..1c4d732 100644 --- a/src/internal/_char.js +++ b/src/internal/_char.js @@ -1,8 +1,12 @@ import { _letter } from './_utils'; const _char = y => { - const f = { X: '0', Y: '1', Z: '2' }[y[0]] || y[0]; - const i = f + '' + y.substr(1, 7); + // Get a number from 0 - 2 when `y` is a NIE + let f = 'xyzXYZ'.indexOf(y[0]) % 3; + // Otherwise default to the number (NIF case only) + if (f === -1) f = y[0]; + // Strip the letters + const i = `${f}${y.slice(1, 8)}`; return _letter(i); }; diff --git a/src/internal/_utils.js b/src/internal/_utils.js index e4a2b7d..a6286b6 100644 --- a/src/internal/_utils.js +++ b/src/internal/_utils.js @@ -9,17 +9,8 @@ _Random.prototype.next = function() { // _Random :: https://gist.github.com/blixt/f17b47c62508be59987b#file-prng-js -const LETTERS = 'TRWAGMYFPDXBNJZSQVHLCKE'; - -const _idxOf = x => y => x.indexOf(y); -const _xyzAsNum = _idxOf('xyz'); -const _lastIndex = _idxOf(LETTERS); -const _upper = x => x.toUpperCase(); - -const _letter = x => LETTERS[+x % 23]; -const _randStrLimit = limit => ('' + Math.random()).substr(-limit); +const _letter = x => 'trwagmyfpdxbnjzsqvhlcke'[+x % 23]; +const _randStrLimit = limit => `${Math.random()}`.slice(-limit); const _randFloat = seed => (new _Random(seed).next() - 1) / 2147483646; -const compose = (...fns) => x => fns.reduceRight((acc, fn) => fn(acc), x); - -export { _letter, _randStrLimit, _Random, _lastIndex, _upper, _randFloat, _xyzAsNum }; +export { _letter, _randStrLimit, _randFloat }; diff --git a/src/isNIE.js b/src/isNIE.js index a68f9a4..4a9bd58 100644 --- a/src/isNIE.js +++ b/src/isNIE.js @@ -1,5 +1,4 @@ import { _isNIE } from './internal/_isNIE'; -import { _upper } from './internal/_utils'; import { ctrlChar } from './ctrlChar'; /** @@ -11,7 +10,9 @@ import { ctrlChar } from './ctrlChar'; * isNIE("X4108613P"); // => true */ const isNIE = value => { - return !!value && value.length === 9 && _isNIE(value) && ctrlChar(value) === _upper(value[8]); + return ( + !!value && value.length === 9 && _isNIE(value) && ctrlChar(value) === value[8].toUpperCase() + ); }; export { isNIE }; diff --git a/src/isNIF.js b/src/isNIF.js index e3e6735..d62cfc8 100644 --- a/src/isNIF.js +++ b/src/isNIF.js @@ -1,5 +1,4 @@ import { _isNIF } from './internal/_isNIF'; -import { _upper } from './internal/_utils'; import { ctrlChar } from './ctrlChar'; /** @@ -11,7 +10,9 @@ import { ctrlChar } from './ctrlChar'; * isNIF("93375221M"); // => true */ const isNIF = value => { - return !!value && value.length === 9 && _isNIF(value) && ctrlChar(value) === _upper(value[8]); + return ( + !!value && value.length === 9 && _isNIF(value) && ctrlChar(value) === value[8].toUpperCase() + ); }; export { isNIF }; diff --git a/src/randomNIE.js b/src/randomNIE.js index d3a7972..7dad0c5 100644 --- a/src/randomNIE.js +++ b/src/randomNIE.js @@ -10,7 +10,8 @@ import { _randStrLimit, _letter } from './internal/_utils'; const randomNIE = () => { const r = Math.floor(Math.random() * 3); const nn = _randStrLimit(7); - return ['X', 'Y', 'Z'][r] + nn + _letter(+(r + '' + nn)); + const l = _letter(+`${r}${nn}`); + return `${'XYZ'[r]}${nn}${l}`; }; export { randomNIE }; diff --git a/src/randomNIEWith.js b/src/randomNIEWith.js index 90baea4..672c851 100644 --- a/src/randomNIEWith.js +++ b/src/randomNIEWith.js @@ -1,4 +1,4 @@ -import { _upper, _randFloat } from './internal/_utils'; +import { _randFloat } from './internal/_utils'; import { ctrlChar } from './ctrlChar'; /**