validate-polish
Advanced tools
Comparing version 2.0.4 to 2.0.5
@@ -1,2 +0,2 @@ | ||
declare const validatePolish: { | ||
export declare const validatePolish: { | ||
checksum: (number: string, weights: number[]) => boolean; | ||
@@ -34,3 +34,2 @@ /** | ||
}; | ||
export { validatePolish }; | ||
export default validatePolish; |
@@ -1,1 +0,176 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var r={checksum:function(r,e){for(var t=r.length-1,n=0,i=0;i<t;i++){n+=parseInt(r[i],10)*e[i]}return(n%11!=10?n%11:0)===parseInt(r.slice(-1),10)},pesel:function(r){if(!/^[0-9]{11}$/.test(r))return!1;var e=[1,3,7,9],t=(""+r).split("").map((function(r){return parseInt(r,10)})),n=t.splice(-1)[0],i=t.reduce((function(r,t,n){return r+t*e[n%4]}))%10;return 10-(0===i?10:i)===n},nip:function(r){if("string"!=typeof r)return!1;var e=r.replace(/-/g,"");if(!1===/^[0-9]{10}$/.test(e))return!1;var t=(""+e).split(""),n=(6*parseInt(t[0],10)+5*parseInt(t[1],10)+7*parseInt(t[2],10)+2*parseInt(t[3],10)+3*parseInt(t[4],10)+4*parseInt(t[5],10)+5*parseInt(t[6],10)+6*parseInt(t[7],10)+7*parseInt(t[8],10))%11;return parseInt(t[9],10)===n},regon:function(e){if(!1===/^[0-9]{9,14}$/.test(e))return!1;var t=[8,9,2,3,4,5,6,7];if(9===e.length)return r.checksum(e,t);return r.checksum(e.slice(0,9),t)&&r.checksum(e,[2,4,8,5,0,9,7,3,6,1,2,4,8])},identityCard:function(r){if(!r||9!==r.length)return!1;for(var e=r.toUpperCase(),t=["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"],n=function(r){for(var e=0,n=t.length;e<n;e++)if(r===t[e])return e;return-1},i=0;i<3;i++)if(n(e[i])<10)return!1;for(i=3;i<9;i++)if(n(e[i])<0||n(e[i])>9)return!1;var s=7*n(e[0])+3*n(e[1])+1*n(e[2])+7*n(e[4])+3*n(e[5])+1*n(e[6])+7*n(e[7])+3*n(e[8]);return(s%=10)===n(e[3])},identityCardWithSeparator:function(r){return!(!r||10!==r.length)&&((" "===r[3]||"-"===r[3])&&this.identityCard(r.replace(/[\s-]/g,"")))}};window&&(window.validatePolish=r),exports.default=r,exports.validatePolish=r; | ||
export var 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; | ||
}, | ||
/** | ||
* 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 validatePolish.checksum(regon, weights9); | ||
} | ||
var weights14 = [2, 4, 8, 5, 0, 9, 7, 3, 6, 1, 2, 4, 8]; | ||
return validatePolish.checksum(regon.slice(0, 9), weights9) && 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; | ||
} | ||
} | ||
// 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; | ||
} | ||
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, '')); | ||
} | ||
}; | ||
export default validatePolish; |
{ | ||
"name": "validate-polish", | ||
"version": "2.0.4", | ||
"version": "2.0.5", | ||
"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.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
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
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
8578
210
4