Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

personnummer

Package Overview
Dependencies
Maintainers
2
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

personnummer - npm Package Compare versions

Comparing version 3.1.3 to 3.1.4

727

dist/cjs/index.js

@@ -1,616 +0,175 @@

'use strict';
var __defProp = Object.defineProperty;
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
// src/index.ts
__markAsModule(exports);
__export(exports, {
default: () => src_default
});
function _defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
// src/errors.ts
var PersonnummerError = class extends Error {
constructor() {
super("Invalid swedish personal identity number");
}
}
};
function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function _inherits(subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function");
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
writable: true,
configurable: true
}
});
if (superClass) _setPrototypeOf(subClass, superClass);
}
function _getPrototypeOf(o) {
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
return o.__proto__ || Object.getPrototypeOf(o);
};
return _getPrototypeOf(o);
}
function _setPrototypeOf(o, p) {
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
o.__proto__ = p;
return o;
};
return _setPrototypeOf(o, p);
}
function _isNativeReflectConstruct() {
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
if (Reflect.construct.sham) return false;
if (typeof Proxy === "function") return true;
try {
Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));
return true;
} catch (e) {
return false;
}
}
function _construct(Parent, args, Class) {
if (_isNativeReflectConstruct()) {
_construct = Reflect.construct;
} else {
_construct = function _construct(Parent, args, Class) {
var a = [null];
a.push.apply(a, args);
var Constructor = Function.bind.apply(Parent, a);
var instance = new Constructor();
if (Class) _setPrototypeOf(instance, Class.prototype);
return instance;
};
}
return _construct.apply(null, arguments);
}
function _isNativeFunction(fn) {
return Function.toString.call(fn).indexOf("[native code]") !== -1;
}
function _wrapNativeSuper(Class) {
var _cache = typeof Map === "function" ? new Map() : undefined;
_wrapNativeSuper = function _wrapNativeSuper(Class) {
if (Class === null || !_isNativeFunction(Class)) return Class;
if (typeof Class !== "function") {
throw new TypeError("Super expression must either be null or a function");
}
if (typeof _cache !== "undefined") {
if (_cache.has(Class)) return _cache.get(Class);
_cache.set(Class, Wrapper);
}
function Wrapper() {
return _construct(Class, arguments, _getPrototypeOf(this).constructor);
}
Wrapper.prototype = Object.create(Class.prototype, {
constructor: {
value: Wrapper,
enumerable: false,
writable: true,
configurable: true
}
});
return _setPrototypeOf(Wrapper, Class);
};
return _wrapNativeSuper(Class);
}
function _assertThisInitialized(self) {
if (self === void 0) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return self;
}
function _possibleConstructorReturn(self, call) {
if (call && (typeof call === "object" || typeof call === "function")) {
return call;
}
return _assertThisInitialized(self);
}
function _createSuper(Derived) {
var hasNativeReflectConstruct = _isNativeReflectConstruct();
return function _createSuperInternal() {
var Super = _getPrototypeOf(Derived),
result;
if (hasNativeReflectConstruct) {
var NewTarget = _getPrototypeOf(this).constructor;
result = Reflect.construct(Super, arguments, NewTarget);
} else {
result = Super.apply(this, arguments);
}
return _possibleConstructorReturn(this, result);
};
}
var PersonnummerError = /*#__PURE__*/function (_Error) {
_inherits(PersonnummerError, _Error);
var _super = _createSuper(PersonnummerError);
function PersonnummerError() {
_classCallCheck(this, PersonnummerError);
return _super.call(this, 'Invalid swedish personal identity number');
}
return PersonnummerError;
}( /*#__PURE__*/_wrapNativeSuper(Error));
/**
* Compare the two dates and return -1, 0 or 1.
*
* @param {Date} dateLeft
* @param {Date} dateRight
*
* @return {number}
*/
var compareAsc = function compareAsc(dateLeft, dateRight) {
var diff = dateLeft.getTime() - dateRight.getTime();
// src/utils.ts
var compareAsc = (dateLeft, dateRight) => {
const diff = dateLeft.getTime() - dateRight.getTime();
return diff < 0 ? -1 : diff > 0 ? 1 : diff;
};
/**
* Get the number of full years between the given dates.
*
* @param {Date} dateLeft
* @param {Date} dateRight
*
* @return {number}
*/
var diffInYears = function diffInYears(dateLeft, dateRight) {
var sign = compareAsc(dateLeft, dateRight);
var yearDiff = Math.abs(dateLeft.getFullYear() - dateRight.getFullYear());
var diffInYears = (dateLeft, dateRight) => {
const sign = compareAsc(dateLeft, dateRight);
const yearDiff = Math.abs(dateLeft.getFullYear() - dateRight.getFullYear());
dateLeft.setFullYear(dateLeft.getFullYear() - sign * yearDiff);
var isLastYearNotFull = compareAsc(dateLeft, dateRight) === -sign;
var result = sign * (yearDiff - +isLastYearNotFull);
const isLastYearNotFull = compareAsc(dateLeft, dateRight) === -sign;
const result = sign * (yearDiff - +isLastYearNotFull);
return result === 0 ? 0 : result;
};
/**
* Calculates the Luhn checksum of a string of digits.
*
* @param {string|number} str
*
* @return {number}
*/
var luhn = function luhn(str) {
var sum = 0;
str += '';
for (var i = 0, l = str.length; i < l; i++) {
var v = parseInt(str[i]);
var luhn = (str) => {
let sum = 0;
str += "";
for (let i = 0, l = str.length; i < l; i++) {
let v = parseInt(str[i]);
v *= 2 - i % 2;
if (v > 9) {
v -= 9;
}
sum += v;
}
return Math.ceil(sum / 10) * 10 - sum;
};
/**
* Test if the input parameters are a valid date or not.
*
* @param {int} year
* @param {int} month
* @param {int} day
*
* @return {boolean}
*/
var testDate = function testDate(year, month, day) {
var testDate = (year, month, day) => {
month -= 1;
var date = new Date(year, month, day);
const date = new Date(year, month, day);
return !(date.getFullYear() !== year || date.getMonth() !== month || date.getDate() !== day);
};
var Personnummer = /*#__PURE__*/function () {
_createClass(Personnummer, [{
key: "century",
/**
* Personnummer century.
*
* @var {string}
*/
/**
* Get century.
*
* @return {string}
*/
get: function get() {
return this._century;
}
/**
* Personnummer full year.
*
* @var {string}
*/
}, {
key: "fullYear",
/**
* Get age.
*
* @return {string}
*/
get: function get() {
return this._fullYear;
}
/**
* Personnummer year.
*
* @var {string}
*/
}, {
key: "year",
/**
* Get age.
*
* @return {string}
*/
get: function get() {
return this._year;
}
/**
* Personnummer month.
*
* @var {string}
*/
}, {
key: "month",
/**
* Get month.
*
* @return {string}
*/
get: function get() {
return this._month;
}
/**
* Personnummer day.
*
* @var {string}
*/
}, {
key: "day",
/**
* Get day.
*
* @return {string}
*/
get: function get() {
return this._day;
}
/**
* Personnummer seperator.
*
* @var {string}
*/
}, {
key: "sep",
/**
* Get sep.
*
* @return {string}
*/
get: function get() {
return this._sep;
}
/**
* Personnumer first three of the last four numbers.
*
* @var {string}
*/
}, {
key: "num",
/**
* Get num.
*
* @return {string}
*/
get: function get() {
return this._num;
}
/**
* The last number of the personnummer.
*
* @var {string}
*/
}, {
key: "check",
/**
* Get check.
*
* @return {string}
*/
get: function get() {
return this._check;
}
/**
* Personnummer constructor.
*
* @param {string} ssn
* @param {object} options
*/
}]);
function Personnummer(ssn, options) {
_classCallCheck(this, Personnummer);
_defineProperty(this, "_century", '');
_defineProperty(this, "_fullYear", '');
_defineProperty(this, "_year", '');
_defineProperty(this, "_month", '');
_defineProperty(this, "_day", '');
_defineProperty(this, "_sep", '');
_defineProperty(this, "_num", '');
_defineProperty(this, "_check", '');
// src/index.ts
var Personnummer = class {
constructor(ssn, options) {
this._century = "";
this._fullYear = "";
this._year = "";
this._month = "";
this._day = "";
this._sep = "";
this._num = "";
this._check = "";
this.parse(ssn, options);
}
/**
* Parse personnummer.
*
* @param {string} ssn
* @param {object} options
*
* @return {Personnummer}
*/
_createClass(Personnummer, [{
key: "parse",
/**
* Parse personnummer and set class properties.
*
* @param {string} ssn
* @param {object} options
*/
// eslint-disable-next-line
value: function parse(ssn, options) {
var reg = /^(\d{2}){0,1}(\d{2})(\d{2})(\d{2})([+\-\s]?)((?!000)\d{3})(\d)$/;
var match = reg.exec(ssn);
if (!match) {
throw new PersonnummerError();
}
var century = match[1];
var year = match[2];
var month = match[3];
var day = match[4];
var sep = match[5];
var num = match[6];
var check = match[7];
if (typeof century === 'undefined' || !century.length) {
var d = new Date();
var baseYear = 0;
if (sep === '+') {
this._sep = '+';
baseYear = d.getFullYear() - 100;
} else {
this._sep = '-';
baseYear = d.getFullYear();
}
this._century = ('' + (baseYear - (baseYear - parseInt(year)) % 100)).substr(0, 2);
} else {
this._century = century;
if (new Date().getFullYear() - parseInt(century + year, 10) < 100) {
this._sep = '-';
} else {
this._sep = '+';
}
}
this._year = year;
this._fullYear = this._century + year;
this._month = month;
this._day = day;
this._num = num;
this._check = check;
if (!this.valid()) {
throw new PersonnummerError();
}
get century() {
return this._century;
}
get fullYear() {
return this._fullYear;
}
get year() {
return this._year;
}
get month() {
return this._month;
}
get day() {
return this._day;
}
get sep() {
return this._sep;
}
get num() {
return this._num;
}
get check() {
return this._check;
}
static parse(ssn, options) {
return new Personnummer(ssn, options);
}
static valid(ssn, options) {
try {
Personnummer.parse(ssn, options);
return true;
} catch (e) {
return false;
}
/**
* Validate a Swedish personal identity number.
*
* @return {boolean}
*/
}, {
key: "valid",
value: function valid() {
var valid = luhn(this.year + this.month + this.day + this.num) === +this.check && !!this.check;
if (valid && testDate(parseInt(this.century + this.year), +this.month, +this.day)) {
return valid;
}
return valid && testDate(parseInt(this.century + this.year), +this.month, +this.day - 60);
}
parse(ssn, options) {
const reg = /^(\d{2}){0,1}(\d{2})(\d{2})(\d{2})([+\-]?)((?!000)\d{3})(\d)$/;
const match = reg.exec(ssn);
if (!match) {
throw new PersonnummerError();
}
/**
* Format a Swedish personal identity number as one of the official formats,
* A long format or a short format.
*
* If the input number could not be parsed a empty string will be returned.
*
* @param {boolean} longFormat
*
* @return {string}
*/
}, {
key: "format",
value: function format() {
var longFormat = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
if (longFormat) {
return "".concat(this.century).concat(this.year).concat(this.month).concat(this.day).concat(this.num).concat(this.check);
const century = match[1];
const year = match[2];
const month = match[3];
const day = match[4];
const sep = match[5];
const num = match[6];
const check = match[7];
if (typeof century === "undefined" || !century.length) {
const d = new Date();
let baseYear = 0;
if (sep === "+") {
this._sep = "+";
baseYear = d.getFullYear() - 100;
} else {
this._sep = "-";
baseYear = d.getFullYear();
}
return "".concat(this.year).concat(this.month).concat(this.day).concat(this.sep).concat(this.num).concat(this.check);
}
/**
* Get age from a Swedish personal identity number.
*
* @return {number}
*/
}, {
key: "getAge",
value: function getAge() {
var ageDay = +this.day;
if (this.isCoordinationNumber()) {
ageDay -= 60;
this._century = ("" + (baseYear - (baseYear - parseInt(year)) % 100)).substr(0, 2);
} else {
this._century = century;
if (new Date().getFullYear() - parseInt(century + year, 10) < 100) {
this._sep = "-";
} else {
this._sep = "+";
}
var ageDate = this.century + this.year + '-' + this.month + '-' + (ageDay < 10 ? '0' + ageDay : ageDay);
return diffInYears(new Date(Date.now()), new Date(ageDate));
}
/**
* Check if a Swedish personal identity number is a coordination number or not.
*
* @return {boolean}
*/
}, {
key: "isCoordinationNumber",
value: function isCoordinationNumber() {
return testDate(parseInt(this.century + this.year), +this.month, +this.day - 60);
this._year = year;
this._fullYear = this._century + year;
this._month = month;
this._day = day;
this._num = num;
this._check = check;
if (!this.valid()) {
throw new PersonnummerError();
}
/**
* Check if a Swedish personal identity number is for a female.
*
* @return {boolean}
*/
}, {
key: "isFemale",
value: function isFemale() {
return !this.isMale();
}
valid() {
const valid = luhn(this.year + this.month + this.day + this.num) === +this.check && !!this.check;
if (valid && testDate(parseInt(this.century + this.year), +this.month, +this.day)) {
return valid;
}
/**
* Check if a Swedish personal identity number is for a male.
*
* @return {boolean}
*/
}, {
key: "isMale",
value: function isMale() {
var sexDigit = parseInt(this.num.substr(-1));
return sexDigit % 2 === 1;
return valid && testDate(parseInt(this.century + this.year), +this.month, +this.day - 60);
}
format(longFormat = false) {
if (longFormat) {
return `${this.century}${this.year}${this.month}${this.day}${this.num}${this.check}`;
}
}], [{
key: "parse",
value: function parse(ssn, options) {
return new Personnummer(ssn, options);
return `${this.year}${this.month}${this.day}${this.sep}${this.num}${this.check}`;
}
getAge() {
let ageDay = +this.day;
if (this.isCoordinationNumber()) {
ageDay -= 60;
}
/**
* Validate a Swedish personal identity number.
*
* @param {string} str
* @param {object} options
*
* @return {boolean}
*/
}, {
key: "valid",
value: function valid(ssn, options) {
try {
Personnummer.parse(ssn, options);
return true;
} catch (e) {
return false;
}
}
}]);
return Personnummer;
}();
module.exports = Personnummer;
const ageDate = this.century + this.year + "-" + this.month + "-" + (ageDay < 10 ? "0" + ageDay : ageDay);
return diffInYears(new Date(Date.now()), new Date(ageDate));
}
isCoordinationNumber() {
return testDate(parseInt(this.century + this.year), +this.month, +this.day - 60);
}
isFemale() {
return !this.isMale();
}
isMale() {
const sexDigit = parseInt(this.num.substr(-1));
return sexDigit % 2 === 1;
}
};
var src_default = Personnummer;

@@ -1,614 +0,165 @@

function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
// src/errors.ts
var PersonnummerError = class extends Error {
constructor() {
super("Invalid swedish personal identity number");
}
}
};
function _defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function _inherits(subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function");
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
writable: true,
configurable: true
}
});
if (superClass) _setPrototypeOf(subClass, superClass);
}
function _getPrototypeOf(o) {
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
return o.__proto__ || Object.getPrototypeOf(o);
};
return _getPrototypeOf(o);
}
function _setPrototypeOf(o, p) {
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
o.__proto__ = p;
return o;
};
return _setPrototypeOf(o, p);
}
function _isNativeReflectConstruct() {
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
if (Reflect.construct.sham) return false;
if (typeof Proxy === "function") return true;
try {
Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));
return true;
} catch (e) {
return false;
}
}
function _construct(Parent, args, Class) {
if (_isNativeReflectConstruct()) {
_construct = Reflect.construct;
} else {
_construct = function _construct(Parent, args, Class) {
var a = [null];
a.push.apply(a, args);
var Constructor = Function.bind.apply(Parent, a);
var instance = new Constructor();
if (Class) _setPrototypeOf(instance, Class.prototype);
return instance;
};
}
return _construct.apply(null, arguments);
}
function _isNativeFunction(fn) {
return Function.toString.call(fn).indexOf("[native code]") !== -1;
}
function _wrapNativeSuper(Class) {
var _cache = typeof Map === "function" ? new Map() : undefined;
_wrapNativeSuper = function _wrapNativeSuper(Class) {
if (Class === null || !_isNativeFunction(Class)) return Class;
if (typeof Class !== "function") {
throw new TypeError("Super expression must either be null or a function");
}
if (typeof _cache !== "undefined") {
if (_cache.has(Class)) return _cache.get(Class);
_cache.set(Class, Wrapper);
}
function Wrapper() {
return _construct(Class, arguments, _getPrototypeOf(this).constructor);
}
Wrapper.prototype = Object.create(Class.prototype, {
constructor: {
value: Wrapper,
enumerable: false,
writable: true,
configurable: true
}
});
return _setPrototypeOf(Wrapper, Class);
};
return _wrapNativeSuper(Class);
}
function _assertThisInitialized(self) {
if (self === void 0) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return self;
}
function _possibleConstructorReturn(self, call) {
if (call && (typeof call === "object" || typeof call === "function")) {
return call;
}
return _assertThisInitialized(self);
}
function _createSuper(Derived) {
var hasNativeReflectConstruct = _isNativeReflectConstruct();
return function _createSuperInternal() {
var Super = _getPrototypeOf(Derived),
result;
if (hasNativeReflectConstruct) {
var NewTarget = _getPrototypeOf(this).constructor;
result = Reflect.construct(Super, arguments, NewTarget);
} else {
result = Super.apply(this, arguments);
}
return _possibleConstructorReturn(this, result);
};
}
var PersonnummerError = /*#__PURE__*/function (_Error) {
_inherits(PersonnummerError, _Error);
var _super = _createSuper(PersonnummerError);
function PersonnummerError() {
_classCallCheck(this, PersonnummerError);
return _super.call(this, 'Invalid swedish personal identity number');
}
return PersonnummerError;
}( /*#__PURE__*/_wrapNativeSuper(Error));
/**
* Compare the two dates and return -1, 0 or 1.
*
* @param {Date} dateLeft
* @param {Date} dateRight
*
* @return {number}
*/
var compareAsc = function compareAsc(dateLeft, dateRight) {
var diff = dateLeft.getTime() - dateRight.getTime();
// src/utils.ts
var compareAsc = (dateLeft, dateRight) => {
const diff = dateLeft.getTime() - dateRight.getTime();
return diff < 0 ? -1 : diff > 0 ? 1 : diff;
};
/**
* Get the number of full years between the given dates.
*
* @param {Date} dateLeft
* @param {Date} dateRight
*
* @return {number}
*/
var diffInYears = function diffInYears(dateLeft, dateRight) {
var sign = compareAsc(dateLeft, dateRight);
var yearDiff = Math.abs(dateLeft.getFullYear() - dateRight.getFullYear());
var diffInYears = (dateLeft, dateRight) => {
const sign = compareAsc(dateLeft, dateRight);
const yearDiff = Math.abs(dateLeft.getFullYear() - dateRight.getFullYear());
dateLeft.setFullYear(dateLeft.getFullYear() - sign * yearDiff);
var isLastYearNotFull = compareAsc(dateLeft, dateRight) === -sign;
var result = sign * (yearDiff - +isLastYearNotFull);
const isLastYearNotFull = compareAsc(dateLeft, dateRight) === -sign;
const result = sign * (yearDiff - +isLastYearNotFull);
return result === 0 ? 0 : result;
};
/**
* Calculates the Luhn checksum of a string of digits.
*
* @param {string|number} str
*
* @return {number}
*/
var luhn = function luhn(str) {
var sum = 0;
str += '';
for (var i = 0, l = str.length; i < l; i++) {
var v = parseInt(str[i]);
var luhn = (str) => {
let sum = 0;
str += "";
for (let i = 0, l = str.length; i < l; i++) {
let v = parseInt(str[i]);
v *= 2 - i % 2;
if (v > 9) {
v -= 9;
}
sum += v;
}
return Math.ceil(sum / 10) * 10 - sum;
};
/**
* Test if the input parameters are a valid date or not.
*
* @param {int} year
* @param {int} month
* @param {int} day
*
* @return {boolean}
*/
var testDate = function testDate(year, month, day) {
var testDate = (year, month, day) => {
month -= 1;
var date = new Date(year, month, day);
const date = new Date(year, month, day);
return !(date.getFullYear() !== year || date.getMonth() !== month || date.getDate() !== day);
};
var Personnummer = /*#__PURE__*/function () {
_createClass(Personnummer, [{
key: "century",
/**
* Personnummer century.
*
* @var {string}
*/
/**
* Get century.
*
* @return {string}
*/
get: function get() {
return this._century;
}
/**
* Personnummer full year.
*
* @var {string}
*/
}, {
key: "fullYear",
/**
* Get age.
*
* @return {string}
*/
get: function get() {
return this._fullYear;
}
/**
* Personnummer year.
*
* @var {string}
*/
}, {
key: "year",
/**
* Get age.
*
* @return {string}
*/
get: function get() {
return this._year;
}
/**
* Personnummer month.
*
* @var {string}
*/
}, {
key: "month",
/**
* Get month.
*
* @return {string}
*/
get: function get() {
return this._month;
}
/**
* Personnummer day.
*
* @var {string}
*/
}, {
key: "day",
/**
* Get day.
*
* @return {string}
*/
get: function get() {
return this._day;
}
/**
* Personnummer seperator.
*
* @var {string}
*/
}, {
key: "sep",
/**
* Get sep.
*
* @return {string}
*/
get: function get() {
return this._sep;
}
/**
* Personnumer first three of the last four numbers.
*
* @var {string}
*/
}, {
key: "num",
/**
* Get num.
*
* @return {string}
*/
get: function get() {
return this._num;
}
/**
* The last number of the personnummer.
*
* @var {string}
*/
}, {
key: "check",
/**
* Get check.
*
* @return {string}
*/
get: function get() {
return this._check;
}
/**
* Personnummer constructor.
*
* @param {string} ssn
* @param {object} options
*/
}]);
function Personnummer(ssn, options) {
_classCallCheck(this, Personnummer);
_defineProperty(this, "_century", '');
_defineProperty(this, "_fullYear", '');
_defineProperty(this, "_year", '');
_defineProperty(this, "_month", '');
_defineProperty(this, "_day", '');
_defineProperty(this, "_sep", '');
_defineProperty(this, "_num", '');
_defineProperty(this, "_check", '');
// src/index.ts
var Personnummer = class {
constructor(ssn, options) {
this._century = "";
this._fullYear = "";
this._year = "";
this._month = "";
this._day = "";
this._sep = "";
this._num = "";
this._check = "";
this.parse(ssn, options);
}
/**
* Parse personnummer.
*
* @param {string} ssn
* @param {object} options
*
* @return {Personnummer}
*/
_createClass(Personnummer, [{
key: "parse",
/**
* Parse personnummer and set class properties.
*
* @param {string} ssn
* @param {object} options
*/
// eslint-disable-next-line
value: function parse(ssn, options) {
var reg = /^(\d{2}){0,1}(\d{2})(\d{2})(\d{2})([+\-\s]?)((?!000)\d{3})(\d)$/;
var match = reg.exec(ssn);
if (!match) {
throw new PersonnummerError();
}
var century = match[1];
var year = match[2];
var month = match[3];
var day = match[4];
var sep = match[5];
var num = match[6];
var check = match[7];
if (typeof century === 'undefined' || !century.length) {
var d = new Date();
var baseYear = 0;
if (sep === '+') {
this._sep = '+';
baseYear = d.getFullYear() - 100;
} else {
this._sep = '-';
baseYear = d.getFullYear();
}
this._century = ('' + (baseYear - (baseYear - parseInt(year)) % 100)).substr(0, 2);
} else {
this._century = century;
if (new Date().getFullYear() - parseInt(century + year, 10) < 100) {
this._sep = '-';
} else {
this._sep = '+';
}
}
this._year = year;
this._fullYear = this._century + year;
this._month = month;
this._day = day;
this._num = num;
this._check = check;
if (!this.valid()) {
throw new PersonnummerError();
}
get century() {
return this._century;
}
get fullYear() {
return this._fullYear;
}
get year() {
return this._year;
}
get month() {
return this._month;
}
get day() {
return this._day;
}
get sep() {
return this._sep;
}
get num() {
return this._num;
}
get check() {
return this._check;
}
static parse(ssn, options) {
return new Personnummer(ssn, options);
}
static valid(ssn, options) {
try {
Personnummer.parse(ssn, options);
return true;
} catch (e) {
return false;
}
/**
* Validate a Swedish personal identity number.
*
* @return {boolean}
*/
}, {
key: "valid",
value: function valid() {
var valid = luhn(this.year + this.month + this.day + this.num) === +this.check && !!this.check;
if (valid && testDate(parseInt(this.century + this.year), +this.month, +this.day)) {
return valid;
}
return valid && testDate(parseInt(this.century + this.year), +this.month, +this.day - 60);
}
parse(ssn, options) {
const reg = /^(\d{2}){0,1}(\d{2})(\d{2})(\d{2})([+\-]?)((?!000)\d{3})(\d)$/;
const match = reg.exec(ssn);
if (!match) {
throw new PersonnummerError();
}
/**
* Format a Swedish personal identity number as one of the official formats,
* A long format or a short format.
*
* If the input number could not be parsed a empty string will be returned.
*
* @param {boolean} longFormat
*
* @return {string}
*/
}, {
key: "format",
value: function format() {
var longFormat = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
if (longFormat) {
return "".concat(this.century).concat(this.year).concat(this.month).concat(this.day).concat(this.num).concat(this.check);
const century = match[1];
const year = match[2];
const month = match[3];
const day = match[4];
const sep = match[5];
const num = match[6];
const check = match[7];
if (typeof century === "undefined" || !century.length) {
const d = new Date();
let baseYear = 0;
if (sep === "+") {
this._sep = "+";
baseYear = d.getFullYear() - 100;
} else {
this._sep = "-";
baseYear = d.getFullYear();
}
return "".concat(this.year).concat(this.month).concat(this.day).concat(this.sep).concat(this.num).concat(this.check);
}
/**
* Get age from a Swedish personal identity number.
*
* @return {number}
*/
}, {
key: "getAge",
value: function getAge() {
var ageDay = +this.day;
if (this.isCoordinationNumber()) {
ageDay -= 60;
this._century = ("" + (baseYear - (baseYear - parseInt(year)) % 100)).substr(0, 2);
} else {
this._century = century;
if (new Date().getFullYear() - parseInt(century + year, 10) < 100) {
this._sep = "-";
} else {
this._sep = "+";
}
var ageDate = this.century + this.year + '-' + this.month + '-' + (ageDay < 10 ? '0' + ageDay : ageDay);
return diffInYears(new Date(Date.now()), new Date(ageDate));
}
/**
* Check if a Swedish personal identity number is a coordination number or not.
*
* @return {boolean}
*/
}, {
key: "isCoordinationNumber",
value: function isCoordinationNumber() {
return testDate(parseInt(this.century + this.year), +this.month, +this.day - 60);
this._year = year;
this._fullYear = this._century + year;
this._month = month;
this._day = day;
this._num = num;
this._check = check;
if (!this.valid()) {
throw new PersonnummerError();
}
/**
* Check if a Swedish personal identity number is for a female.
*
* @return {boolean}
*/
}, {
key: "isFemale",
value: function isFemale() {
return !this.isMale();
}
valid() {
const valid = luhn(this.year + this.month + this.day + this.num) === +this.check && !!this.check;
if (valid && testDate(parseInt(this.century + this.year), +this.month, +this.day)) {
return valid;
}
/**
* Check if a Swedish personal identity number is for a male.
*
* @return {boolean}
*/
}, {
key: "isMale",
value: function isMale() {
var sexDigit = parseInt(this.num.substr(-1));
return sexDigit % 2 === 1;
return valid && testDate(parseInt(this.century + this.year), +this.month, +this.day - 60);
}
format(longFormat = false) {
if (longFormat) {
return `${this.century}${this.year}${this.month}${this.day}${this.num}${this.check}`;
}
}], [{
key: "parse",
value: function parse(ssn, options) {
return new Personnummer(ssn, options);
return `${this.year}${this.month}${this.day}${this.sep}${this.num}${this.check}`;
}
getAge() {
let ageDay = +this.day;
if (this.isCoordinationNumber()) {
ageDay -= 60;
}
/**
* Validate a Swedish personal identity number.
*
* @param {string} str
* @param {object} options
*
* @return {boolean}
*/
}, {
key: "valid",
value: function valid(ssn, options) {
try {
Personnummer.parse(ssn, options);
return true;
} catch (e) {
return false;
}
}
}]);
return Personnummer;
}();
export default Personnummer;
const ageDate = this.century + this.year + "-" + this.month + "-" + (ageDay < 10 ? "0" + ageDay : ageDay);
return diffInYears(new Date(Date.now()), new Date(ageDate));
}
isCoordinationNumber() {
return testDate(parseInt(this.century + this.year), +this.month, +this.day - 60);
}
isFemale() {
return !this.isMale();
}
isMale() {
const sexDigit = parseInt(this.num.substr(-1));
return sexDigit % 2 === 1;
}
};
var src_default = Personnummer;
export {
src_default as default
};

45

package.json
{
"name": "personnummer",
"description": "Validate Swedish personal identity numbers",
"version": "3.1.3",
"version": "3.1.4",
"license": "MIT",

@@ -20,9 +20,7 @@ "homepage": "https://github.com/personnummer/js",

"scripts": {
"build:types": "tsc --emitDeclarationOnly",
"build:js": "NODE_ENV=production rollup -c",
"build": "rimraf dist && npm run build:types && npm run build:js",
"build": "pine build",
"format": "prettier --write 'src/**/*.ts' test.ts",
"lint": "eslint src --ext .ts",
"prepublishOnly": "npm run build",
"test": "jest"
"test": "pine test"
},

@@ -33,23 +31,16 @@ "main": "dist/cjs/index.js",

"devDependencies": {
"@babel/cli": "^7.12.1",
"@babel/core": "^7.12.3",
"@babel/eslint-parser": "^7.12.1",
"@babel/preset-typescript": "^7.12.1",
"@jitesoft/babel-preset-main": "^2.6.1",
"@rollup/plugin-babel": "^5.2.1",
"@rollup/plugin-commonjs": "^17.0.0",
"@rollup/plugin-node-resolve": "^11.0.0",
"@types/jest": "^26.0.15",
"@typescript-eslint/eslint-plugin": "^4.7.0",
"@typescript-eslint/parser": "^4.7.0",
"babel-loader": "^8.2.1",
"core-js": "^3.7.0",
"eslint": "^7.13.0",
"jest": "^26.6.3",
"@pinefile/pine": "^1.0.0-alpha.28",
"@types/jest": "^26.0.23",
"@typescript-eslint/eslint-plugin": "^4.26.0",
"@typescript-eslint/parser": "^4.26.0",
"core-js": "^3.13.1",
"esbuild": "^0.12.6",
"esbuild-jest": "^0.5.0",
"eslint": "^7.27.0",
"is-ci": "^3.0.0",
"jest": "^27.0.4",
"node-fetch": "^2.6.1",
"prettier": "^2.1.2",
"prettier": "^2.3.0",
"rimraf": "^3.0.2",
"rollup": "^2.33.1",
"tslib": "^2.0.3",
"typescript": "^4.0.5"
"typescript": "^4.3.2"
},

@@ -63,3 +54,7 @@ "keywords": [

"numbers"
]
],
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/personnummer"
}
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc