MCPcopy Create free account
hub / github.com/debbide/GeoFill / generatePhone

Function generatePhone

scripts/generators.js:895–952  ·  view source on GitHub ↗

* 生成电话号码(根据国家格式)

(country)

Source from the content-addressed store, hash-verified

893 * 生成电话号码(根据国家格式)
894 */
895function generatePhone(country) {
896 const config = PHONE_FORMATS[country];
897
898 // 如果没有配置,使用默认美国格式
899 if (!config) {
900 const defaultConfig = PHONE_FORMATS['United States'];
901 const prefix = randomChoice(defaultConfig.areaCodePrefixes);
902 const remaining = randomDigits(defaultConfig.length - prefix.length);
903 return `${defaultConfig.code} ${defaultConfig.format(prefix + remaining)}`;
904 }
905
906 let phoneNumber = '';
907 let attempts = 0;
908
909 // 循环生成,直到满足质量要求 (避免 1234, 0000 等)
910 do {
911 attempts++;
912 phoneNumber = '';
913
914 // 处理有区号前缀的情况(如美国、加拿大、巴西、墨西哥)
915 if (config.areaCodePrefixes) {
916 const areaCode = randomChoice(config.areaCodePrefixes);
917
918 // 巴西特殊处理:区号 + 9 + 8位
919 if (config.mobileFirstDigit) {
920 const remaining = randomDigits(config.length - areaCode.length - 1);
921 phoneNumber = areaCode + config.mobileFirstDigit + remaining;
922 } else {
923 const remaining = randomDigits(config.length - areaCode.length);
924 phoneNumber = areaCode + remaining;
925 }
926 }
927 // 处理有手机前缀的情况(如中国、日本、英国等)
928 else if (config.mobilePrefixes) {
929 const prefix = randomChoice(config.mobilePrefixes);
930 const remaining = randomDigits(config.length - prefix.length);
931 phoneNumber = prefix + remaining;
932 }
933 // 默认情况
934 else {
935 phoneNumber = randomDigits(config.length);
936 }
937
938 // 质量检查:如果是日本,确保不包含 1234 或 0000
939 if (country === 'Japan') {
940 if (phoneNumber.includes('1234') || phoneNumber.includes('0000')) {
941 continue; // 重新生成
942 }
943 }
944
945 break; // 成功
946 } while (attempts < 5);
947
948 // 应用格式化
949 const formattedNumber = config.format(phoneNumber);
950
951 return config.code ? `${config.code} ${formattedNumber}` : formattedNumber;
952}

Callers 3

generateAllInfoFunction · 0.85
regenerateFieldFunction · 0.85

Calls 2

randomDigitsFunction · 0.85
randomChoiceFunction · 0.70

Tested by

no test coverage detected