faker.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. import fs from "fs";
  2. import path from "path";
  3. import { fileURLToPath } from "url";
  4. const __filename = fileURLToPath(import.meta.url);
  5. const __dirname = path.dirname(__filename);
  6. const usedNames = new Set();
  7. const usedEmails = new Set();
  8. const usedNumbers = new Set();
  9. const usedSlugs = new Set();
  10. const usedCurrencies = new Set();
  11. export function generateName() {
  12. const adjectives = [
  13. "Cool",
  14. "Smart",
  15. "Fast",
  16. "Sleek",
  17. "Innovative",
  18. "Shiny",
  19. "Bold",
  20. "Elegant",
  21. "Epic",
  22. "Mystic",
  23. "Brilliant",
  24. "Luminous",
  25. ];
  26. const nouns = [
  27. "Star",
  28. "Vision",
  29. "Echo",
  30. "Spark",
  31. "Horizon",
  32. "Nova",
  33. "Shadow",
  34. "Wave",
  35. "Pulse",
  36. "Vortex",
  37. "Zenith",
  38. "Element",
  39. ];
  40. let name = "";
  41. do {
  42. const adj = adjectives[Math.floor(Math.random() * adjectives.length)];
  43. const noun = nouns[Math.floor(Math.random() * nouns.length)];
  44. name = `${adj} ${noun}`;
  45. } while (usedNames.has(name));
  46. usedNames.add(name);
  47. return name;
  48. }
  49. export function generateFirstName() {
  50. const firstNames = [
  51. "James",
  52. "Emma",
  53. "Liam",
  54. "Olivia",
  55. "Noah",
  56. "Ava",
  57. "William",
  58. "Sophia",
  59. "Benjamin",
  60. "Isabella",
  61. "Lucas",
  62. "Mia",
  63. ];
  64. return firstNames[Math.floor(Math.random() * firstNames.length)];
  65. }
  66. export function generateLastName() {
  67. const lastNames = [
  68. "Smith",
  69. "Johnson",
  70. "Brown",
  71. "Williams",
  72. "Jones",
  73. "Garcia",
  74. "Miller",
  75. "Davis",
  76. "Rodriguez",
  77. "Martinez",
  78. "Hernandez",
  79. "Lopez",
  80. ];
  81. return lastNames[Math.floor(Math.random() * lastNames.length)];
  82. }
  83. export function generateFullName() {
  84. return `${generateFirstName()} ${generateLastName()}`;
  85. }
  86. export function generateEmail() {
  87. const adjectives = [
  88. "Cool",
  89. "Smart",
  90. "Fast",
  91. "Sleek",
  92. "Innovative",
  93. "Shiny",
  94. "Bold",
  95. "Elegant",
  96. "Epic",
  97. "Mystic",
  98. "Brilliant",
  99. "Luminous",
  100. ];
  101. const nouns = [
  102. "Star",
  103. "Vision",
  104. "Echo",
  105. "Spark",
  106. "Horizon",
  107. "Nova",
  108. "Shadow",
  109. "Wave",
  110. "Pulse",
  111. "Vortex",
  112. "Zenith",
  113. "Element",
  114. ];
  115. let email = "";
  116. do {
  117. const adj = adjectives[Math.floor(Math.random() * adjectives.length)];
  118. const noun = nouns[Math.floor(Math.random() * nouns.length)];
  119. const number = Math.floor(1000 + Math.random() * 9000);
  120. email = `${adj}${noun}${number}@example.com`.toLowerCase();
  121. } while (usedEmails.has(email));
  122. usedEmails.add(email);
  123. return email;
  124. }
  125. export function generatePhoneNumber() {
  126. let phoneNumber;
  127. do {
  128. phoneNumber = Math.floor(6000000000 + Math.random() * 4000000000);
  129. } while (usedNumbers.has(phoneNumber));
  130. usedNumbers.add(phoneNumber);
  131. return `${phoneNumber}`;
  132. }
  133. export function generateLocation() {
  134. const location = [
  135. "New York",
  136. "Los Angeles",
  137. "Chicago",
  138. "Houston",
  139. "Phoenix",
  140. "Philadelphia",
  141. "San Antonio",
  142. "San Diego",
  143. "Dallas",
  144. "San Jose",
  145. "Austin",
  146. "Jacksonville",
  147. "San Francisco",
  148. "Indianapolis",
  149. "Columbus",
  150. "Fort Worth",
  151. ];
  152. return location[Math.floor(Math.random() * location.length)];
  153. }
  154. export function generateSKU() {
  155. const letters = Array.from({ length: 3 }, () =>
  156. String.fromCharCode(65 + Math.floor(Math.random() * 26))
  157. ).join("");
  158. const numbers = Math.floor(1000 + Math.random() * 9000);
  159. return `${letters}${numbers}`;
  160. }
  161. export function generateSlug(delimiter = "-") {
  162. let slug;
  163. do {
  164. const name = generateName();
  165. const randomStr = Math.random().toString(36).substring(2, 8);
  166. slug = `${name
  167. .toLowerCase()
  168. .replace(/\s+/g, delimiter)}${delimiter}${randomStr}`;
  169. } while (usedSlugs.has(slug));
  170. usedSlugs.add(slug);
  171. return slug;
  172. }
  173. export function generateDescription(length = 255) {
  174. const phrases = [
  175. "An innovative and sleek design.",
  176. "Built for speed and efficiency.",
  177. "Experience the future today.",
  178. "A perfect blend of style and power.",
  179. "Engineered to perfection.",
  180. "Designed for those who dream big.",
  181. "Unleash creativity with this masterpiece.",
  182. "A game-changer in every way.",
  183. "Smart, fast, and reliable.",
  184. "The perfect companion for your journey.",
  185. "Crafted with precision and excellence.",
  186. "Innovation that redefines possibilities.",
  187. "Enhancing your experience like never before.",
  188. "Where technology meets elegance.",
  189. "Power, performance, and perfection combined.",
  190. "Redefining the way you experience the world.",
  191. "A masterpiece of engineering and design.",
  192. "Unmatched quality and exceptional performance.",
  193. "Designed to elevate your lifestyle.",
  194. "Beyond expectations, beyond limits.",
  195. ];
  196. let description = "";
  197. while (description.length < length) {
  198. let phrase = phrases[Math.floor(Math.random() * phrases.length)];
  199. if (description.length + phrase.length <= length) {
  200. description += (description ? " " : "") + phrase;
  201. } else {
  202. description +=
  203. " " + phrase.substring(0, length - description.length);
  204. break;
  205. }
  206. }
  207. return description.trim();
  208. }
  209. export function generateHostname() {
  210. const words = [
  211. "tech",
  212. "cloud",
  213. "byte",
  214. "stream",
  215. "nexus",
  216. "core",
  217. "pulse",
  218. "data",
  219. "sync",
  220. "wave",
  221. "hub",
  222. "zone",
  223. ];
  224. const domains = [".com", ".net", ".io", ".ai", ".xyz", ".co"];
  225. const part1 = words[Math.floor(Math.random() * words.length)];
  226. const part2 = words[Math.floor(Math.random() * words.length)];
  227. const domain = domains[Math.floor(Math.random() * domains.length)];
  228. return `https://${part1}${part2}${domain}`;
  229. }
  230. export function generateCurrency() {
  231. const currencies = [
  232. {
  233. name: "US Dollar",
  234. code: "USD",
  235. symbol: "$",
  236. decimalDigits: "2",
  237. groupSeparator: ",",
  238. decimalSeparator: ".",
  239. },
  240. {
  241. name: "Euro",
  242. code: "EUR",
  243. symbol: "€",
  244. decimalDigits: "2",
  245. groupSeparator: ".",
  246. decimalSeparator: ",",
  247. },
  248. {
  249. name: "British Pound",
  250. code: "GBP",
  251. symbol: "£",
  252. decimalDigits: "2",
  253. groupSeparator: ",",
  254. decimalSeparator: ".",
  255. },
  256. {
  257. name: "Japanese Yen",
  258. code: "JPY",
  259. symbol: "¥",
  260. decimalDigits: "0",
  261. groupSeparator: ",",
  262. decimalSeparator: ".",
  263. },
  264. {
  265. name: "Australian Dollar",
  266. code: "AUD",
  267. symbol: "A$",
  268. decimalDigits: "2",
  269. groupSeparator: ",",
  270. decimalSeparator: ".",
  271. },
  272. {
  273. name: "Canadian Dollar",
  274. code: "CAD",
  275. symbol: "C$",
  276. decimalDigits: "2",
  277. groupSeparator: ",",
  278. decimalSeparator: ".",
  279. },
  280. {
  281. name: "Swiss Franc",
  282. code: "CHF",
  283. symbol: "CHF",
  284. decimalDigits: "2",
  285. groupSeparator: "'",
  286. decimalSeparator: ".",
  287. },
  288. {
  289. name: "Chinese Yuan",
  290. code: "CNY",
  291. symbol: "¥",
  292. decimalDigits: "2",
  293. groupSeparator: ",",
  294. decimalSeparator: ".",
  295. },
  296. {
  297. name: "Indian Rupee",
  298. code: "INR",
  299. symbol: "₹",
  300. decimalDigits: "2",
  301. groupSeparator: ",",
  302. decimalSeparator: ".",
  303. },
  304. {
  305. name: "South Korean Won",
  306. code: "KRW",
  307. symbol: "₩",
  308. decimalDigits: "0",
  309. groupSeparator: ",",
  310. decimalSeparator: ".",
  311. },
  312. {
  313. name: "Mexican Peso",
  314. code: "MXN",
  315. symbol: "MX$",
  316. decimalDigits: "2",
  317. groupSeparator: ",",
  318. decimalSeparator: ".",
  319. },
  320. {
  321. name: "Russian Ruble",
  322. code: "RUB",
  323. symbol: "₽",
  324. decimalDigits: "2",
  325. groupSeparator: " ",
  326. decimalSeparator: ",",
  327. },
  328. {
  329. name: "Brazilian Real",
  330. code: "BRL",
  331. symbol: "R$",
  332. decimalDigits: "2",
  333. groupSeparator: ".",
  334. decimalSeparator: ",",
  335. },
  336. {
  337. name: "South African Rand",
  338. code: "ZAR",
  339. symbol: "R",
  340. decimalDigits: "2",
  341. groupSeparator: " ",
  342. decimalSeparator: ",",
  343. },
  344. {
  345. name: "New Zealand Dollar",
  346. code: "NZD",
  347. symbol: "NZ$",
  348. decimalDigits: "2",
  349. groupSeparator: ",",
  350. decimalSeparator: ".",
  351. },
  352. {
  353. name: "Singapore Dollar",
  354. code: "SGD",
  355. symbol: "S$",
  356. decimalDigits: "2",
  357. groupSeparator: ",",
  358. decimalSeparator: ".",
  359. },
  360. {
  361. name: "Hong Kong Dollar",
  362. code: "HKD",
  363. symbol: "HK$",
  364. decimalDigits: "2",
  365. groupSeparator: ",",
  366. decimalSeparator: ".",
  367. },
  368. {
  369. name: "Norwegian Krone",
  370. code: "NOK",
  371. symbol: "kr",
  372. decimalDigits: "2",
  373. groupSeparator: " ",
  374. decimalSeparator: ",",
  375. },
  376. {
  377. name: "Swedish Krona",
  378. code: "SEK",
  379. symbol: "kr",
  380. decimalDigits: "2",
  381. groupSeparator: " ",
  382. decimalSeparator: ",",
  383. },
  384. {
  385. name: "Danish Krone",
  386. code: "DKK",
  387. symbol: "kr",
  388. decimalDigits: "2",
  389. groupSeparator: ".",
  390. decimalSeparator: ",",
  391. },
  392. {
  393. name: "Polish Złoty",
  394. code: "PLN",
  395. symbol: "zł",
  396. decimalDigits: "2",
  397. groupSeparator: " ",
  398. decimalSeparator: ",",
  399. },
  400. {
  401. name: "Turkish Lira",
  402. code: "TRY",
  403. symbol: "₺",
  404. decimalDigits: "2",
  405. groupSeparator: ".",
  406. decimalSeparator: ",",
  407. },
  408. {
  409. name: "Thai Baht",
  410. code: "THB",
  411. symbol: "฿",
  412. decimalDigits: "2",
  413. groupSeparator: ",",
  414. decimalSeparator: ".",
  415. },
  416. {
  417. name: "Indonesian Rupiah",
  418. code: "IDR",
  419. symbol: "Rp",
  420. decimalDigits: "0",
  421. groupSeparator: ".",
  422. decimalSeparator: ",",
  423. },
  424. {
  425. name: "Malaysian Ringgit",
  426. code: "MYR",
  427. symbol: "RM",
  428. decimalDigits: "2",
  429. groupSeparator: ",",
  430. decimalSeparator: ".",
  431. },
  432. {
  433. name: "Philippine Peso",
  434. code: "PHP",
  435. symbol: "₱",
  436. decimalDigits: "2",
  437. groupSeparator: ",",
  438. decimalSeparator: ".",
  439. },
  440. {
  441. name: "Israeli Shekel",
  442. code: "ILS",
  443. symbol: "₪",
  444. decimalDigits: "2",
  445. groupSeparator: ",",
  446. decimalSeparator: ".",
  447. },
  448. {
  449. name: "Saudi Riyal",
  450. code: "SAR",
  451. symbol: "﷼",
  452. decimalDigits: "2",
  453. groupSeparator: ",",
  454. decimalSeparator: ".",
  455. },
  456. {
  457. name: "UAE Dirham",
  458. code: "AED",
  459. symbol: "د.إ",
  460. decimalDigits: "2",
  461. groupSeparator: ",",
  462. decimalSeparator: ".",
  463. },
  464. {
  465. name: "Czech Koruna",
  466. code: "CZK",
  467. symbol: "Kč",
  468. decimalDigits: "2",
  469. groupSeparator: " ",
  470. decimalSeparator: ",",
  471. },
  472. {
  473. name: "Hungarian Forint",
  474. code: "HUF",
  475. symbol: "Ft",
  476. decimalDigits: "0",
  477. groupSeparator: " ",
  478. decimalSeparator: ",",
  479. },
  480. {
  481. name: "Romanian Leu",
  482. code: "RON",
  483. symbol: "lei",
  484. decimalDigits: "2",
  485. groupSeparator: ".",
  486. decimalSeparator: ",",
  487. },
  488. {
  489. name: "Bulgarian Lev",
  490. code: "BGN",
  491. symbol: "лв",
  492. decimalDigits: "2",
  493. groupSeparator: " ",
  494. decimalSeparator: ",",
  495. },
  496. {
  497. name: "Croatian Kuna",
  498. code: "HRK",
  499. symbol: "kn",
  500. decimalDigits: "2",
  501. groupSeparator: ".",
  502. decimalSeparator: ",",
  503. },
  504. {
  505. name: "Icelandic Króna",
  506. code: "ISK",
  507. symbol: "kr",
  508. decimalDigits: "0",
  509. groupSeparator: ".",
  510. decimalSeparator: ",",
  511. },
  512. {
  513. name: "Ukrainian Hryvnia",
  514. code: "UAH",
  515. symbol: "₴",
  516. decimalDigits: "2",
  517. groupSeparator: " ",
  518. decimalSeparator: ",",
  519. },
  520. {
  521. name: "Pakistani Rupee",
  522. code: "PKR",
  523. symbol: "₨",
  524. decimalDigits: "2",
  525. groupSeparator: ",",
  526. decimalSeparator: ".",
  527. },
  528. {
  529. name: "Bangladeshi Taka",
  530. code: "BDT",
  531. symbol: "৳",
  532. decimalDigits: "2",
  533. groupSeparator: ",",
  534. decimalSeparator: ".",
  535. },
  536. {
  537. name: "Sri Lankan Rupee",
  538. code: "LKR",
  539. symbol: "Rs",
  540. decimalDigits: "2",
  541. groupSeparator: ",",
  542. decimalSeparator: ".",
  543. },
  544. {
  545. name: "Nepalese Rupee",
  546. code: "NPR",
  547. symbol: "₨",
  548. decimalDigits: "2",
  549. groupSeparator: ",",
  550. decimalSeparator: ".",
  551. },
  552. {
  553. name: "Kuwaiti Dinar",
  554. code: "KWD",
  555. symbol: "د.ك",
  556. decimalDigits: "3",
  557. groupSeparator: ",",
  558. decimalSeparator: ".",
  559. },
  560. {
  561. name: "Qatari Riyal",
  562. code: "QAR",
  563. symbol: "ر.ق",
  564. decimalDigits: "2",
  565. groupSeparator: ",",
  566. decimalSeparator: ".",
  567. },
  568. {
  569. name: "Omani Rial",
  570. code: "OMR",
  571. symbol: "ر.ع.",
  572. decimalDigits: "3",
  573. groupSeparator: ",",
  574. decimalSeparator: ".",
  575. },
  576. {
  577. name: "Bahraini Dinar",
  578. code: "BHD",
  579. symbol: "ب.د",
  580. decimalDigits: "3",
  581. groupSeparator: ",",
  582. decimalSeparator: ".",
  583. },
  584. {
  585. name: "Argentine Peso",
  586. code: "ARS",
  587. symbol: "$",
  588. decimalDigits: "2",
  589. groupSeparator: ".",
  590. decimalSeparator: ",",
  591. },
  592. {
  593. name: "Chilean Peso",
  594. code: "CLP",
  595. symbol: "$",
  596. decimalDigits: "0",
  597. groupSeparator: ".",
  598. decimalSeparator: ",",
  599. },
  600. {
  601. name: "Colombian Peso",
  602. code: "COP",
  603. symbol: "$",
  604. decimalDigits: "0",
  605. groupSeparator: ".",
  606. decimalSeparator: ",",
  607. },
  608. {
  609. name: "Peruvian Sol",
  610. code: "PEN",
  611. symbol: "S/.",
  612. decimalDigits: "2",
  613. groupSeparator: ",",
  614. decimalSeparator: ".",
  615. },
  616. {
  617. name: "Venezuelan Bolívar",
  618. code: "VES",
  619. symbol: "Bs.",
  620. decimalDigits: "2",
  621. groupSeparator: ".",
  622. decimalSeparator: ",",
  623. },
  624. ];
  625. if (usedCurrencies.size >= currencies.length) {
  626. throw new Error("All currencies have been used.");
  627. }
  628. let currency;
  629. do {
  630. const randomIndex = Math.floor(Math.random() * currencies.length);
  631. currency = currencies[randomIndex];
  632. } while (usedCurrencies.has(currency.code));
  633. usedCurrencies.add(currency.code);
  634. return currency;
  635. }
  636. export function randomElement(array) {
  637. return array[Math.floor(Math.random() * array.length)];
  638. }
  639. export function getImageFile(
  640. directory = path.resolve(__dirname, "../data/images/")
  641. ) {
  642. if (!fs.existsSync(directory)) {
  643. throw new Error(`Directory does not exist: ${directory}`);
  644. }
  645. const files = fs.readdirSync(directory);
  646. const imageFiles = files.filter((file) =>
  647. /\.(gif|jpeg|jpg|png|svg|webp)$/i.test(file)
  648. );
  649. if (!imageFiles.length) {
  650. throw new Error("No image files found in the directory.");
  651. }
  652. const randomIndex = Math.floor(Math.random() * imageFiles.length);
  653. return path.join(directory, imageFiles[randomIndex]);
  654. }