Comparing version 2.0.0-beta.1 to 2.0.0-beta.2
@@ -9,3 +9,4 @@ declare module "kennitala" { | ||
export function generatePerson(date: Date): string; | ||
export function generatePerson(date: Date): string | ||
export function generatePerson(date: Date, startingIncrement: number): string | undefined; | ||
export function generateCompany(date: Date): string; | ||
@@ -12,0 +13,0 @@ |
@@ -1,2 +0,2 @@ | ||
!function(){function r(r,n){var t=s(r);if(10!==t.length)return!1;if(n&&!n(t))return!1;var e=t.split("").reduce(function(r,n,t){return r+n*f[t]},0),a=11-e%11,i=parseInt(t.substring(8,9),10);return 11==a&&0===i||a===i}function n(r){var n=s(r),t=parseInt(n.substring(0,2),10),e=parseInt(n.substring(2,4),10),a=parseInt(n.substring(4,6),10),i=parseInt(n.substring(9,10),10),u="";if(isNaN(t)||isNaN(e)||isNaN(a)||isNaN(i))return!1;t>40&&t<=71&&(t-=40),0===i?u="20":9===i?u="19":8===i&&(u="18");var o=u;return o+=a<10?"0"+a:""+a,o+="-",o+=e<10?"0"+e:""+e,o+="-",o+=t<10?"0"+t:""+t,!isNaN(new Date(o).getTime())}function t(r){var n=s(r),t=parseInt(n.substring(0,2),10),e=parseInt(n.substring(6,8),10);return t>0&&t<=31&&e>=20&&e<=99}function e(r){var n=s(r),t=parseInt(n.substring(0,2),10);return t>40&&t<=71}function a(r,n){function e(r){var n=""+Math.floor(10*Math.random()),a=""+Math.floor(10*Math.random());t(r)&&(n=""+Math.floor(8*Math.random()+2));for(var i=r+n+a,u=0,s=0;s<8;s++)u+=i[s]*f[s];return u=11-u%11,u=11==u?0:u,10==u?e(r):n+a+u}var a="",i=r.getDate();i<10&&(i="0"+i),i=""+i,i=n(i),a+=i;var u=r.getMonth();u+=1,u<10&&(u="0"+u),u=""+u,a+=u;var s=r.getFullYear();return s=""+s,s=s[2]+s[3],a+=s,a+=e(a),s=r.getFullYear(),s=""+s,a+=s[1]}function i(r){return r}function u(r){return""+(parseInt(r,10)+40)}function s(r){var n=""+r;return n=n.replace(/(\D)+/g,"")}var o={};o.isValid=function(a){var i=s(a);if(10===i.length&&("8"===i.substring(0,1)||"9"===i.substring(0,1)))return!0;var u=r(a,t),o=r(a,e);return n(a)&&(u||o)},o.isPerson=function(e){return n(e)&&r(e,t)},o.isCompany=function(t){return n(t)&&r(t,e)},o.isTemporary=function(r){var n=s(r);return 10===n.length&&("8"===n.substring(0,1)||"9"===n.substring(0,1))},o.sanitize=function(r){return s(r)},o.format=function(r,n){var t=s(r);return n=void 0!==n?n:"-",t=t.substring(0,6)+n+t.substring(6,10)},o.generatePerson=function(r){return a(r,i)},o.generateCompany=function(r){return a(r,u)},o.info=function(n){var a={};a.kt=s(n);var i=r(n,t),u=r(n,e);if(i||u){a.valid=!0,a.type=!0===i?"person":"company";var o=s(n),f=o.substring(0,2);f>31&&(f-=40);var g=o.substring(2,4),v=(0==o.substring(9,10)?20:19)+o.substring(4,6),l=new Date(v,g-1,f);a.birthday=l,a.birthdayReadable=l.toDateString();var c=new Date,d=c.getFullYear(),b=new Date(l);b.setFullYear(d);var p=d-l.getFullYear();return b>c&&p--,p<0?p+=100:p=p<1?(c.getTime()-l.getTime())/1e3/60/60/24/365.2422:p,a.age=p,a}return a.valid=!1,a};var f=[3,2,7,6,5,4,3,2,0,0];"undefined"!=typeof module&&module.exports?module.exports=o:"function"==typeof define&&define.amd?define(o):window&&(window.kennitala=o)}(); | ||
!function(){function r(r,n){var t=s(r);if(10!==t.length)return!1;if(n&&!n(t))return!1;var e=t.split("").reduce(function(r,n,t){return r+n*f[t]},0),a=11-e%11,i=parseInt(t.substring(8,9),10);return 11==a&&0===i||a===i}function n(r){var n=s(r),t=parseInt(n.substring(0,2),10),e=parseInt(n.substring(2,4),10),a=parseInt(n.substring(4,6),10),i=parseInt(n.substring(9,10),10),u="";if(isNaN(t)||isNaN(e)||isNaN(a)||isNaN(i))return!1;t>40&&t<=71&&(t-=40),0===i?u="20":9===i?u="19":8===i&&(u="18");var o=u;return o+=a<10?"0"+a:""+a,o+="-",o+=e<10?"0"+e:""+e,o+="-",o+=t<10?"0"+t:""+t,!isNaN(new Date(o).getTime())}function t(r){var n=s(r),t=parseInt(n.substring(0,2),10);return t>0&&t<=31}function e(r){var n=s(r),t=parseInt(n.substring(0,2),10);return t>40&&t<=71}function a(r,n,e){function a(r){var n=""+Math.floor(10*Math.random()),e=""+Math.floor(10*Math.random());t(r)&&(n=""+Math.floor(8*Math.random()+2));for(var i=r+n+e,u=0,s=0;s<8;s++)u+=i[s]*f[s];return u=11-u%11,u=11==u?0:u,10==u?a(r):n+e+u}function i(r,n){var t=parseInt(n,10);if(!(t>=100||isNaN(t))){for(var e=t.toString().split(""),a=e[0],u=e[1],s=r+a+u,o=0,g=0;g<8;g++)o+=s[g]*f[g];return o=11-o%11,o=11==o?0:o,10==o?i(r,t+1):a+u+o}}var u="",s=r.getDate();s<10&&(s="0"+s),s=""+s,s=n(s),u+=s;var o=r.getMonth();o+=1,o<10&&(o="0"+o),o=""+o,u+=o;var g=r.getFullYear();if(g=""+g,g=g[2]+g[3],u+=g,"number"==typeof e){var v=i(u,e);if(void 0===v)return;u+=v}else u+=a(u);return g=r.getFullYear(),g=""+g,u+=g[1]}function i(r){return r}function u(r){return""+(parseInt(r,10)+40)}function s(r){var n=""+r;return n=n.replace(/(\D)+/g,"")}var o={};o.isValid=function(a){var i=s(a);if(10===i.length&&("8"===i.substring(0,1)||"9"===i.substring(0,1)))return!0;var u=r(a,t),o=r(a,e);return n(a)&&(u||o)},o.isPerson=function(e){return n(e)&&r(e,t)},o.isCompany=function(t){return n(t)&&r(t,e)},o.isTemporary=function(r){var n=s(r);return 10===n.length&&("8"===n.substring(0,1)||"9"===n.substring(0,1))},o.sanitize=function(r){return s(r)},o.format=function(r,n){var t=s(r);return n=void 0!==n?n:"-",t.substring(0,6)+n+t.substring(6,10)},o.generatePerson=function(r,n){return a(r,i,n)},o.generateCompany=function(r){return a(r,u)},o.info=function(n){var a={};a.kt=s(n);var i=r(n,t),u=r(n,e);if(i||u){a.valid=!0,a.type=!0===i?"person":"company";var o=s(n),f=o.substring(0,2);f>31&&(f-=40);var g=o.substring(2,4),v=(0==o.substring(9,10)?20:19)+o.substring(4,6),l=new Date(v,g-1,f);a.birthday=l,a.birthdayReadable=l.toDateString();var c=new Date,d=c.getFullYear(),p=new Date(l);p.setFullYear(d);var b=d-l.getFullYear();return p>c&&b--,b<0?b+=100:b=b<1?(c.getTime()-l.getTime())/1e3/60/60/24/365.2422:b,a.age=b,a}return a.valid=!1,a};var f=[3,2,7,6,5,4,3,2,0,0];"undefined"!=typeof module&&module.exports?module.exports=o:"function"==typeof define&&define.amd?define(o):window&&(window.kennitala=o)}(); | ||
//# sourceMappingURL=kennitala.min.js.map |
103
kennitala.js
@@ -10,3 +10,4 @@ (function(){ | ||
// Adds support for temporary KT | ||
var kt = formatKennitala(kennitala); | ||
var kt = sanitizeInput(kennitala); | ||
if (kt.length === 10 && (kt.substring(0, 1) === '8' || kt.substring(0, 1) === '9')) { | ||
@@ -36,25 +37,20 @@ return true; | ||
kennitala.isTemporary = function(kennitala) { | ||
var kt = formatKennitala(kennitala) | ||
if (kt.length === 10 && (kt.substring(0, 1) === '8' || kt.substring(0, 1) === '9')) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
var kt = sanitizeInput(kennitala) | ||
return (kt.length === 10 && (kt.substring(0, 1) === '8' || kt.substring(0, 1) === '9')); | ||
} | ||
kennitala.sanitize = function (kennitala) { | ||
return formatKennitala(kennitala); | ||
return sanitizeInput(kennitala); | ||
}; | ||
kennitala.format = function (kennitala, spacer) { | ||
var kt = formatKennitala(kennitala); | ||
spacer = typeof spacer !== 'undefined' ? spacer : '-'; | ||
var kt = sanitizeInput(kennitala); | ||
spacer = spacer !== undefined ? spacer : '-'; | ||
kt = kt.substring(0,6) + spacer + kt.substring(6, 10); | ||
return kt; | ||
return kt.substring(0,6) + spacer + kt.substring(6, 10); | ||
}; | ||
kennitala.generatePerson = function(date) { | ||
return generateKennitala(date, personDayDelta); | ||
kennitala.generatePerson = function(date, startingIncrement) { | ||
return generateKennitala(date, personDayDelta, startingIncrement); | ||
}; | ||
@@ -80,3 +76,3 @@ | ||
info.kt = formatKennitala(kt); | ||
info.kt = sanitizeInput(kt); | ||
@@ -92,3 +88,3 @@ var isPersonType = evaluate(kt, isPerson); | ||
// Get birthday Date object | ||
var kennitala = formatKennitala(kt); | ||
var kennitala = sanitizeInput(kt); | ||
var day = kennitala.substring(0, 2); | ||
@@ -155,3 +151,3 @@ | ||
function evaluate(input, entityEvaluationFn) { | ||
var kt = formatKennitala(input); | ||
var kt = sanitizeInput(input); | ||
if (kt.length !== 10) { | ||
@@ -176,3 +172,3 @@ return false; | ||
function isValidDate(kennitala) { | ||
var kt = formatKennitala(kennitala) | ||
var kt = sanitizeInput(kennitala) | ||
@@ -214,9 +210,6 @@ var d = parseInt(kt.substring(0, 2), 10); | ||
function isPerson(kennitala) { | ||
var kt = formatKennitala(kennitala) | ||
var kt = sanitizeInput(kennitala) | ||
var d = parseInt(kt.substring(0, 2), 10); | ||
// 7th and 8th characters for persons should be between incrementing from 20-99 | ||
var inc = parseInt(kt.substring(6, 8), 10); | ||
return d > 0 && d <= 31 && inc >= 20 && inc <= 99; | ||
return d > 0 && d <= 31; | ||
} | ||
@@ -226,3 +219,3 @@ | ||
function isCompany(kennitala) { | ||
var kt = formatKennitala(kennitala) | ||
var kt = sanitizeInput(kennitala) | ||
var d = parseInt(kt.substring(0, 2), 10); | ||
@@ -234,3 +227,3 @@ | ||
// Generate kennitala, takes in person/company entity function as well | ||
function generateKennitala(date, entityFn) { | ||
function generateKennitala(date, entityFn, startingIncrement) { | ||
var kt = ''; | ||
@@ -305,5 +298,57 @@ | ||
// 7-9th characters | ||
kt += randomAndChecksum(kt); | ||
/* | ||
Recursive function that generates two digits from the optional starting | ||
increment parameter then generates 9th character from 1-8th characters | ||
Checks if 9th character is 10 in which case the entire proccess is repeated | ||
*/ | ||
function incrementingChecksum(kt, incrementFrom) { | ||
/* | ||
7th and 8th characters are seemingly random for companies | ||
but are incrementing from 20-99 for individuals | ||
*/ | ||
var inc = parseInt(incrementFrom, 10) | ||
if (inc >= 100 || isNaN(inc)) { | ||
return undefined; | ||
} | ||
var digits = inc.toString().split(''); | ||
var digit7 = digits[0]; | ||
var digit8 = digits[1]; | ||
var tempKt = kt + digit7 + digit8; | ||
// Ninth number | ||
var sum = 0; | ||
for (var i = 0; i < 8; i++) { | ||
sum += tempKt[i] * MAGIC_NUMBERS[i]; | ||
} | ||
sum = 11 - (sum % 11); | ||
sum = (sum == 11) ? 0 : sum; | ||
if (sum == 10) { | ||
return incrementingChecksum(kt, inc+1); | ||
} | ||
else{ | ||
return digit7 + digit8 + sum; | ||
} | ||
} | ||
if (typeof startingIncrement == 'number') { | ||
var digits789 = incrementingChecksum(kt, startingIncrement) | ||
// If no solution is found return undefined | ||
if (digits789 === undefined) { | ||
return undefined; | ||
} | ||
kt += digits789 | ||
} else { | ||
// 7-9th characters | ||
kt += randomAndChecksum(kt); | ||
} | ||
// 10th character is century | ||
@@ -328,3 +373,3 @@ year = date.getFullYear(); | ||
// Ensures datatype is string, then removes all non-digit characters from kennitala | ||
function formatKennitala(p_kennitala) { | ||
function sanitizeInput(p_kennitala) { | ||
var kennitala = "" + p_kennitala; | ||
@@ -331,0 +376,0 @@ |
{ | ||
"name": "kennitala", | ||
"version": "2.0.0-beta.1", | ||
"version": "2.0.0-beta.2", | ||
"description": "Icelandic social security number (kennitölur) utilities for servers and clients", | ||
@@ -5,0 +5,0 @@ "main": "dist/kennitala.min.js", |
@@ -50,2 +50,8 @@ var kennitala = require('../kennitala.js'); | ||
describe("#generateKennitala", function () { | ||
it("should generate my id", function () { | ||
kennitala.generatePerson(new Date("1996-08-31"), 20).should.equal("3108962099") | ||
}) | ||
}); | ||
describe("#sanitize inputs", function () { | ||
@@ -52,0 +58,0 @@ it("should remove invalid characters in kennitölur", function () { // ég bara gat ekki skrifað kennitala-s einu sinni í viðbót >_< |
Sorry, the diff of this file is not supported yet
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
41783
415