personnummer
Advanced tools
Comparing version 3.1.5 to 3.2.0
@@ -71,6 +71,6 @@ "use strict"; | ||
* | ||
* @param {string} ssn | ||
* @param {string} pin | ||
* @param {object} options | ||
*/ | ||
constructor(ssn, options) { | ||
constructor(pin, options) { | ||
/** | ||
@@ -124,3 +124,7 @@ * Personnummer century. | ||
this._check = ""; | ||
this.parse(ssn, options); | ||
this.parse(pin, { | ||
allowCoordinationNumber: true, | ||
allowInterimNumber: false, | ||
...options | ||
}); | ||
} | ||
@@ -194,3 +198,3 @@ /** | ||
* | ||
* @param {string} ssn | ||
* @param {string} pin | ||
* @param {object} options | ||
@@ -200,4 +204,4 @@ * | ||
*/ | ||
static parse(ssn, options) { | ||
return new Personnummer(ssn, options); | ||
static parse(pin, options) { | ||
return new Personnummer(pin, options); | ||
} | ||
@@ -212,5 +216,5 @@ /** | ||
*/ | ||
static valid(ssn, options) { | ||
static valid(pin, options) { | ||
try { | ||
Personnummer.parse(ssn, options); | ||
Personnummer.parse(pin, options); | ||
return true; | ||
@@ -224,9 +228,11 @@ } catch (e) { | ||
* | ||
* @param {string} ssn | ||
* @param {string} pin | ||
* @param {object} options | ||
*/ | ||
// eslint-disable-next-line | ||
parse(ssn, options) { | ||
const reg = /^(\d{2}){0,1}(\d{2})(\d{2})(\d{2})([+-]?)((?!000)\d{3})(\d)$/; | ||
const match = reg.exec(ssn); | ||
parse(pin, options) { | ||
if (pin.length < 10 || pin.length > 13) { | ||
throw new PersonnummerError(); | ||
} | ||
const reg = /^(\d{2}){0,1}(\d{2})(\d{2})(\d{2})([+-]?)((?!000)\d{3}|[TRSUWXJKLMN]\d{2})(\d)$/; | ||
const match = reg.exec(pin); | ||
if (!match) { | ||
@@ -243,3 +249,3 @@ throw new PersonnummerError(); | ||
if (typeof century === "undefined" || !century.length) { | ||
const d = new Date(); | ||
const d = /* @__PURE__ */ new Date(); | ||
let baseYear = 0; | ||
@@ -256,3 +262,3 @@ if (sep === "+") { | ||
this._century = century; | ||
if (new Date().getFullYear() - parseInt(century + year, 10) < 100) { | ||
if ((/* @__PURE__ */ new Date()).getFullYear() - parseInt(century + year, 10) < 100) { | ||
this._sep = "-"; | ||
@@ -272,2 +278,8 @@ } else { | ||
} | ||
if (!(options == null ? void 0 : options.allowCoordinationNumber) && this.isCoordinationNumber()) { | ||
throw new PersonnummerError(); | ||
} | ||
if (!(options == null ? void 0 : options.allowInterimNumber) && this.isInterimNumber()) { | ||
throw new PersonnummerError(); | ||
} | ||
} | ||
@@ -280,3 +292,5 @@ /** | ||
valid() { | ||
const valid = luhn(this.year + this.month + this.day + this.num) === +this.check && !!this.check; | ||
const valid = luhn( | ||
this.year + this.month + this.day + this.num.replace(/[TRSUWXJKLMN]/, "1") | ||
) === +this.check && !!this.check; | ||
if (valid && testDate(parseInt(this.century + this.year), +this.month, +this.day)) { | ||
@@ -309,2 +323,11 @@ return valid; | ||
getAge() { | ||
const date = this.getDate(); | ||
return diffInYears(new Date(Date.now()), date); | ||
} | ||
/** | ||
* Get date from a Swedish personal identity number. | ||
* | ||
* @return {Date} | ||
*/ | ||
getDate() { | ||
let ageDay = +this.day; | ||
@@ -315,5 +338,13 @@ if (this.isCoordinationNumber()) { | ||
const ageDate = this.century + this.year + "-" + this.month + "-" + (ageDay < 10 ? "0" + ageDay : ageDay); | ||
return diffInYears(new Date(Date.now()), new Date(ageDate)); | ||
return new Date(ageDate); | ||
} | ||
/** | ||
* Check if a Swedish personal identity number is a interim number or not. | ||
* | ||
* @return {boolean} | ||
*/ | ||
isInterimNumber() { | ||
return /[TRSUWXJKLMN]/.test(this.num[0]); | ||
} | ||
/** | ||
* Check if a Swedish personal identity number is a coordination number or not. | ||
@@ -320,0 +351,0 @@ * |
@@ -45,6 +45,6 @@ // src/errors.ts | ||
* | ||
* @param {string} ssn | ||
* @param {string} pin | ||
* @param {object} options | ||
*/ | ||
constructor(ssn, options) { | ||
constructor(pin, options) { | ||
/** | ||
@@ -98,3 +98,7 @@ * Personnummer century. | ||
this._check = ""; | ||
this.parse(ssn, options); | ||
this.parse(pin, { | ||
allowCoordinationNumber: true, | ||
allowInterimNumber: false, | ||
...options | ||
}); | ||
} | ||
@@ -168,3 +172,3 @@ /** | ||
* | ||
* @param {string} ssn | ||
* @param {string} pin | ||
* @param {object} options | ||
@@ -174,4 +178,4 @@ * | ||
*/ | ||
static parse(ssn, options) { | ||
return new Personnummer(ssn, options); | ||
static parse(pin, options) { | ||
return new Personnummer(pin, options); | ||
} | ||
@@ -186,5 +190,5 @@ /** | ||
*/ | ||
static valid(ssn, options) { | ||
static valid(pin, options) { | ||
try { | ||
Personnummer.parse(ssn, options); | ||
Personnummer.parse(pin, options); | ||
return true; | ||
@@ -198,9 +202,11 @@ } catch (e) { | ||
* | ||
* @param {string} ssn | ||
* @param {string} pin | ||
* @param {object} options | ||
*/ | ||
// eslint-disable-next-line | ||
parse(ssn, options) { | ||
const reg = /^(\d{2}){0,1}(\d{2})(\d{2})(\d{2})([+-]?)((?!000)\d{3})(\d)$/; | ||
const match = reg.exec(ssn); | ||
parse(pin, options) { | ||
if (pin.length < 10 || pin.length > 13) { | ||
throw new PersonnummerError(); | ||
} | ||
const reg = /^(\d{2}){0,1}(\d{2})(\d{2})(\d{2})([+-]?)((?!000)\d{3}|[TRSUWXJKLMN]\d{2})(\d)$/; | ||
const match = reg.exec(pin); | ||
if (!match) { | ||
@@ -217,3 +223,3 @@ throw new PersonnummerError(); | ||
if (typeof century === "undefined" || !century.length) { | ||
const d = new Date(); | ||
const d = /* @__PURE__ */ new Date(); | ||
let baseYear = 0; | ||
@@ -230,3 +236,3 @@ if (sep === "+") { | ||
this._century = century; | ||
if (new Date().getFullYear() - parseInt(century + year, 10) < 100) { | ||
if ((/* @__PURE__ */ new Date()).getFullYear() - parseInt(century + year, 10) < 100) { | ||
this._sep = "-"; | ||
@@ -246,2 +252,8 @@ } else { | ||
} | ||
if (!(options == null ? void 0 : options.allowCoordinationNumber) && this.isCoordinationNumber()) { | ||
throw new PersonnummerError(); | ||
} | ||
if (!(options == null ? void 0 : options.allowInterimNumber) && this.isInterimNumber()) { | ||
throw new PersonnummerError(); | ||
} | ||
} | ||
@@ -254,3 +266,5 @@ /** | ||
valid() { | ||
const valid = luhn(this.year + this.month + this.day + this.num) === +this.check && !!this.check; | ||
const valid = luhn( | ||
this.year + this.month + this.day + this.num.replace(/[TRSUWXJKLMN]/, "1") | ||
) === +this.check && !!this.check; | ||
if (valid && testDate(parseInt(this.century + this.year), +this.month, +this.day)) { | ||
@@ -283,2 +297,11 @@ return valid; | ||
getAge() { | ||
const date = this.getDate(); | ||
return diffInYears(new Date(Date.now()), date); | ||
} | ||
/** | ||
* Get date from a Swedish personal identity number. | ||
* | ||
* @return {Date} | ||
*/ | ||
getDate() { | ||
let ageDay = +this.day; | ||
@@ -289,5 +312,13 @@ if (this.isCoordinationNumber()) { | ||
const ageDate = this.century + this.year + "-" + this.month + "-" + (ageDay < 10 ? "0" + ageDay : ageDay); | ||
return diffInYears(new Date(Date.now()), new Date(ageDate)); | ||
return new Date(ageDate); | ||
} | ||
/** | ||
* Check if a Swedish personal identity number is a interim number or not. | ||
* | ||
* @return {boolean} | ||
*/ | ||
isInterimNumber() { | ||
return /[TRSUWXJKLMN]/.test(this.num[0]); | ||
} | ||
/** | ||
* Check if a Swedish personal identity number is a coordination number or not. | ||
@@ -294,0 +325,0 @@ * |
type OptionsType = { | ||
[key: string]: boolean | number | string; | ||
allowCoordinationNumber: boolean; | ||
allowInterimNumber: boolean; | ||
}; | ||
@@ -104,10 +105,10 @@ declare class Personnummer { | ||
* | ||
* @param {string} ssn | ||
* @param {string} pin | ||
* @param {object} options | ||
*/ | ||
constructor(ssn: string, options?: OptionsType); | ||
constructor(pin: string, options?: OptionsType); | ||
/** | ||
* Parse personnummer. | ||
* | ||
* @param {string} ssn | ||
* @param {string} pin | ||
* @param {object} options | ||
@@ -117,3 +118,3 @@ * | ||
*/ | ||
static parse(ssn: string, options?: OptionsType): Personnummer; | ||
static parse(pin: string, options?: OptionsType): Personnummer; | ||
/** | ||
@@ -127,7 +128,7 @@ * Validate a Swedish personal identity number. | ||
*/ | ||
static valid(ssn: string, options?: OptionsType): boolean; | ||
static valid(pin: string, options?: OptionsType): boolean; | ||
/** | ||
* Parse personnummer and set class properties. | ||
* | ||
* @param {string} ssn | ||
* @param {string} pin | ||
* @param {object} options | ||
@@ -160,2 +161,14 @@ */ | ||
/** | ||
* Get date from a Swedish personal identity number. | ||
* | ||
* @return {Date} | ||
*/ | ||
getDate(): Date; | ||
/** | ||
* Check if a Swedish personal identity number is a interim number or not. | ||
* | ||
* @return {boolean} | ||
*/ | ||
isInterimNumber(): boolean; | ||
/** | ||
* Check if a Swedish personal identity number is a coordination number or not. | ||
@@ -162,0 +175,0 @@ * |
{ | ||
"name": "personnummer", | ||
"description": "Validate Swedish personal identity numbers", | ||
"version": "3.1.5", | ||
"version": "3.2.0", | ||
"license": "MIT", | ||
@@ -30,15 +30,14 @@ "homepage": "https://github.com/personnummer/js", | ||
"devDependencies": { | ||
"@pinefile/pine": "^2.0.2", | ||
"@types/jest": "^29.2.6", | ||
"@typescript-eslint/eslint-plugin": "^5.48.2", | ||
"@typescript-eslint/parser": "^5.48.2", | ||
"esbuild": "^0.17.3", | ||
"esbuild-jest": "^0.5.0", | ||
"eslint": "^8.32.0", | ||
"@frozzare/pkg": "^1.0.7", | ||
"@pinefile/pine": "^2.0.3", | ||
"@types/jest": "^29.4.0", | ||
"@typescript-eslint/eslint-plugin": "^5.54.1", | ||
"@typescript-eslint/parser": "^5.54.1", | ||
"eslint": "^8.35.0", | ||
"is-ci": "^3.0.1", | ||
"jest": "^29.3.1", | ||
"prettier": "^2.8.3", | ||
"rimraf": "^4.1.1", | ||
"typescript": "^4.9.4", | ||
"undici": "^5.15.1" | ||
"prettier": "^2.8.4", | ||
"rimraf": "^4.4.0", | ||
"typescript": "^4.9.5", | ||
"undici": "^5.20.0", | ||
"vitest": "^0.29.2" | ||
}, | ||
@@ -45,0 +44,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
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
23767
12
922