mirror of
https://github.com/swc-project/swc.git
synced 2024-12-26 07:02:28 +03:00
266 lines
7.4 KiB
TypeScript
266 lines
7.4 KiB
TypeScript
// Loaded from https://deno.land/x/segno@v1.1.0/lib/validations/isMobilePhone.ts
|
|
|
|
|
|
// @ts-ignore allowing typedoc to build
|
|
import { assertString } from '../helpers/assertString.ts';
|
|
|
|
type PhoneLocale =
|
|
| 'am-AM'
|
|
| 'ar-AE'
|
|
| 'ar-BH'
|
|
| 'ar-DZ'
|
|
| 'ar-EG'
|
|
| 'ar-IQ'
|
|
| 'ar-JO'
|
|
| 'ar-KW'
|
|
| 'ar-LY'
|
|
| 'ar-SA'
|
|
| 'ar-SY'
|
|
| 'ar-TN'
|
|
| 'bs-BA'
|
|
| 'be-BY'
|
|
| 'bg-BG'
|
|
| 'bn-BD'
|
|
| 'cs-CZ'
|
|
| 'da-DK'
|
|
| 'de-DE'
|
|
| 'de-AT'
|
|
| 'de-CH'
|
|
| 'el-GR'
|
|
| 'en-AU'
|
|
| 'en-GB'
|
|
| 'en-GG'
|
|
| 'en-GH'
|
|
| 'en-HK'
|
|
| 'en-MO'
|
|
| 'en-IE'
|
|
| 'en-IN'
|
|
| 'en-KE'
|
|
| 'en-MT'
|
|
| 'en-MU'
|
|
| 'en-NG'
|
|
| 'en-NZ'
|
|
| 'en-PK'
|
|
| 'en-RW'
|
|
| 'en-SG'
|
|
| 'en-SL'
|
|
| 'en-TZ'
|
|
| 'en-UG'
|
|
| 'en-US'
|
|
| 'en-ZA'
|
|
| 'en-ZM'
|
|
| 'en-ZW'
|
|
| 'es-CO'
|
|
| 'es-CL'
|
|
| 'es-CR'
|
|
| 'es-EC'
|
|
| 'es-ES'
|
|
| 'es-MX'
|
|
| 'es-PA'
|
|
| 'es-PY'
|
|
| 'es-UY'
|
|
| 'et-EE'
|
|
| 'fa-IR'
|
|
| 'fi-FI'
|
|
| 'fj-FJ'
|
|
| 'fo-FO'
|
|
| 'fr-FR'
|
|
| 'fr-GF'
|
|
| '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-BA': /^((((\+|00)3876)|06))((([0-3]|[5-6])\d{6})|(4\d{7}))$/,
|
|
'be-BY': /^(\+?375)?(24|25|29|33|44)\d{7}$/,
|
|
'bg-BG': /^(\+?359|0)?8[789]\d{7}$/,
|
|
'bn-BD': /^(\+?880|0)1[13456789][0-9]{8}$/,
|
|
'cs-CZ': /^(\+?420)? ?[1-9][0-9]{2} ?[0-9]{3} ?[0-9]{3}$/,
|
|
'da-DK': /^(\+?45)?\s?\d{2}\s?\d{2}\s?\d{2}\s?\d{2}$/,
|
|
'de-DE': /^(\+49)?0?[1|3]([0|5][0-45-9]\d|6([23]|0\d?)|7([0-57-9]|6\d))\d{7}$/,
|
|
'de-AT': /^(\+43|0)\d{1,4}\d{3,12}$/,
|
|
'de-CH': /^(\+41|0)(7[5-9])\d{1,7}$/,
|
|
'el-GR': /^(\+?30|0)?(69\d{8})$/,
|
|
'en-AU': /^(\+?61|0)4\d{8}$/,
|
|
'en-GB': /^(\+?44|0)7\d{9}$/,
|
|
'en-GG': /^(\+?44|0)1481\d{6}$/,
|
|
'en-GH': /^(\+233|0)(20|50|24|54|27|57|26|56|23|28)\d{7}$/,
|
|
'en-HK': /^(\+?852[-\s]?)?[456789]\d{3}[-\s]?\d{4}$/,
|
|
'en-MO': /^(\+?853[-\s]?)?[6]\d{3}[-\s]?\d{4}$/,
|
|
'en-IE': /^(\+?353|0)8[356789]\d{7}$/,
|
|
'en-IN': /^(\+?91|0)?[6789]\d{9}$/,
|
|
'en-KE': /^(\+?254|0)(7|1)\d{8}$/,
|
|
'en-MT': /^(\+?356|0)?(99|79|77|21|27|22|25)[0-9]{6}$/,
|
|
'en-MU': /^(\+?230|0)?\d{8}$/,
|
|
'en-NG': /^(\+?234|0)?[789]\d{9}$/,
|
|
'en-NZ': /^(\+?64|0)[28]\d{7,9}$/,
|
|
'en-PK': /^((\+92)|(0092))-{0,1}\d{3}-{0,1}\d{7}$|^\d{11}$|^\d{4}-\d{7}$/,
|
|
'en-RW': /^(\+?250|0)?[7]\d{8}$/,
|
|
'en-SG': /^(\+65)?[689]\d{7}$/,
|
|
'en-SL': /^(?:0|94|\+94)?(7(0|1|2|5|6|7|8)( |-)?\d)\d{6}$/,
|
|
'en-TZ': /^(\+?255|0)?[67]\d{8}$/,
|
|
'en-UG': /^(\+?256|0)?[7]\d{8}$/,
|
|
'en-US': /^((\+1|1)?( |-)?)?(\([2-9][0-9]{2}\)|[2-9][0-9]{2})( |-)?([2-9][0-9]{2}( |-)?[0-9]{4})$/,
|
|
'en-ZA': /^(\+?27|0)\d{9}$/,
|
|
'en-ZM': /^(\+?26)?09[567]\d{7}$/,
|
|
'en-ZW': /^(\+263)[0-9]{9}$/,
|
|
'es-CO': /^(\+?57)?([1-8]{1}|3[0-9]{2})?[2-9]{1}\d{6}$/,
|
|
'es-CL': /^(\+?56|0)[2-9]\d{1}\d{7}$/,
|
|
'es-CR': /^(\+506)?[2-8]\d{7}$/,
|
|
'es-EC': /^(\+?593|0)([2-7]|9[2-9])\d{7}$/,
|
|
'es-ES': /^(\+?34)?[6|7]\d{8}$/,
|
|
'es-MX': /^(\+?52)?(1|01)?\d{10,11}$/,
|
|
'es-PA': /^(\+?507)\d{7,8}$/,
|
|
'es-PY': /^(\+?595|0)9[9876]\d{7}$/,
|
|
'es-UY': /^(\+598|0)9[1-9][\d]{6}$/,
|
|
'et-EE': /^(\+?372)?\s?(5|8[1-4])\s?([0-9]\s?){6,7}$/,
|
|
'fa-IR': /^(\+?98[\-\s]?|0)9[0-39]\d[\-\s]?\d{3}[\-\s]?\d{4}$/,
|
|
'fi-FI': /^(\+?358|0)\s?(4(0|1|2|4|5|6)?|50)\s?(\d\s?){4,8}\d$/,
|
|
'fj-FJ': /^(\+?679)?\s?\d{3}\s?\d{4}$/,
|
|
'fo-FO': /^(\+?298)?\s?\d{2}\s?\d{2}\s?\d{2}$/,
|
|
'fr-FR': /^(\+?33|0)[67]\d{8}$/,
|
|
'fr-GF': /^(\+?594|0|00594)[67]\d{8}$/,
|
|
'fr-GP': /^(\+?590|0|00590)[67]\d{8}$/,
|
|
'fr-MQ': /^(\+?596|0|00596)[67]\d{8}$/,
|
|
'fr-RE': /^(\+?262|0|00262)[67]\d{8}$/,
|
|
'he-IL': /^(\+972|0)([23489]|5[012345689]|77)[1-9]\d{6}$/,
|
|
'hu-HU': /^(\+?36)(20|30|70)\d{7}$/,
|
|
'id-ID': /^(\+?62|0)8(1[123456789]|2[1238]|3[1238]|5[12356789]|7[78]|9[56789]|8[123456789])([\s?|\d]{5,11})$/,
|
|
'it-IT': /^(\+?39)?\s?3\d{2} ?\d{6,7}$/,
|
|
'ja-JP': /^(\+81[ \-]?(\(0\))?|0)[6789]0[ \-]?\d{4}[ \-]?\d{4}$/,
|
|
'kk-KZ': /^(\+?7|8)?7\d{9}$/,
|
|
'kl-GL': /^(\+?299)?\s?\d{2}\s?\d{2}\s?\d{2}$/,
|
|
'ko-KR': /^((\+?82)[ \-]?)?0?1([0|1|6|7|8|9]{1})[ \-]?\d{3,4}[ \-]?\d{4}$/,
|
|
'lt-LT': /^(\+370|8)\d{8}$/,
|
|
'ms-MY': /^(\+?6?01){1}(([0145]{1}(\-|\s)?\d{7,8})|([236789]{1}(\s|\-)?\d{7}))$/,
|
|
'nb-NO': /^(\+?47)?[49]\d{7}$/,
|
|
'ne-NP': /^(\+?977)?9[78]\d{8}$/,
|
|
'nl-BE': /^(\+?32|0)4?\d{8}$/,
|
|
'nl-NL': /^(((\+|00)?31\(0\))|((\+|00)?31)|0)6{1}\d{8}$/,
|
|
'nn-NO': /^(\+?47)?[49]\d{7}$/,
|
|
'pl-PL': /^(\+?48)? ?[5-8]\d ?\d{3} ?\d{2} ?\d{2}$/,
|
|
'pt-BR': /(?=^(\+?5{2}\-?|0)[1-9]{2}\-?\d{4}\-?\d{4}$)(^(\+?5{2}\-?|0)[1-9]{2}\-?[6-9]{1}\d{3}\-?\d{4}$)|(^(\+?5{2}\-?|0)[1-9]{2}\-?9[6-9]{1}\d{3}\-?\d{4}$)/,
|
|
'pt-PT': /^(\+?351)?9[1236]\d{7}$/,
|
|
'ro-RO': /^(\+?4?0)\s?7\d{2}(\/|\s|\.|\-)?\d{3}(\s|\.|\-)?\d{3}$/,
|
|
'ru-RU': /^(\+?7|8)?9\d{9}$/,
|
|
'sl-SI': /^(\+386\s?|0)(\d{1}\s?\d{3}\s?\d{2}\s?\d{2}|\d{2}\s?\d{3}\s?\d{3})$/,
|
|
'sk-SK': /^(\+?421)? ?[1-9][0-9]{2} ?[0-9]{3} ?[0-9]{3}$/,
|
|
'sr-RS': /^(\+3816|06)[- \d]{5,9}$/,
|
|
'sv-SE': /^(\+?46|0)[\s\-]?7[\s\-]?[02369]([\s\-]?\d){7}$/,
|
|
'th-TH': /^(\+66|66|0)\d{9}$/,
|
|
'tr-TR': /^(\+?90|0)?5\d{9}$/,
|
|
'uk-UA': /^(\+?38|8)?0\d{9}$/,
|
|
'uz-UZ': /^(\+?998)?(6[125-79]|7[1-69]|88|9\d)\d{7}$/,
|
|
'vi-VN': /^(\+?84|0)((3([2-9]))|(5([2689]))|(7([0|6-9]))|(8([1-6|89]))|(9([0-9])))([0-9]{7})$/,
|
|
'zh-CN': /^((\+|00)86)?1([3568][0-9]|4[579]|6[67]|7[01235678]|9[012356789])[0-9]{8}$/,
|
|
'zh-TW': /^(\+?886\-?|0)?9\d{8}$/,
|
|
'en-CA': /^$/,
|
|
'fr-BE': /^$/,
|
|
'zh-HK': /^$/,
|
|
'zh-MO': /^$/,
|
|
};
|
|
|
|
// aliases
|
|
phones['en-CA'] = phones['en-US'];
|
|
phones['fr-BE'] = phones['nl-BE'];
|
|
phones['zh-HK'] = phones['en-HK'];
|
|
phones['zh-MO'] = phones['en-MO'];
|
|
|
|
type MobilePhoneOptions = {
|
|
strictMode?: boolean;
|
|
};
|
|
|
|
export const isMobilePhone = (
|
|
str: string,
|
|
locale: PhoneLocale,
|
|
options?: MobilePhoneOptions
|
|
) => {
|
|
assertString(str);
|
|
if (options && options.strictMode && !str.startsWith('+')) {
|
|
return false;
|
|
}
|
|
if (Array.isArray(locale)) {
|
|
return locale.some((key) => {
|
|
// https://github.com/gotwarlost/istanbul/blob/master/ignoring-code-for-coverage.md#ignoring-code-for-coverage-purposes
|
|
// istanbul ignore else
|
|
if (phones.hasOwnProperty(key)) {
|
|
// @ts-ignore allowing typedoc to build
|
|
const phone = phones[key];
|
|
if (phone.test(str)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
});
|
|
} else if (locale in phones) {
|
|
// @ts-ignore allowing typedoc to build
|
|
return phones[locale].test(str);
|
|
// alias falsey locale as 'any'
|
|
} else if (!locale || locale === 'any') {
|
|
for (const key in phones) {
|
|
// istanbul ignore else
|
|
if (phones.hasOwnProperty(key)) {
|
|
// @ts-ignore allowing typedoc to build
|
|
const phone = phones[key];
|
|
if (phone.test(str)) {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
throw new Error(`Invalid locale '${locale}'`);
|
|
};
|
|
|
|
export const mobileLocales = Object.keys(phones);
|