UNPKG

john-doe-gen

Version:

Generate completely random identities for testing projects and more!

65 lines (64 loc) 2.47 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.NameType = exports.Name = void 0; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); // Helper function to read JSON files from the 'info/names' folder within the john-doe-gen package const readJsonFile = (fileName) => { // Resolve the package's directory, using 'john-doe-gen' as the package name const packageDir = path_1.default.dirname(require.resolve('john-doe-gen/package.json')); // Build the file path relative to the package's 'info/names' directory const filePath = path_1.default.join(packageDir, 'info', 'names', fileName); // Check if the file exists if (!fs_1.default.existsSync(filePath)) { throw new Error(`File not found: ${filePath}`); } // Read the file and parse it const fileContent = fs_1.default.readFileSync(filePath, 'utf-8'); return JSON.parse(fileContent); }; const maleFirstNames = readJsonFile('male.json'); const femaleFirstNames = readJsonFile('female.json'); const unisexFirstNames = readJsonFile('unisex.json'); const lastNames = readJsonFile('last.json'); const NameType = { Male: 'Male', Female: 'Female', Unisex: 'Unisex' }; exports.NameType = NameType; class Name { static generateRandom(type, namePart) { let firstName; let lastName = lastNames[Math.floor(Math.random() * lastNames.length)]; switch (type) { case 'Male': firstName = maleFirstNames[Math.floor(Math.random() * maleFirstNames.length)]; break; case 'Female': firstName = femaleFirstNames[Math.floor(Math.random() * femaleFirstNames.length)]; break; case 'Unisex': firstName = unisexFirstNames[Math.floor(Math.random() * unisexFirstNames.length)]; break; default: throw new Error('Invalid NameType'); } if (namePart === 0) { return firstName; } else if (namePart === 1) { return lastName; } else if (namePart === 2) { return `${firstName} ${lastName}`; } else { throw new Error('Invalid namePart value'); } } } exports.Name = Name;