UNPKG

thai-citizenid-gen

Version:

A library for generating valid Thai Citizen IDs and mock Thai personal data. Updated to correctly follow the ID generation algorithm and resolve security vulnerabilities in dependencies.

61 lines (60 loc) 3.01 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.generateMockPerson = generateMockPerson; const faker_1 = require("@faker-js/faker"); const thaiReligions_json_1 = __importDefault(require("./data/thaiReligions.json")); const thaiProvinces_json_1 = __importDefault(require("./data/thaiProvinces.json")); function generateBirthDate() { return faker_1.faker.date.birthdate({ min: 18, max: 75, mode: 'age' }).toISOString().split('T')[0]; } function calculateAge(birthDate) { const birth = new Date(birthDate); return Math.floor((new Date().getTime() - birth.getTime()) / (1000 * 3600 * 24 * 365)); } function generateThaiID() { const idNumber = [...Array(12)].map(() => Math.floor(Math.random() * 10)); const checksum = idNumber.reduce((sum, num, index) => sum + (13 - index) * num, 0) % 11; idNumber.push((11 - checksum) % 10); return idNumber.join(''); } function generateMockPerson() { const gender = faker_1.faker.helpers.arrayElement(['ชาย', 'หญิง']); const firstName = faker_1.faker.person.firstName(gender === 'ชาย' ? 'male' : 'female'); const lastName = faker_1.faker.person.lastName(); const birthDate = generateBirthDate(); const age = calculateAge(birthDate); const religion = faker_1.faker.helpers.arrayElement(thaiReligions_json_1.default); const randomProvince = faker_1.faker.helpers.arrayElement(thaiProvinces_json_1.default); const province = randomProvince.province; const randomDistrict = faker_1.faker.helpers.arrayElement(randomProvince.districts); const randomSubdistrict = faker_1.faker.helpers.arrayElement(randomDistrict.subdistricts); const postcode = randomSubdistrict.postcode; const issuedDate = faker_1.faker.date.past({ years: 10 }); const expiredDate = new Date(issuedDate.getTime()); expiredDate.setFullYear(expiredDate.getFullYear() + 10); let address = `123 หมู่ 4 ต.${randomSubdistrict.subdistrict} อ.${randomDistrict.district} จ.${province} ${postcode}`; if (province === 'กรุงเทพมหานคร') { address = `123 หมู่ 4 แขวง${randomSubdistrict.subdistrict} เขต${randomDistrict.district} จ.${province} ${postcode}`; } return { id: generateThaiID(), firstName, lastName, gender, birthDate, age, religion, laserCode: `JT${faker_1.faker.number.int({ min: 1000000, max: 9999999 })}-${faker_1.faker.number.int({ min: 100, max: 999 })}`, issuedDate: issuedDate.toISOString().split('T')[0], expiredDate: expiredDate.toISOString().split('T')[0], englishName: `${firstName} ${lastName}`, province, district: randomDistrict.district, subdistrict: randomSubdistrict.subdistrict, postcode, address }; }