validate-polish
Advanced tools
Comparing version 2.0.0 to 2.0.1
@@ -1,179 +0,189 @@ | ||
"use strict"; | ||
exports.__esModule = true; | ||
exports.validatePolish = void 0; | ||
exports.validatePolish = { | ||
checksum: function (number, weights) { | ||
var max = number.length - 1; | ||
var sum = 0; | ||
for (var i = 0; i < max; i++) { | ||
var n = parseInt(number[i], 10); | ||
sum += n * weights[i]; | ||
} | ||
var resultSum = sum % 11 !== 10 ? sum % 11 : 0; | ||
var lastDigit = parseInt(number.slice(-1), 10); | ||
return resultSum === lastDigit; | ||
}, | ||
/** | ||
* Validation of PESEL. | ||
* @param {string} pesel | ||
* @returns {boolean} | ||
*/ | ||
pesel: function (pesel) { | ||
if (!/^[0-9]{11}$/.test(pesel)) { | ||
(function (factory) { | ||
if (typeof module === "object" && typeof module.exports === "object") { | ||
var v = factory(require, exports); | ||
if (v !== undefined) module.exports = v; | ||
} | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports"], factory); | ||
} | ||
})(function (require, exports) { | ||
"use strict"; | ||
exports.__esModule = true; | ||
exports.validatePolish = void 0; | ||
exports.validatePolish = { | ||
checksum: function (number, weights) { | ||
var max = number.length - 1; | ||
var sum = 0; | ||
for (var i = 0; i < max; i++) { | ||
var n = parseInt(number[i], 10); | ||
sum += n * weights[i]; | ||
} | ||
var resultSum = sum % 11 !== 10 ? sum % 11 : 0; | ||
var lastDigit = parseInt(number.slice(-1), 10); | ||
return resultSum === lastDigit; | ||
}, | ||
/** | ||
* Validation of PESEL. | ||
* @param {string} pesel | ||
* @returns {boolean} | ||
*/ | ||
pesel: function (pesel) { | ||
if (!/^[0-9]{11}$/.test(pesel)) { | ||
return false; | ||
} | ||
var times = [1, 3, 7, 9]; | ||
var digits = ("" + pesel).split('').map(function (digit) { return parseInt(digit, 10); }); | ||
var dig11 = digits.splice(-1)[0]; | ||
var control = digits.reduce(function (previousValue, currentValue, index) { return previousValue + currentValue * times[index % 4]; }) % 10; | ||
return 10 - (control === 0 ? 10 : control) === dig11; | ||
}, | ||
/** | ||
* Validation of NIP. | ||
* @param {*} nip | ||
* @returns {boolean} | ||
*/ | ||
nip: function (nip) { | ||
if (typeof nip !== 'string') { | ||
return false; | ||
} | ||
var nipWithoutDashes = nip.replace(/-/g, ''); | ||
var reg = /^[0-9]{10}$/; | ||
if (reg.test(nipWithoutDashes) === false) { | ||
return false; | ||
} | ||
var dig = ('' + nipWithoutDashes).split(''); | ||
var control = (6 * parseInt(dig[0], 10) + | ||
5 * parseInt(dig[1], 10) + | ||
7 * parseInt(dig[2], 10) + | ||
2 * parseInt(dig[3], 10) + | ||
3 * parseInt(dig[4], 10) + | ||
4 * parseInt(dig[5], 10) + | ||
5 * parseInt(dig[6], 10) + | ||
6 * parseInt(dig[7], 10) + | ||
7 * parseInt(dig[8], 10)) % | ||
11; | ||
if (parseInt(dig[9], 10) === control) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
var times = [1, 3, 7, 9]; | ||
var digits = ("" + pesel).split('').map(function (digit) { return parseInt(digit, 10); }); | ||
var dig11 = digits.splice(-1)[0]; | ||
var control = digits.reduce(function (previousValue, currentValue, index) { return previousValue + currentValue * times[index % 4]; }) % 10; | ||
return 10 - (control === 0 ? 10 : control) === dig11; | ||
}, | ||
/** | ||
* Validation of NIP. | ||
* @param {*} nip | ||
* @returns {boolean} | ||
*/ | ||
nip: function (nip) { | ||
if (typeof nip !== 'string') { | ||
return false; | ||
} | ||
var nipWithoutDashes = nip.replace(/-/g, ''); | ||
var reg = /^[0-9]{10}$/; | ||
if (reg.test(nipWithoutDashes) === false) { | ||
return false; | ||
} | ||
var dig = ('' + nipWithoutDashes).split(''); | ||
var control = (6 * parseInt(dig[0], 10) + | ||
5 * parseInt(dig[1], 10) + | ||
7 * parseInt(dig[2], 10) + | ||
2 * parseInt(dig[3], 10) + | ||
3 * parseInt(dig[4], 10) + | ||
4 * parseInt(dig[5], 10) + | ||
5 * parseInt(dig[6], 10) + | ||
6 * parseInt(dig[7], 10) + | ||
7 * parseInt(dig[8], 10)) % | ||
11; | ||
if (parseInt(dig[9], 10) === control) { | ||
return true; | ||
} | ||
return false; | ||
}, | ||
/** | ||
* Validation of REGON. | ||
* @param {string} regon | ||
* @returns {boolean} | ||
*/ | ||
regon: function (regon) { | ||
var reg = /^[0-9]{9,14}$/; | ||
if (reg.test(regon) === false) { | ||
return false; | ||
} | ||
var weights9 = [8, 9, 2, 3, 4, 5, 6, 7]; | ||
if (regon.length === 9) { | ||
return exports.validatePolish.checksum(regon, weights9); | ||
} | ||
var weights14 = [2, 4, 8, 5, 0, 9, 7, 3, 6, 1, 2, 4, 8]; | ||
return exports.validatePolish.checksum(regon.slice(0, 9), weights9) && exports.validatePolish.checksum(regon, weights14); | ||
}, | ||
/** | ||
* Validation of identity card. | ||
* @param {*} num | ||
* @returns {boolean} | ||
*/ | ||
identityCard: function (num) { | ||
// Check length | ||
if (!num || num.length !== 9) { | ||
return false; | ||
} | ||
var upperNum = num.toUpperCase(); | ||
var letterValues = [ | ||
'0', | ||
'1', | ||
'2', | ||
'3', | ||
'4', | ||
'5', | ||
'6', | ||
'7', | ||
'8', | ||
'9', | ||
'A', | ||
'B', | ||
'C', | ||
'D', | ||
'E', | ||
'F', | ||
'G', | ||
'H', | ||
'I', | ||
'J', | ||
'K', | ||
'L', | ||
'M', | ||
'N', | ||
'O', | ||
'P', | ||
'Q', | ||
'R', | ||
'S', | ||
'T', | ||
'U', | ||
'V', | ||
'W', | ||
'X', | ||
'Y', | ||
'Z', | ||
]; | ||
var getLetterValue = function (letter) { | ||
for (var j = 0, max = letterValues.length; j < max; j++) { | ||
if (letter === letterValues[j]) { | ||
return j; | ||
}, | ||
/** | ||
* Validation of REGON. | ||
* @param {string} regon | ||
* @returns {boolean} | ||
*/ | ||
regon: function (regon) { | ||
var reg = /^[0-9]{9,14}$/; | ||
if (reg.test(regon) === false) { | ||
return false; | ||
} | ||
var weights9 = [8, 9, 2, 3, 4, 5, 6, 7]; | ||
if (regon.length === 9) { | ||
return exports.validatePolish.checksum(regon, weights9); | ||
} | ||
var weights14 = [2, 4, 8, 5, 0, 9, 7, 3, 6, 1, 2, 4, 8]; | ||
return exports.validatePolish.checksum(regon.slice(0, 9), weights9) && exports.validatePolish.checksum(regon, weights14); | ||
}, | ||
/** | ||
* Validation of identity card. | ||
* @param {*} num | ||
* @returns {boolean} | ||
*/ | ||
identityCard: function (num) { | ||
// Check length | ||
if (!num || num.length !== 9) { | ||
return false; | ||
} | ||
var upperNum = num.toUpperCase(); | ||
var letterValues = [ | ||
'0', | ||
'1', | ||
'2', | ||
'3', | ||
'4', | ||
'5', | ||
'6', | ||
'7', | ||
'8', | ||
'9', | ||
'A', | ||
'B', | ||
'C', | ||
'D', | ||
'E', | ||
'F', | ||
'G', | ||
'H', | ||
'I', | ||
'J', | ||
'K', | ||
'L', | ||
'M', | ||
'N', | ||
'O', | ||
'P', | ||
'Q', | ||
'R', | ||
'S', | ||
'T', | ||
'U', | ||
'V', | ||
'W', | ||
'X', | ||
'Y', | ||
'Z', | ||
]; | ||
var getLetterValue = function (letter) { | ||
for (var j = 0, max = letterValues.length; j < max; j++) { | ||
if (letter === letterValues[j]) { | ||
return j; | ||
} | ||
} | ||
return -1; | ||
}; | ||
// check series | ||
for (var i = 0; i < 3; i++) { | ||
if (getLetterValue(upperNum[i]) < 10) { | ||
return false; | ||
} | ||
} | ||
return -1; | ||
}; | ||
// check series | ||
for (var i = 0; i < 3; i++) { | ||
if (getLetterValue(upperNum[i]) < 10) { | ||
// check number | ||
for (var i = 3; i < 9; i++) { | ||
if (getLetterValue(upperNum[i]) < 0 || getLetterValue(upperNum[i]) > 9) { | ||
return false; | ||
} | ||
} | ||
// checksum | ||
var sum = 7 * getLetterValue(upperNum[0]) + | ||
3 * getLetterValue(upperNum[1]) + | ||
1 * getLetterValue(upperNum[2]) + | ||
7 * getLetterValue(upperNum[4]) + | ||
3 * getLetterValue(upperNum[5]) + | ||
1 * getLetterValue(upperNum[6]) + | ||
7 * getLetterValue(upperNum[7]) + | ||
3 * getLetterValue(upperNum[8]); | ||
sum %= 10; | ||
if (sum !== getLetterValue(upperNum[3])) { | ||
return false; | ||
} | ||
} | ||
// check number | ||
for (var i = 3; i < 9; i++) { | ||
if (getLetterValue(upperNum[i]) < 0 || getLetterValue(upperNum[i]) > 9) { | ||
return true; | ||
}, | ||
/** | ||
* Checks if given number of identity card is valid. | ||
* @param {string} num - series and number of identity card to validate | ||
* @return {boolean} - true if identity card is valid, false if invalid | ||
*/ | ||
identityCardWithSeparator: function (num) { | ||
// check length | ||
if (!num || num.length !== 10) { | ||
return false; | ||
} | ||
// check separator | ||
if (num[3] !== ' ' && num[3] !== '-') { | ||
return false; | ||
} | ||
return this.identityCard(num.replace(/[\s-]/g, '')); | ||
} | ||
// checksum | ||
var sum = 7 * getLetterValue(upperNum[0]) + | ||
3 * getLetterValue(upperNum[1]) + | ||
1 * getLetterValue(upperNum[2]) + | ||
7 * getLetterValue(upperNum[4]) + | ||
3 * getLetterValue(upperNum[5]) + | ||
1 * getLetterValue(upperNum[6]) + | ||
7 * getLetterValue(upperNum[7]) + | ||
3 * getLetterValue(upperNum[8]); | ||
sum %= 10; | ||
if (sum !== getLetterValue(upperNum[3])) { | ||
return false; | ||
} | ||
return true; | ||
}, | ||
/** | ||
* Checks if given number of identity card is valid. | ||
* @param {string} num - series and number of identity card to validate | ||
* @return {boolean} - true if identity card is valid, false if invalid | ||
*/ | ||
identityCardWithSeparator: function (num) { | ||
// check length | ||
if (!num || num.length !== 10) { | ||
return false; | ||
} | ||
// check separator | ||
if (num[3] !== ' ' && num[3] !== '-') { | ||
return false; | ||
} | ||
return this.identityCard(num.replace(/[\s-]/g, '')); | ||
} | ||
}; | ||
exports["default"] = exports.validatePolish; | ||
}; | ||
exports["default"] = exports.validatePolish; | ||
}); |
{ | ||
"name": "validate-polish", | ||
"version": "2.0.0", | ||
"description": "Utility library for validation of PESEL, NIP, REGON, identity card etc. Aimed at mostly at Polish enviroment. [Polish] Walidacja numerów pesel, nip, regon, dowodu osobistego.", | ||
"version": "2.0.1", | ||
"description": "Utility library for validation of PESEL, NIP, REGON, identity card etc. Aimed mostly at Polish enviroment. [Polish] Walidacja numerów pesel, nip, regon, dowodu osobistego.", | ||
"keywords": [ | ||
@@ -6,0 +6,0 @@ "pesel", |
<p align="center"> | ||
<h1>validate-polish</h1> | ||
<div>Utility library for validation of PESEL, NIP, REGON, identity card etc. Aimed at mostly at Polish enviroment. [Polish] Walidacja numerów pesel, nip, regon, dowodu osobistego.</div> | ||
<div>Utility library for validation of PESEL, NIP, REGON, identity card etc. Aimed mostly at Polish enviroment. [Polish] Walidacja numerów pesel, nip, regon, dowodu osobistego.</div> | ||
</p> | ||
@@ -43,3 +43,4 @@ | ||
## Features | ||
- **Fast, lightweight and 0 dependencies** - no dependencies ensure there are no security breaches from other packages. | ||
- **Cross-platform** - works in Node.js and browser, available via cdn (https://cdn.jsdelivr.net/npm/validate-polish@latest/dist/index.js) | ||
- **Fast, lightweight, 0 dependencies** - no dependencies ensure there are no security breaches from other packages. | ||
- **Maintained** - if you need additional functionality feel free to create PR or an Issue. | ||
@@ -46,0 +47,0 @@ - **Strongly typed** - library written fully in TypeScript. |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
9576
223
50
0