UNPKG

french-ssn

Version:

🇫🇷 A parser / validator for French Social Security Number

60 lines • 2.39 kB
"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getParts = void 0; const checkControlKey_1 = __importDefault(require("./checkControlKey")); const normalize_1 = __importDefault(require("./normalize")); const makeGender_1 = __importDefault(require("./parse/makeGender")); const Birth_1 = __importDefault(require("./Birth")); const re = /^((\d)(\d{2})(\d{2})(\d{5}|2[abAB]\d{3})(\d{3}))(\d{2})$/; const getParts = (ssn) => { ssn = (0, normalize_1.default)(ssn); const parts = re.exec(ssn); if (!parts) { throw new Error("Unexpected error"); } const [partialSsn, gender, year, month, place, rank, controlKey] = parts.slice(1); return { partialSsn, gender, year, month, place, rank, controlKey }; }; exports.getParts = getParts; class Ssn { constructor(ssn) { const { partialSsn, gender, year, month, place, // eslint-disable-next-line @typescript-eslint/no-unused-vars rank, controlKey, } = (0, exports.getParts)(ssn); (0, checkControlKey_1.default)(partialSsn, controlKey); this.gender = (0, makeGender_1.default)(gender); this.birth = new Birth_1.default(month, year, place); this.provisional = isProvisional(gender, controlKey); } toJSON() { const { gender, provisional } = this; const _a = this.birth.toJSON(), { approximateAge } = _a, birth = __rest(_a, ["approximateAge"]); return { birth, gender, provisional, approximateAge, }; } } function isProvisional(gender, controlKey) { return Number(gender) > 2 || controlKey === "98"; } exports.default = (ssn) => { return new Ssn(ssn).toJSON(); }; //# sourceMappingURL=parse.js.map