french-ssn
Version:
🇫🇷 A parser / validator for French Social Security Number
72 lines • 2.45 kB
JavaScript
;
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 between_1 = __importDefault(require("./between"));
const countries_json_1 = __importDefault(require("../../data/countries.json"));
const counties_json_1 = __importDefault(require("../../data/counties.json"));
const unknown_1 = __importDefault(require("./unknown"));
const makeGetInsee = ({ items, error }) => (insee) => {
const item = items[insee];
if (!item) {
return Object.assign({ insee }, (0, unknown_1.default)(error));
}
return { insee, name: item, unknown: undefined };
};
const getCountry = makeGetInsee({ items: countries_json_1.default });
const getCounty = makeGetInsee({
items: counties_json_1.default,
error: "appears to be incorrect",
});
const makePlace = ({ country, county = (0, unknown_1.default)(), city = (0, unknown_1.default)(), }) => {
return {
country: country || (county.unknown ? (0, unknown_1.default)() : getCountry("100")),
county,
city,
};
};
const re = /^([0-8][0-9]|2[abAB]|9[0-69]|9[78][0-9])(\d+)$/;
const getParts = (insee) => {
const result = re.exec(insee);
if (!result) {
throw new Error(`Unkown error`);
}
const [countyCode, code] = result.slice(1);
return { countyCode, code };
};
exports.getParts = getParts;
exports.default = (insee, year) => {
const { countyCode, code } = (0, exports.getParts)(insee);
if (countyCode === "99") {
return makePlace({
country: getCountry(code),
});
}
if (year) {
if ((0, between_1.default)(91, Number(countyCode), 94) && year <= 1962) {
return makePlace({
country: getCountry("352"),
city: { insee },
});
}
if (countyCode === "95" && year <= 1964) {
return makePlace({
country: getCountry("350"),
city: { insee },
});
}
if (countyCode === "96" && year <= 1964) {
return makePlace({
country: getCountry("351"),
city: { insee },
});
}
}
return makePlace({
county: getCounty(countyCode),
city: { insee },
});
};
//# sourceMappingURL=makePlace.js.map