stdnum
Version:
Standard Number Validation
147 lines • 11.6 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.compact = exports.format = exports.validate = exports.abbreviation = exports.localName = exports.name = void 0;
const exceptions = __importStar(require("../exceptions"));
const util_1 = require("../util");
const checksum_1 = require("../util/checksum");
const ONE_HUNDRED_YEARS_IN_MS = 100 * 365 * 24 * 60 * 60 * 1000;
function clean(input) {
const [value, err] = util_1.strings.cleanUnicode(input, ' :');
if (err) {
return [value, err];
}
const [a, b, c] = util_1.strings.splitAt(value, -5, -4);
return [`${a.replace(/[-+]/g, '')}${b}${c}`, null];
}
function formatImpl(input) {
const [value] = clean(input);
let front, back, sep = '-';
if (value.length === 12 || value.length === 13) {
const [yyyy, mm, dd] = util_1.strings.splitAt(value, 0, 4, 6, 8);
const d = (0, util_1.buildDate)(yyyy, mm, dd);
if (d === null) {
return value;
}
if (new Date().getTime() - d.getTime() > ONE_HUNDRED_YEARS_IN_MS) {
sep = '+';
}
front = `${yyyy.substring(2)}${mm}${dd}`;
back = value.substring(value.length - 4);
}
else if (value.length === 10) {
front = value.substring(0, 6);
back = value.substring(6);
}
else if (value.length === 11) {
front = value.substring(0, 6);
sep = value[6];
back = value.substring(7);
}
else {
return value;
}
return `${front}${sep}${back}`;
}
const impl = {
name: 'Swedish Personal Identity Number',
localName: 'Personnummer',
compact(input) {
const [value, err] = clean(input);
if (err) {
throw err;
}
return value;
},
format: formatImpl,
validate(input) {
const [value, error] = clean(input);
if (error) {
return { isValid: false, error };
}
let a, b, c;
if (value.length === 11) {
[a, b, c] = util_1.strings.splitAt(value, -5, -4);
}
else if (value.length === 13) {
[, a, b, c] = util_1.strings.splitAt(value, -11, -5, -4);
}
else {
return { isValid: false, error: new exceptions.InvalidLength() };
}
if (!'-+'.includes(b)) {
return { isValid: false, error: new exceptions.InvalidFormat() };
}
const digits = `${a}${c}`;
if (!util_1.strings.isdigits(digits)) {
return { isValid: false, error: new exceptions.InvalidFormat() };
}
let yyyymmdd;
if (value.length === 13) {
yyyymmdd = value.substring(0, 8);
}
else {
const yymmdd = value.substring(0, 6);
const year = new Date().getFullYear();
let century = Math.floor(year / 100);
if (parseInt(yymmdd.substring(0, 2), 10) > year % 100) {
century -= 1;
}
if (b === '+') {
century -= 1;
}
yyyymmdd = `${century}${yymmdd}`;
}
const day = parseInt(yyyymmdd.substring(6, 8), 10);
if (day > 60) {
yyyymmdd =
yyyymmdd.substring(0, 6) + (day - 60).toString().padStart(2, '0');
}
if (!(0, util_1.isValidDateCompactYYYYMMDD)(yyyymmdd, true)) {
return { isValid: false, error: new exceptions.InvalidComponent() };
}
if (!(0, checksum_1.luhnChecksumValidate)(digits)) {
return { isValid: false, error: new exceptions.InvalidChecksum() };
}
return {
isValid: true,
compact: formatImpl(input),
isIndividual: true,
isCompany: false,
};
},
};
exports.name = impl.name, exports.localName = impl.localName, exports.abbreviation = impl.abbreviation, exports.validate = impl.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