UNPKG

french-ssn

Version:

🇫🇷 A parser / validator for French Social Security Number

82 lines • 3.01 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 }); const makeMonth_1 = __importDefault(require("./parse/makeMonth")); const makePlace_1 = __importDefault(require("./parse/makePlace")); const makeYear_1 = __importDefault(require("./parse/makeYear")); class Birth { constructor(month, year, place) { this.month = (0, makeMonth_1.default)(month); this.year = (0, makeYear_1.default)(year); const { country, county, city } = (0, makePlace_1.default)(place, this.year); this.country = country; this.county = county; this.city = city; } get approximateDate() { return makeApproximateBirthDate(this); } get approximateAge() { return makeApproximateAge(this.approximateDate); } toJSON() { const { month, year, country, county, city, approximateDate, approximateAge, } = this; return { month: noUndefined(month), year, country: noUndefined(country), county: noUndefined(county), city, approximateDate, approximateAge, }; } } exports.default = Birth; function noUndefined(_a) { var { unknown } = _a, rest = __rest(_a, ["unknown"]); if (unknown) { return Object.assign({ unknown }, rest); } return rest; } const millisecondsCountInOneDay = 1000 * 60 * 60 * 24; const dateBetween = (start, end) => { const date = new Date(start.valueOf()); date.setUTCDate(date.getUTCDate() + Math.round((end.valueOf() - start.valueOf()) / millisecondsCountInOneDay / 2)); return date; }; const makeApproximateBirthDate = ({ year, month }) => { const date = new Date(Date.UTC(year, 0, 1)); if (!month.unknown) { date.setUTCMonth(month.index - 1); const endOfMonth = new Date(date.valueOf()); endOfMonth.setUTCMonth(month.index); return dateBetween(date, endOfMonth); } const endOfYear = new Date(date.valueOf()); endOfYear.setUTCFullYear(year + 1); return dateBetween(date, endOfYear); }; const makeApproximateAge = (birthDate) => { const now = new Date(Date.now()); const monthsAge = (now.getFullYear() - birthDate.getFullYear()) * 12 + now.getMonth() - birthDate.getMonth(); return Math.floor(monthsAge / 12); }; //# sourceMappingURL=Birth.js.map