Comparing version 1.3.2 to 1.4.0
@@ -7,2 +7,196 @@ 'use strict'; | ||
// https://github.com/validatorjs/validator.js/blob/master/src/lib/isMobilePhone.js | ||
const phones = { | ||
'zh-CN': /^((\+|00)86)?(1[3-9]|9[28])\d{9}$/, | ||
'zh-TW': /^(\+?886\-?|0)?9\d{8}$/, | ||
'am-AM': /^(\+?374|0)(33|4[134]|55|77|88|9[13-689])\d{6}$/, | ||
'ar-AE': /^((\+?971)|0)?5[024568]\d{7}$/, | ||
'ar-BH': /^(\+?973)?(3|6)\d{7}$/, | ||
'ar-DZ': /^(\+?213|0)(5|6|7)\d{8}$/, | ||
'ar-LB': /^(\+?961)?((3|81)\d{6}|7\d{7})$/, | ||
'ar-EG': /^((\+?20)|0)?1[0125]\d{8}$/, | ||
'ar-IQ': /^(\+?964|0)?7[0-9]\d{8}$/, | ||
'ar-JO': /^(\+?962|0)?7[789]\d{7}$/, | ||
'ar-KW': /^(\+?965)([569]\d{7}|41\d{6})$/, | ||
'ar-LY': /^((\+?218)|0)?(9[1-6]\d{7}|[1-8]\d{7,9})$/, | ||
'ar-MA': /^(?:(?:\+|00)212|0)[5-7]\d{8}$/, | ||
'ar-OM': /^((\+|00)968)?(9[1-9])\d{6}$/, | ||
'ar-PS': /^(\+?970|0)5[6|9](\d{7})$/, | ||
'ar-SA': /^(!?(\+?966)|0)?5\d{8}$/, | ||
'ar-SD': /^((\+?249)|0)?(9[012369]|1[012])\d{7}$/, | ||
'ar-SY': /^(!?(\+?963)|0)?9\d{8}$/, | ||
'ar-TN': /^(\+?216)?[2459]\d{7}$/, | ||
'az-AZ': /^(\+994|0)(10|5[015]|7[07]|99)\d{7}$/, | ||
'bs-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}$/, | ||
'ca-AD': /^(\+376)?[346]\d{5}$/, | ||
'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)(5[0-25-9]\d|6([23]|0\d?)|7([0-57-9]|6\d))\d{7,9}$/, | ||
'de-AT': /^(\+43|0)\d{1,4}\d{3,12}$/, | ||
'de-CH': /^(\+41|0)([1-9])\d{1,9}$/, | ||
'de-LU': /^(\+352)?((6\d1)\d{6})$/, | ||
'dv-MV': /^(\+?960)?(7[2-9]|9[1-9])\d{5}$/, | ||
'el-GR': /^(\+?30|0)?6(8[5-9]|9(?![26])[0-9])\d{7}$/, | ||
'el-CY': /^(\+?357?)?(9(9|6)\d{6})$/, | ||
'en-AI': | ||
/^(\+?1|0)264(?:2(35|92)|4(?:6[1-2]|76|97)|5(?:3[6-9]|8[1-4])|7(?:2(4|9)|72))\d{4}$/, | ||
'en-AU': /^(\+?61|0)4\d{8}$/, | ||
'en-AG': /^(?:\+1|1)268(?:464|7(?:1[3-9]|[28]\d|3[0246]|64|7[0-689]))\d{4}$/, | ||
'en-BM': /^(\+?1)?441(((3|7)\d{6}$)|(5[0-3][0-9]\d{4}$)|(59\d{5}$))/, | ||
'en-BS': /^(\+?1[-\s]?|0)?\(?242\)?[-\s]?\d{3}[-\s]?\d{4}$/, | ||
'en-GB': /^(\+?44|0)7[1-9]\d{8}$/, | ||
'en-GG': /^(\+?44|0)1481\d{6}$/, | ||
'en-GH': /^(\+233|0)(20|50|24|54|27|57|26|56|23|28|55|59)\d{7}$/, | ||
'en-GY': /^(\+592|0)6\d{6}$/, | ||
'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-JM': /^(\+?876)?\d{7}$/, | ||
'en-KE': /^(\+?254|0)(7|1)\d{8}$/, | ||
'fr-CF': /^(\+?236| ?)(70|75|77|72|21|22)\d{6}$/, | ||
'en-SS': /^(\+?211|0)(9[1257])\d{7}$/, | ||
'en-KI': /^((\+686|686)?)?( )?((6|7)(2|3|8)[0-9]{6})$/, | ||
'en-KN': /^(?:\+1|1)869(?:46\d|48[89]|55[6-8]|66\d|76[02-7])\d{4}$/, | ||
'en-LS': /^(\+?266)(22|28|57|58|59|27|52)\d{6}$/, | ||
'en-MT': /^(\+?356|0)?(99|79|77|21|27|22|25)[0-9]{6}$/, | ||
'en-MU': /^(\+?230|0)?\d{8}$/, | ||
'en-MW': | ||
/^(\+?265|0)(((77|88|31|99|98|21)\d{7})|(((111)|1)\d{6})|(32000\d{4}))$/, | ||
'en-NA': /^(\+?264|0)(6|8)\d{7}$/, | ||
'en-NG': /^(\+?234|0)?[789]\d{9}$/, | ||
'en-NZ': /^(\+?64|0)[28]\d{7,9}$/, | ||
'en-PG': /^(\+?675|0)?(7\d|8[18])\d{6}$/, | ||
'en-PK': /^((00|\+)?92|0)3[0-6]\d{8}$/, | ||
'en-PH': /^(09|\+639)\d{9}$/, | ||
'en-RW': /^(\+?250|0)?[7]\d{8}$/, | ||
'en-SG': /^(\+65)?[3689]\d{7}$/, | ||
'en-SL': /^(\+?232|0)\d{8}$/, | ||
'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}$/, | ||
'en-BW': /^(\+?267)?(7[1-8]{1})\d{6}$/, | ||
'es-AR': /^\+?549(11|[2368]\d)\d{8}$/, | ||
'es-BO': /^(\+?591)?(6|7)\d{7}$/, | ||
'es-CO': /^(\+?57)?3(0(0|1|2|4|5)|1\d|2[0-4]|5(0|1))\d{7}$/, | ||
'es-CL': /^(\+?56|0)[2-9]\d{1}\d{7}$/, | ||
'es-CR': /^(\+506)?[2-8]\d{7}$/, | ||
'es-CU': /^(\+53|0053)?5\d{7}$/, | ||
'es-DO': /^(\+?1)?8[024]9\d{7}$/, | ||
'es-HN': /^(\+?504)?[9|8|3|2]\d{7}$/, | ||
'es-EC': /^(\+?593|0)([2-7]|9[2-9])\d{7}$/, | ||
'es-ES': /^(\+?34)?[6|7]\d{8}$/, | ||
'es-GT': /^(\+?502)?[2|6|7]\d{7}$/, | ||
'es-PE': /^(\+?51)?9\d{8}$/, | ||
'es-MX': /^(\+?52)?(1|01)?\d{10,11}$/, | ||
'es-NI': /^(\+?505)\d{7,8}$/, | ||
'es-PA': /^(\+?507)\d{7,8}$/, | ||
'es-PY': /^(\+?595|0)9[9876]\d{7}$/, | ||
'es-SV': /^(\+?503)?[67]\d{7}$/, | ||
'es-UY': /^(\+598|0)9[1-9][\d]{6}$/, | ||
'es-VE': /^(\+?58)?(2|4)\d{9}$/, | ||
'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-6]|50)\s?(\d\s?){4,8}$/, | ||
'fj-FJ': /^(\+?679)?\s?\d{3}\s?\d{4}$/, | ||
'fo-FO': /^(\+?298)?\s?\d{2}\s?\d{2}\s?\d{2}$/, | ||
'fr-BF': /^(\+226|0)[67]\d{7}$/, | ||
'fr-BJ': /^(\+229)\d{8}$/, | ||
'fr-CD': /^(\+?243|0)?(8|9)\d{8}$/, | ||
'fr-CM': /^(\+?237)6[0-9]{8}$/, | ||
'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-PF': /^(\+?689)?8[789]\d{6}$/, | ||
'fr-RE': /^(\+?262|0|00262)[67]\d{8}$/, | ||
'fr-WF': /^(\+681)?\d{6}$/, | ||
'he-IL': /^(\+972|0)([23489]|5[012345689]|77)[1-9]\d{6}$/, | ||
'hu-HU': /^(\+?36|06)(20|30|31|50|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})$/, | ||
'ir-IR': /^(\+98|0)?9\d{9}$/, | ||
'it-IT': /^(\+?39)?\s?3\d{2} ?\d{6,7}$/, | ||
'it-SM': /^((\+378)|(0549)|(\+390549)|(\+3780549))?6\d{5,9}$/, | ||
'ja-JP': /^(\+81[ \-]?(\(0\))?|0)[6789]0[ \-]?\d{4}[ \-]?\d{4}$/, | ||
'ka-GE': /^(\+?995)?(79\d{7}|5\d{8})$/, | ||
'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}$/, | ||
'ky-KG': /^(\+?7\s?\+?7|0)\s?\d{2}\s?\d{3}\s?\d{4}$/, | ||
'lt-LT': /^(\+370|8)\d{8}$/, | ||
'lv-LV': /^(\+?371)2\d{7}$/, | ||
'mg-MG': /^((\+?261|0)(2|3)\d)?\d{7}$/, | ||
'mn-MN': /^(\+|00|011)?976(77|81|88|91|94|95|96|99)\d{6}$/, | ||
'my-MM': /^(\+?959|09|9)(2[5-7]|3[1-2]|4[0-5]|6[6-9]|7[5-9]|9[6-9])[0-9]{7}$/, | ||
'ms-MY': /^(\+?60|0)1(([0145](-|\s)?\d{7,8})|([236-9](-|\s)?\d{7}))$/, | ||
'mz-MZ': /^(\+?258)?8[234567]\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}$/, | ||
'nl-AW': /^(\+)?297(56|59|64|73|74|99)\d{5}$/, | ||
'nn-NO': /^(\+?47)?[49]\d{7}$/, | ||
'pl-PL': /^(\+?48)? ?([5-8]\d|45) ?\d{3} ?\d{2} ?\d{2}$/, | ||
'pt-BR': | ||
/^((\+?55\ ?[1-9]{2}\ ?)|(\+?55\ ?\([1-9]{2}\)\ ?)|(0[1-9]{2}\ ?)|(\([1-9]{2}\)\ ?)|([1-9]{2}\ ?))((\d{4}\-?\d{4})|(9[1-9]{1}\d{3}\-?\d{4}))$/, | ||
'pt-PT': /^(\+?351)?9[1236]\d{7}$/, | ||
'pt-AO': /^(\+244)\d{9}$/, | ||
'ro-MD': /^(\+?373|0)((6(0|1|2|6|7|8|9))|(7(6|7|8|9)))\d{6}$/, | ||
'ro-RO': /^(\+?40|0)\s?7\d{2}(\/|\s|\.|-)?\d{3}(\s|\.|-)?\d{3}$/, | ||
'ru-RU': /^(\+?7|8)?9\d{9}$/, | ||
'si-LK': /^(?:0|94|\+94)?(7(0|1|2|4|5|6|7|8)( |-)?)\d{7}$/, | ||
'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}$/, | ||
'so-SO': /^(\+?252|0)((6[0-9])\d{7}|(7[1-9])\d{7})$/, | ||
'sq-AL': /^(\+355|0)6[789]\d{6}$/, | ||
'sr-RS': /^(\+3816|06)[- \d]{5,9}$/, | ||
'sv-SE': /^(\+?46|0)[\s\-]?7[\s\-]?[02369]([\s\-]?\d){7}$/, | ||
'tg-TJ': /^(\+?992)?[5][5]\d{7}$/, | ||
'th-TH': /^(\+66|66|0)\d{9}$/, | ||
'tr-TR': /^(\+?90|0)?5\d{9}$/, | ||
'tk-TM': /^(\+993|993|8)\d{8}$/, | ||
'uk-UA': /^(\+?38)?0(50|6[36-8]|7[357]|9[1-9])\d{7}$/, | ||
'uz-UZ': /^(\+?998)?(6[125-79]|7[1-69]|88|9\d)\d{7}$/, | ||
'vi-VN': | ||
/^((\+?84)|0)((3([2-9]))|(5([25689]))|(7([0|6-9]))|(8([1-9]))|(9([0-9])))([0-9]{7})$/, | ||
'dz-BT': /^(\+?975|0)?(17|16|77|02)\d{6}$/, | ||
'ar-YE': /^(((\+|00)9677|0?7)[0137]\d{7}|((\+|00)967|0)[1-7]\d{6})$/, | ||
'ar-EH': /^(\+?212|0)[\s\-]?(5288|5289)[\s\-]?\d{5}$/, | ||
'fa-AF': /^(\+93|0)?(2{1}[0-8]{1}|[3-5]{1}[0-4]{1})(\d{7})$/, | ||
// aliases | ||
get 'en-CA'() { | ||
return phones['en-US']; | ||
}, | ||
get 'fr-CA'() { | ||
return phones['en-CA']; | ||
}, | ||
get 'fr-BE'() { | ||
return phones['nl-BE']; | ||
}, | ||
get 'zh-HK'() { | ||
return phones['en-HK']; | ||
}, | ||
get 'zh-MO'() { | ||
return phones['en-MO']; | ||
}, | ||
get 'ga-IE'() { | ||
return phones['en-IE']; | ||
}, | ||
get 'fr-CH'() { | ||
return phones['de-CH']; | ||
}, | ||
get 'it-CH'() { | ||
return phones['fr-CH']; | ||
}, | ||
}; | ||
const objectToString = Object.prototype.toString; | ||
@@ -44,2 +238,3 @@ const supportWasm = typeof WebAssembly === 'object'; | ||
const isPromise = (v) => isObject(v) && typeof v.then === 'function'; | ||
const isPort = (n) => Number.isInteger(n) && n >= 0 && n <= 65535; | ||
const isBuffer = (v) => { | ||
@@ -72,2 +267,135 @@ if (!isObject(v)) return false; | ||
}; | ||
const isByteLength = (val, options = {}) => { | ||
const min = options.min || 0; | ||
const max = options.max; | ||
const len = encodeURI(val).split(/%..|./).length - 1; | ||
return len >= min && (typeof max === 'undefined' || len <= max); | ||
}; | ||
const notBase64 = /[^A-Z0-9+\/=]/i; | ||
const urlSafeBase64 = /^[A-Z0-9_\-]*$/i; | ||
const isBase64 = (val, urlSafe = false) => { | ||
const len = val.length; | ||
if (urlSafe) return urlSafeBase64.test(val); | ||
if (len % 4 !== 0 || notBase64.test(val)) return false; | ||
const firstPaddingChar = val.indexOf('='); | ||
return ( | ||
firstPaddingChar === -1 || | ||
firstPaddingChar === len - 1 || | ||
(firstPaddingChar === len - 2 && val[len - 1] === '=') | ||
); | ||
}; | ||
const IPv4SegmentFormat = | ||
'(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'; | ||
const IPv4AddressFormat = `(${IPv4SegmentFormat}[.]){3}${IPv4SegmentFormat}`; | ||
const IPv4AddressRegExp = new RegExp(`^${IPv4AddressFormat}$`); | ||
const IPv6SegmentFormat = '(?:[0-9a-fA-F]{1,4})'; | ||
const IPv6AddressRegExp = new RegExp( | ||
'^(' + | ||
`(?:${IPv6SegmentFormat}:){7}(?:${IPv6SegmentFormat}|:)|` + | ||
`(?:${IPv6SegmentFormat}:){6}(?:${IPv4AddressFormat}|:${IPv6SegmentFormat}|:)|` + | ||
`(?:${IPv6SegmentFormat}:){5}(?::${IPv4AddressFormat}|(:${IPv6SegmentFormat}){1,2}|:)|` + | ||
`(?:${IPv6SegmentFormat}:){4}(?:(:${IPv6SegmentFormat}){0,1}:${IPv4AddressFormat}|(:${IPv6SegmentFormat}){1,3}|:)|` + | ||
`(?:${IPv6SegmentFormat}:){3}(?:(:${IPv6SegmentFormat}){0,2}:${IPv4AddressFormat}|(:${IPv6SegmentFormat}){1,4}|:)|` + | ||
`(?:${IPv6SegmentFormat}:){2}(?:(:${IPv6SegmentFormat}){0,3}:${IPv4AddressFormat}|(:${IPv6SegmentFormat}){1,5}|:)|` + | ||
`(?:${IPv6SegmentFormat}:){1}(?:(:${IPv6SegmentFormat}){0,4}:${IPv4AddressFormat}|(:${IPv6SegmentFormat}){1,6}|:)|` + | ||
`(?::((?::${IPv6SegmentFormat}){0,5}:${IPv4AddressFormat}|(?::${IPv6SegmentFormat}){1,7}|:))` + | ||
')(%[0-9a-zA-Z-.:]{1,})?$', | ||
); | ||
// https://github.com/validatorjs/validator.js/blob/master/src/lib/isIP.js | ||
const isIP = (val, version) => { | ||
if (!version) return isIP(val, '4') || isIP(val, '6'); | ||
if (version === '4') return IPv4AddressRegExp.test(val); | ||
if (version === '6') return IPv6AddressRegExp.test(val); | ||
return false; | ||
}; | ||
const isDomain = (val) => { | ||
const parts = val.split('.'); | ||
if (parts.length < 2) return false; | ||
const tld = parts[parts.length - 1]; | ||
const reg = | ||
/^([a-z\u00A1-\u00A8\u00AA-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]{2,}|xn[a-z0-9-]{2,})$/i; | ||
if (!reg.test(tld)) return false; | ||
if (/\s/.test(tld)) return false; | ||
if (/^\d+$/.test(tld)) return false; | ||
return parts.every((part) => { | ||
if (part.length > 63) return false; | ||
if (!/^[a-z_\u00a1-\uffff0-9-]+$/i.test(part)) return false; | ||
if (/[\uff01-\uff5e]/.test(part)) return false; | ||
if (/^-|-$/.test(part)) return false; | ||
if (/_/.test(part)) return false; | ||
return true; | ||
}); | ||
}; | ||
const isPhone = (val, locale, strictMode) => { | ||
if (strictMode && !val.startsWith('+')) { | ||
return false; | ||
} | ||
if (typeof locale === 'string' && locale in phones) { | ||
return phones[locale].test(val); | ||
} | ||
if (isArray(locale)) { | ||
return locale.some((key) => { | ||
if (phones.hasOwnProperty(key)) { | ||
if (phones[key].test(val)) return true; | ||
} | ||
return false; | ||
}); | ||
} | ||
if (!locale) { | ||
for (const key in phones) { | ||
if (phones.hasOwnProperty(key)) { | ||
const phone = phones[key]; | ||
if (phone.test(val)) return true; | ||
} | ||
} | ||
return false; | ||
} | ||
throw new Error(`Invalid locale '${locale}'`); | ||
}; | ||
const isCNPhone = (val, strictMode) => isPhone(val, 'zh-CN', strictMode); | ||
const emailUserUtf8Part = | ||
/^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~\u00A1-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+$/i; | ||
const quotedEmailUserUtf8 = | ||
/^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*$/i; | ||
/** | ||
* Not verify `display_name` | ||
* */ | ||
const isEmail = (val, options) => { | ||
// If greater than default max email length | ||
if (val.length > 254) return false; | ||
const parts = val.split('@'); | ||
const domain = parts.pop(); | ||
if (!domain) return false; | ||
let user = parts.join('@'); | ||
if (!isByteLength(user, { max: 64 }) || !isByteLength(domain, { max: 254 })) { | ||
return false; | ||
} | ||
if (!isDomain(domain)) { | ||
if (options && options.disableIPDomain) { | ||
return false; | ||
} | ||
// user@[192.168.1.1] | ||
// user@[2001:0db8:85a3:0000:0000:8a2e:0370:7334] | ||
if (!isIP(domain)) { | ||
if (!domain.startsWith('[') || !domain.endsWith(']')) { | ||
return false; | ||
} | ||
const noBracketdomain = domain.slice(1, -1); | ||
if (noBracketdomain.length === 0 || !isIP(noBracketdomain)) { | ||
return false; | ||
} | ||
} | ||
} | ||
if (user[0] === '"') { | ||
user = user.slice(1, user.length - 1); | ||
return quotedEmailUserUtf8.test(user); | ||
} | ||
const userParts = user.split('.'); | ||
for (let i = 0; i < userParts.length; i++) { | ||
if (!emailUserUtf8Part.test(userParts[i])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; | ||
@@ -1238,7 +1566,13 @@ const freeSelf = | ||
exports.isArray = isArray; | ||
exports.isBase64 = isBase64; | ||
exports.isBrowser = isBrowser; | ||
exports.isBuffer = isBuffer; | ||
exports.isByteLength = isByteLength; | ||
exports.isCNPhone = isCNPhone; | ||
exports.isDate = isDate; | ||
exports.isDomain = isDomain; | ||
exports.isEmail = isEmail; | ||
exports.isEmptyObject = isEmptyObject; | ||
exports.isFunction = isFunction; | ||
exports.isIP = isIP; | ||
exports.isInBounds = isInBounds; | ||
@@ -1249,3 +1583,5 @@ exports.isMap = isMap; | ||
exports.isObject = isObject; | ||
exports.isPhone = isPhone; | ||
exports.isPlainObject = isPlainObject; | ||
exports.isPort = isPort; | ||
exports.isPrimitiveValue = isPrimitiveValue; | ||
@@ -1252,0 +1588,0 @@ exports.isPromise = isPromise; |
export { Queue } from 'small-queue'; | ||
// https://github.com/validatorjs/validator.js/blob/master/src/lib/isMobilePhone.js | ||
const phones = { | ||
'zh-CN': /^((\+|00)86)?(1[3-9]|9[28])\d{9}$/, | ||
'zh-TW': /^(\+?886\-?|0)?9\d{8}$/, | ||
'am-AM': /^(\+?374|0)(33|4[134]|55|77|88|9[13-689])\d{6}$/, | ||
'ar-AE': /^((\+?971)|0)?5[024568]\d{7}$/, | ||
'ar-BH': /^(\+?973)?(3|6)\d{7}$/, | ||
'ar-DZ': /^(\+?213|0)(5|6|7)\d{8}$/, | ||
'ar-LB': /^(\+?961)?((3|81)\d{6}|7\d{7})$/, | ||
'ar-EG': /^((\+?20)|0)?1[0125]\d{8}$/, | ||
'ar-IQ': /^(\+?964|0)?7[0-9]\d{8}$/, | ||
'ar-JO': /^(\+?962|0)?7[789]\d{7}$/, | ||
'ar-KW': /^(\+?965)([569]\d{7}|41\d{6})$/, | ||
'ar-LY': /^((\+?218)|0)?(9[1-6]\d{7}|[1-8]\d{7,9})$/, | ||
'ar-MA': /^(?:(?:\+|00)212|0)[5-7]\d{8}$/, | ||
'ar-OM': /^((\+|00)968)?(9[1-9])\d{6}$/, | ||
'ar-PS': /^(\+?970|0)5[6|9](\d{7})$/, | ||
'ar-SA': /^(!?(\+?966)|0)?5\d{8}$/, | ||
'ar-SD': /^((\+?249)|0)?(9[012369]|1[012])\d{7}$/, | ||
'ar-SY': /^(!?(\+?963)|0)?9\d{8}$/, | ||
'ar-TN': /^(\+?216)?[2459]\d{7}$/, | ||
'az-AZ': /^(\+994|0)(10|5[015]|7[07]|99)\d{7}$/, | ||
'bs-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}$/, | ||
'ca-AD': /^(\+376)?[346]\d{5}$/, | ||
'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)(5[0-25-9]\d|6([23]|0\d?)|7([0-57-9]|6\d))\d{7,9}$/, | ||
'de-AT': /^(\+43|0)\d{1,4}\d{3,12}$/, | ||
'de-CH': /^(\+41|0)([1-9])\d{1,9}$/, | ||
'de-LU': /^(\+352)?((6\d1)\d{6})$/, | ||
'dv-MV': /^(\+?960)?(7[2-9]|9[1-9])\d{5}$/, | ||
'el-GR': /^(\+?30|0)?6(8[5-9]|9(?![26])[0-9])\d{7}$/, | ||
'el-CY': /^(\+?357?)?(9(9|6)\d{6})$/, | ||
'en-AI': | ||
/^(\+?1|0)264(?:2(35|92)|4(?:6[1-2]|76|97)|5(?:3[6-9]|8[1-4])|7(?:2(4|9)|72))\d{4}$/, | ||
'en-AU': /^(\+?61|0)4\d{8}$/, | ||
'en-AG': /^(?:\+1|1)268(?:464|7(?:1[3-9]|[28]\d|3[0246]|64|7[0-689]))\d{4}$/, | ||
'en-BM': /^(\+?1)?441(((3|7)\d{6}$)|(5[0-3][0-9]\d{4}$)|(59\d{5}$))/, | ||
'en-BS': /^(\+?1[-\s]?|0)?\(?242\)?[-\s]?\d{3}[-\s]?\d{4}$/, | ||
'en-GB': /^(\+?44|0)7[1-9]\d{8}$/, | ||
'en-GG': /^(\+?44|0)1481\d{6}$/, | ||
'en-GH': /^(\+233|0)(20|50|24|54|27|57|26|56|23|28|55|59)\d{7}$/, | ||
'en-GY': /^(\+592|0)6\d{6}$/, | ||
'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-JM': /^(\+?876)?\d{7}$/, | ||
'en-KE': /^(\+?254|0)(7|1)\d{8}$/, | ||
'fr-CF': /^(\+?236| ?)(70|75|77|72|21|22)\d{6}$/, | ||
'en-SS': /^(\+?211|0)(9[1257])\d{7}$/, | ||
'en-KI': /^((\+686|686)?)?( )?((6|7)(2|3|8)[0-9]{6})$/, | ||
'en-KN': /^(?:\+1|1)869(?:46\d|48[89]|55[6-8]|66\d|76[02-7])\d{4}$/, | ||
'en-LS': /^(\+?266)(22|28|57|58|59|27|52)\d{6}$/, | ||
'en-MT': /^(\+?356|0)?(99|79|77|21|27|22|25)[0-9]{6}$/, | ||
'en-MU': /^(\+?230|0)?\d{8}$/, | ||
'en-MW': | ||
/^(\+?265|0)(((77|88|31|99|98|21)\d{7})|(((111)|1)\d{6})|(32000\d{4}))$/, | ||
'en-NA': /^(\+?264|0)(6|8)\d{7}$/, | ||
'en-NG': /^(\+?234|0)?[789]\d{9}$/, | ||
'en-NZ': /^(\+?64|0)[28]\d{7,9}$/, | ||
'en-PG': /^(\+?675|0)?(7\d|8[18])\d{6}$/, | ||
'en-PK': /^((00|\+)?92|0)3[0-6]\d{8}$/, | ||
'en-PH': /^(09|\+639)\d{9}$/, | ||
'en-RW': /^(\+?250|0)?[7]\d{8}$/, | ||
'en-SG': /^(\+65)?[3689]\d{7}$/, | ||
'en-SL': /^(\+?232|0)\d{8}$/, | ||
'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}$/, | ||
'en-BW': /^(\+?267)?(7[1-8]{1})\d{6}$/, | ||
'es-AR': /^\+?549(11|[2368]\d)\d{8}$/, | ||
'es-BO': /^(\+?591)?(6|7)\d{7}$/, | ||
'es-CO': /^(\+?57)?3(0(0|1|2|4|5)|1\d|2[0-4]|5(0|1))\d{7}$/, | ||
'es-CL': /^(\+?56|0)[2-9]\d{1}\d{7}$/, | ||
'es-CR': /^(\+506)?[2-8]\d{7}$/, | ||
'es-CU': /^(\+53|0053)?5\d{7}$/, | ||
'es-DO': /^(\+?1)?8[024]9\d{7}$/, | ||
'es-HN': /^(\+?504)?[9|8|3|2]\d{7}$/, | ||
'es-EC': /^(\+?593|0)([2-7]|9[2-9])\d{7}$/, | ||
'es-ES': /^(\+?34)?[6|7]\d{8}$/, | ||
'es-GT': /^(\+?502)?[2|6|7]\d{7}$/, | ||
'es-PE': /^(\+?51)?9\d{8}$/, | ||
'es-MX': /^(\+?52)?(1|01)?\d{10,11}$/, | ||
'es-NI': /^(\+?505)\d{7,8}$/, | ||
'es-PA': /^(\+?507)\d{7,8}$/, | ||
'es-PY': /^(\+?595|0)9[9876]\d{7}$/, | ||
'es-SV': /^(\+?503)?[67]\d{7}$/, | ||
'es-UY': /^(\+598|0)9[1-9][\d]{6}$/, | ||
'es-VE': /^(\+?58)?(2|4)\d{9}$/, | ||
'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-6]|50)\s?(\d\s?){4,8}$/, | ||
'fj-FJ': /^(\+?679)?\s?\d{3}\s?\d{4}$/, | ||
'fo-FO': /^(\+?298)?\s?\d{2}\s?\d{2}\s?\d{2}$/, | ||
'fr-BF': /^(\+226|0)[67]\d{7}$/, | ||
'fr-BJ': /^(\+229)\d{8}$/, | ||
'fr-CD': /^(\+?243|0)?(8|9)\d{8}$/, | ||
'fr-CM': /^(\+?237)6[0-9]{8}$/, | ||
'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-PF': /^(\+?689)?8[789]\d{6}$/, | ||
'fr-RE': /^(\+?262|0|00262)[67]\d{8}$/, | ||
'fr-WF': /^(\+681)?\d{6}$/, | ||
'he-IL': /^(\+972|0)([23489]|5[012345689]|77)[1-9]\d{6}$/, | ||
'hu-HU': /^(\+?36|06)(20|30|31|50|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})$/, | ||
'ir-IR': /^(\+98|0)?9\d{9}$/, | ||
'it-IT': /^(\+?39)?\s?3\d{2} ?\d{6,7}$/, | ||
'it-SM': /^((\+378)|(0549)|(\+390549)|(\+3780549))?6\d{5,9}$/, | ||
'ja-JP': /^(\+81[ \-]?(\(0\))?|0)[6789]0[ \-]?\d{4}[ \-]?\d{4}$/, | ||
'ka-GE': /^(\+?995)?(79\d{7}|5\d{8})$/, | ||
'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}$/, | ||
'ky-KG': /^(\+?7\s?\+?7|0)\s?\d{2}\s?\d{3}\s?\d{4}$/, | ||
'lt-LT': /^(\+370|8)\d{8}$/, | ||
'lv-LV': /^(\+?371)2\d{7}$/, | ||
'mg-MG': /^((\+?261|0)(2|3)\d)?\d{7}$/, | ||
'mn-MN': /^(\+|00|011)?976(77|81|88|91|94|95|96|99)\d{6}$/, | ||
'my-MM': /^(\+?959|09|9)(2[5-7]|3[1-2]|4[0-5]|6[6-9]|7[5-9]|9[6-9])[0-9]{7}$/, | ||
'ms-MY': /^(\+?60|0)1(([0145](-|\s)?\d{7,8})|([236-9](-|\s)?\d{7}))$/, | ||
'mz-MZ': /^(\+?258)?8[234567]\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}$/, | ||
'nl-AW': /^(\+)?297(56|59|64|73|74|99)\d{5}$/, | ||
'nn-NO': /^(\+?47)?[49]\d{7}$/, | ||
'pl-PL': /^(\+?48)? ?([5-8]\d|45) ?\d{3} ?\d{2} ?\d{2}$/, | ||
'pt-BR': | ||
/^((\+?55\ ?[1-9]{2}\ ?)|(\+?55\ ?\([1-9]{2}\)\ ?)|(0[1-9]{2}\ ?)|(\([1-9]{2}\)\ ?)|([1-9]{2}\ ?))((\d{4}\-?\d{4})|(9[1-9]{1}\d{3}\-?\d{4}))$/, | ||
'pt-PT': /^(\+?351)?9[1236]\d{7}$/, | ||
'pt-AO': /^(\+244)\d{9}$/, | ||
'ro-MD': /^(\+?373|0)((6(0|1|2|6|7|8|9))|(7(6|7|8|9)))\d{6}$/, | ||
'ro-RO': /^(\+?40|0)\s?7\d{2}(\/|\s|\.|-)?\d{3}(\s|\.|-)?\d{3}$/, | ||
'ru-RU': /^(\+?7|8)?9\d{9}$/, | ||
'si-LK': /^(?:0|94|\+94)?(7(0|1|2|4|5|6|7|8)( |-)?)\d{7}$/, | ||
'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}$/, | ||
'so-SO': /^(\+?252|0)((6[0-9])\d{7}|(7[1-9])\d{7})$/, | ||
'sq-AL': /^(\+355|0)6[789]\d{6}$/, | ||
'sr-RS': /^(\+3816|06)[- \d]{5,9}$/, | ||
'sv-SE': /^(\+?46|0)[\s\-]?7[\s\-]?[02369]([\s\-]?\d){7}$/, | ||
'tg-TJ': /^(\+?992)?[5][5]\d{7}$/, | ||
'th-TH': /^(\+66|66|0)\d{9}$/, | ||
'tr-TR': /^(\+?90|0)?5\d{9}$/, | ||
'tk-TM': /^(\+993|993|8)\d{8}$/, | ||
'uk-UA': /^(\+?38)?0(50|6[36-8]|7[357]|9[1-9])\d{7}$/, | ||
'uz-UZ': /^(\+?998)?(6[125-79]|7[1-69]|88|9\d)\d{7}$/, | ||
'vi-VN': | ||
/^((\+?84)|0)((3([2-9]))|(5([25689]))|(7([0|6-9]))|(8([1-9]))|(9([0-9])))([0-9]{7})$/, | ||
'dz-BT': /^(\+?975|0)?(17|16|77|02)\d{6}$/, | ||
'ar-YE': /^(((\+|00)9677|0?7)[0137]\d{7}|((\+|00)967|0)[1-7]\d{6})$/, | ||
'ar-EH': /^(\+?212|0)[\s\-]?(5288|5289)[\s\-]?\d{5}$/, | ||
'fa-AF': /^(\+93|0)?(2{1}[0-8]{1}|[3-5]{1}[0-4]{1})(\d{7})$/, | ||
// aliases | ||
get 'en-CA'() { | ||
return phones['en-US']; | ||
}, | ||
get 'fr-CA'() { | ||
return phones['en-CA']; | ||
}, | ||
get 'fr-BE'() { | ||
return phones['nl-BE']; | ||
}, | ||
get 'zh-HK'() { | ||
return phones['en-HK']; | ||
}, | ||
get 'zh-MO'() { | ||
return phones['en-MO']; | ||
}, | ||
get 'ga-IE'() { | ||
return phones['en-IE']; | ||
}, | ||
get 'fr-CH'() { | ||
return phones['de-CH']; | ||
}, | ||
get 'it-CH'() { | ||
return phones['fr-CH']; | ||
}, | ||
}; | ||
const objectToString = Object.prototype.toString; | ||
@@ -39,2 +233,3 @@ const supportWasm = typeof WebAssembly === 'object'; | ||
const isPromise = (v) => isObject(v) && typeof v.then === 'function'; | ||
const isPort = (n) => Number.isInteger(n) && n >= 0 && n <= 65535; | ||
const isBuffer = (v) => { | ||
@@ -67,2 +262,135 @@ if (!isObject(v)) return false; | ||
}; | ||
const isByteLength = (val, options = {}) => { | ||
const min = options.min || 0; | ||
const max = options.max; | ||
const len = encodeURI(val).split(/%..|./).length - 1; | ||
return len >= min && (typeof max === 'undefined' || len <= max); | ||
}; | ||
const notBase64 = /[^A-Z0-9+\/=]/i; | ||
const urlSafeBase64 = /^[A-Z0-9_\-]*$/i; | ||
const isBase64 = (val, urlSafe = false) => { | ||
const len = val.length; | ||
if (urlSafe) return urlSafeBase64.test(val); | ||
if (len % 4 !== 0 || notBase64.test(val)) return false; | ||
const firstPaddingChar = val.indexOf('='); | ||
return ( | ||
firstPaddingChar === -1 || | ||
firstPaddingChar === len - 1 || | ||
(firstPaddingChar === len - 2 && val[len - 1] === '=') | ||
); | ||
}; | ||
const IPv4SegmentFormat = | ||
'(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'; | ||
const IPv4AddressFormat = `(${IPv4SegmentFormat}[.]){3}${IPv4SegmentFormat}`; | ||
const IPv4AddressRegExp = new RegExp(`^${IPv4AddressFormat}$`); | ||
const IPv6SegmentFormat = '(?:[0-9a-fA-F]{1,4})'; | ||
const IPv6AddressRegExp = new RegExp( | ||
'^(' + | ||
`(?:${IPv6SegmentFormat}:){7}(?:${IPv6SegmentFormat}|:)|` + | ||
`(?:${IPv6SegmentFormat}:){6}(?:${IPv4AddressFormat}|:${IPv6SegmentFormat}|:)|` + | ||
`(?:${IPv6SegmentFormat}:){5}(?::${IPv4AddressFormat}|(:${IPv6SegmentFormat}){1,2}|:)|` + | ||
`(?:${IPv6SegmentFormat}:){4}(?:(:${IPv6SegmentFormat}){0,1}:${IPv4AddressFormat}|(:${IPv6SegmentFormat}){1,3}|:)|` + | ||
`(?:${IPv6SegmentFormat}:){3}(?:(:${IPv6SegmentFormat}){0,2}:${IPv4AddressFormat}|(:${IPv6SegmentFormat}){1,4}|:)|` + | ||
`(?:${IPv6SegmentFormat}:){2}(?:(:${IPv6SegmentFormat}){0,3}:${IPv4AddressFormat}|(:${IPv6SegmentFormat}){1,5}|:)|` + | ||
`(?:${IPv6SegmentFormat}:){1}(?:(:${IPv6SegmentFormat}){0,4}:${IPv4AddressFormat}|(:${IPv6SegmentFormat}){1,6}|:)|` + | ||
`(?::((?::${IPv6SegmentFormat}){0,5}:${IPv4AddressFormat}|(?::${IPv6SegmentFormat}){1,7}|:))` + | ||
')(%[0-9a-zA-Z-.:]{1,})?$', | ||
); | ||
// https://github.com/validatorjs/validator.js/blob/master/src/lib/isIP.js | ||
const isIP = (val, version) => { | ||
if (!version) return isIP(val, '4') || isIP(val, '6'); | ||
if (version === '4') return IPv4AddressRegExp.test(val); | ||
if (version === '6') return IPv6AddressRegExp.test(val); | ||
return false; | ||
}; | ||
const isDomain = (val) => { | ||
const parts = val.split('.'); | ||
if (parts.length < 2) return false; | ||
const tld = parts[parts.length - 1]; | ||
const reg = | ||
/^([a-z\u00A1-\u00A8\u00AA-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]{2,}|xn[a-z0-9-]{2,})$/i; | ||
if (!reg.test(tld)) return false; | ||
if (/\s/.test(tld)) return false; | ||
if (/^\d+$/.test(tld)) return false; | ||
return parts.every((part) => { | ||
if (part.length > 63) return false; | ||
if (!/^[a-z_\u00a1-\uffff0-9-]+$/i.test(part)) return false; | ||
if (/[\uff01-\uff5e]/.test(part)) return false; | ||
if (/^-|-$/.test(part)) return false; | ||
if (/_/.test(part)) return false; | ||
return true; | ||
}); | ||
}; | ||
const isPhone = (val, locale, strictMode) => { | ||
if (strictMode && !val.startsWith('+')) { | ||
return false; | ||
} | ||
if (typeof locale === 'string' && locale in phones) { | ||
return phones[locale].test(val); | ||
} | ||
if (isArray(locale)) { | ||
return locale.some((key) => { | ||
if (phones.hasOwnProperty(key)) { | ||
if (phones[key].test(val)) return true; | ||
} | ||
return false; | ||
}); | ||
} | ||
if (!locale) { | ||
for (const key in phones) { | ||
if (phones.hasOwnProperty(key)) { | ||
const phone = phones[key]; | ||
if (phone.test(val)) return true; | ||
} | ||
} | ||
return false; | ||
} | ||
throw new Error(`Invalid locale '${locale}'`); | ||
}; | ||
const isCNPhone = (val, strictMode) => isPhone(val, 'zh-CN', strictMode); | ||
const emailUserUtf8Part = | ||
/^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~\u00A1-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+$/i; | ||
const quotedEmailUserUtf8 = | ||
/^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*$/i; | ||
/** | ||
* Not verify `display_name` | ||
* */ | ||
const isEmail = (val, options) => { | ||
// If greater than default max email length | ||
if (val.length > 254) return false; | ||
const parts = val.split('@'); | ||
const domain = parts.pop(); | ||
if (!domain) return false; | ||
let user = parts.join('@'); | ||
if (!isByteLength(user, { max: 64 }) || !isByteLength(domain, { max: 254 })) { | ||
return false; | ||
} | ||
if (!isDomain(domain)) { | ||
if (options && options.disableIPDomain) { | ||
return false; | ||
} | ||
// user@[192.168.1.1] | ||
// user@[2001:0db8:85a3:0000:0000:8a2e:0370:7334] | ||
if (!isIP(domain)) { | ||
if (!domain.startsWith('[') || !domain.endsWith(']')) { | ||
return false; | ||
} | ||
const noBracketdomain = domain.slice(1, -1); | ||
if (noBracketdomain.length === 0 || !isIP(noBracketdomain)) { | ||
return false; | ||
} | ||
} | ||
} | ||
if (user[0] === '"') { | ||
user = user.slice(1, user.length - 1); | ||
return quotedEmailUserUtf8.test(user); | ||
} | ||
const userParts = user.split('.'); | ||
for (let i = 0; i < userParts.length; i++) { | ||
if (!emailUserUtf8Part.test(userParts[i])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; | ||
@@ -1233,7 +1561,13 @@ const freeSelf = | ||
isArray, | ||
isBase64, | ||
isBrowser, | ||
isBuffer, | ||
isByteLength, | ||
isCNPhone, | ||
isDate, | ||
isDomain, | ||
isEmail, | ||
isEmptyObject, | ||
isFunction, | ||
isIP, | ||
isInBounds, | ||
@@ -1244,3 +1578,5 @@ isMap, | ||
isObject, | ||
isPhone, | ||
isPlainObject, | ||
isPort, | ||
isPrimitiveValue, | ||
@@ -1247,0 +1583,0 @@ isPromise, |
@@ -12,2 +12,198 @@ (function (global, factory) { | ||
// https://github.com/validatorjs/validator.js/blob/master/src/lib/isMobilePhone.js | ||
const phones = { | ||
'zh-CN': /^((\+|00)86)?(1[3-9]|9[28])\d{9}$/, | ||
'zh-TW': /^(\+?886\-?|0)?9\d{8}$/, | ||
'am-AM': /^(\+?374|0)(33|4[134]|55|77|88|9[13-689])\d{6}$/, | ||
'ar-AE': /^((\+?971)|0)?5[024568]\d{7}$/, | ||
'ar-BH': /^(\+?973)?(3|6)\d{7}$/, | ||
'ar-DZ': /^(\+?213|0)(5|6|7)\d{8}$/, | ||
'ar-LB': /^(\+?961)?((3|81)\d{6}|7\d{7})$/, | ||
'ar-EG': /^((\+?20)|0)?1[0125]\d{8}$/, | ||
'ar-IQ': /^(\+?964|0)?7[0-9]\d{8}$/, | ||
'ar-JO': /^(\+?962|0)?7[789]\d{7}$/, | ||
'ar-KW': /^(\+?965)([569]\d{7}|41\d{6})$/, | ||
'ar-LY': /^((\+?218)|0)?(9[1-6]\d{7}|[1-8]\d{7,9})$/, | ||
'ar-MA': /^(?:(?:\+|00)212|0)[5-7]\d{8}$/, | ||
'ar-OM': /^((\+|00)968)?(9[1-9])\d{6}$/, | ||
'ar-PS': /^(\+?970|0)5[6|9](\d{7})$/, | ||
'ar-SA': /^(!?(\+?966)|0)?5\d{8}$/, | ||
'ar-SD': /^((\+?249)|0)?(9[012369]|1[012])\d{7}$/, | ||
'ar-SY': /^(!?(\+?963)|0)?9\d{8}$/, | ||
'ar-TN': /^(\+?216)?[2459]\d{7}$/, | ||
'az-AZ': /^(\+994|0)(10|5[015]|7[07]|99)\d{7}$/, | ||
'bs-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}$/, | ||
'ca-AD': /^(\+376)?[346]\d{5}$/, | ||
'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)(5[0-25-9]\d|6([23]|0\d?)|7([0-57-9]|6\d))\d{7,9}$/, | ||
'de-AT': /^(\+43|0)\d{1,4}\d{3,12}$/, | ||
'de-CH': /^(\+41|0)([1-9])\d{1,9}$/, | ||
'de-LU': /^(\+352)?((6\d1)\d{6})$/, | ||
'dv-MV': /^(\+?960)?(7[2-9]|9[1-9])\d{5}$/, | ||
'el-GR': /^(\+?30|0)?6(8[5-9]|9(?![26])[0-9])\d{7}$/, | ||
'el-CY': /^(\+?357?)?(9(9|6)\d{6})$/, | ||
'en-AI': | ||
/^(\+?1|0)264(?:2(35|92)|4(?:6[1-2]|76|97)|5(?:3[6-9]|8[1-4])|7(?:2(4|9)|72))\d{4}$/, | ||
'en-AU': /^(\+?61|0)4\d{8}$/, | ||
'en-AG': | ||
/^(?:\+1|1)268(?:464|7(?:1[3-9]|[28]\d|3[0246]|64|7[0-689]))\d{4}$/, | ||
'en-BM': /^(\+?1)?441(((3|7)\d{6}$)|(5[0-3][0-9]\d{4}$)|(59\d{5}$))/, | ||
'en-BS': /^(\+?1[-\s]?|0)?\(?242\)?[-\s]?\d{3}[-\s]?\d{4}$/, | ||
'en-GB': /^(\+?44|0)7[1-9]\d{8}$/, | ||
'en-GG': /^(\+?44|0)1481\d{6}$/, | ||
'en-GH': /^(\+233|0)(20|50|24|54|27|57|26|56|23|28|55|59)\d{7}$/, | ||
'en-GY': /^(\+592|0)6\d{6}$/, | ||
'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-JM': /^(\+?876)?\d{7}$/, | ||
'en-KE': /^(\+?254|0)(7|1)\d{8}$/, | ||
'fr-CF': /^(\+?236| ?)(70|75|77|72|21|22)\d{6}$/, | ||
'en-SS': /^(\+?211|0)(9[1257])\d{7}$/, | ||
'en-KI': /^((\+686|686)?)?( )?((6|7)(2|3|8)[0-9]{6})$/, | ||
'en-KN': /^(?:\+1|1)869(?:46\d|48[89]|55[6-8]|66\d|76[02-7])\d{4}$/, | ||
'en-LS': /^(\+?266)(22|28|57|58|59|27|52)\d{6}$/, | ||
'en-MT': /^(\+?356|0)?(99|79|77|21|27|22|25)[0-9]{6}$/, | ||
'en-MU': /^(\+?230|0)?\d{8}$/, | ||
'en-MW': | ||
/^(\+?265|0)(((77|88|31|99|98|21)\d{7})|(((111)|1)\d{6})|(32000\d{4}))$/, | ||
'en-NA': /^(\+?264|0)(6|8)\d{7}$/, | ||
'en-NG': /^(\+?234|0)?[789]\d{9}$/, | ||
'en-NZ': /^(\+?64|0)[28]\d{7,9}$/, | ||
'en-PG': /^(\+?675|0)?(7\d|8[18])\d{6}$/, | ||
'en-PK': /^((00|\+)?92|0)3[0-6]\d{8}$/, | ||
'en-PH': /^(09|\+639)\d{9}$/, | ||
'en-RW': /^(\+?250|0)?[7]\d{8}$/, | ||
'en-SG': /^(\+65)?[3689]\d{7}$/, | ||
'en-SL': /^(\+?232|0)\d{8}$/, | ||
'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}$/, | ||
'en-BW': /^(\+?267)?(7[1-8]{1})\d{6}$/, | ||
'es-AR': /^\+?549(11|[2368]\d)\d{8}$/, | ||
'es-BO': /^(\+?591)?(6|7)\d{7}$/, | ||
'es-CO': /^(\+?57)?3(0(0|1|2|4|5)|1\d|2[0-4]|5(0|1))\d{7}$/, | ||
'es-CL': /^(\+?56|0)[2-9]\d{1}\d{7}$/, | ||
'es-CR': /^(\+506)?[2-8]\d{7}$/, | ||
'es-CU': /^(\+53|0053)?5\d{7}$/, | ||
'es-DO': /^(\+?1)?8[024]9\d{7}$/, | ||
'es-HN': /^(\+?504)?[9|8|3|2]\d{7}$/, | ||
'es-EC': /^(\+?593|0)([2-7]|9[2-9])\d{7}$/, | ||
'es-ES': /^(\+?34)?[6|7]\d{8}$/, | ||
'es-GT': /^(\+?502)?[2|6|7]\d{7}$/, | ||
'es-PE': /^(\+?51)?9\d{8}$/, | ||
'es-MX': /^(\+?52)?(1|01)?\d{10,11}$/, | ||
'es-NI': /^(\+?505)\d{7,8}$/, | ||
'es-PA': /^(\+?507)\d{7,8}$/, | ||
'es-PY': /^(\+?595|0)9[9876]\d{7}$/, | ||
'es-SV': /^(\+?503)?[67]\d{7}$/, | ||
'es-UY': /^(\+598|0)9[1-9][\d]{6}$/, | ||
'es-VE': /^(\+?58)?(2|4)\d{9}$/, | ||
'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-6]|50)\s?(\d\s?){4,8}$/, | ||
'fj-FJ': /^(\+?679)?\s?\d{3}\s?\d{4}$/, | ||
'fo-FO': /^(\+?298)?\s?\d{2}\s?\d{2}\s?\d{2}$/, | ||
'fr-BF': /^(\+226|0)[67]\d{7}$/, | ||
'fr-BJ': /^(\+229)\d{8}$/, | ||
'fr-CD': /^(\+?243|0)?(8|9)\d{8}$/, | ||
'fr-CM': /^(\+?237)6[0-9]{8}$/, | ||
'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-PF': /^(\+?689)?8[789]\d{6}$/, | ||
'fr-RE': /^(\+?262|0|00262)[67]\d{8}$/, | ||
'fr-WF': /^(\+681)?\d{6}$/, | ||
'he-IL': /^(\+972|0)([23489]|5[012345689]|77)[1-9]\d{6}$/, | ||
'hu-HU': /^(\+?36|06)(20|30|31|50|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})$/, | ||
'ir-IR': /^(\+98|0)?9\d{9}$/, | ||
'it-IT': /^(\+?39)?\s?3\d{2} ?\d{6,7}$/, | ||
'it-SM': /^((\+378)|(0549)|(\+390549)|(\+3780549))?6\d{5,9}$/, | ||
'ja-JP': /^(\+81[ \-]?(\(0\))?|0)[6789]0[ \-]?\d{4}[ \-]?\d{4}$/, | ||
'ka-GE': /^(\+?995)?(79\d{7}|5\d{8})$/, | ||
'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}$/, | ||
'ky-KG': /^(\+?7\s?\+?7|0)\s?\d{2}\s?\d{3}\s?\d{4}$/, | ||
'lt-LT': /^(\+370|8)\d{8}$/, | ||
'lv-LV': /^(\+?371)2\d{7}$/, | ||
'mg-MG': /^((\+?261|0)(2|3)\d)?\d{7}$/, | ||
'mn-MN': /^(\+|00|011)?976(77|81|88|91|94|95|96|99)\d{6}$/, | ||
'my-MM': | ||
/^(\+?959|09|9)(2[5-7]|3[1-2]|4[0-5]|6[6-9]|7[5-9]|9[6-9])[0-9]{7}$/, | ||
'ms-MY': /^(\+?60|0)1(([0145](-|\s)?\d{7,8})|([236-9](-|\s)?\d{7}))$/, | ||
'mz-MZ': /^(\+?258)?8[234567]\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}$/, | ||
'nl-AW': /^(\+)?297(56|59|64|73|74|99)\d{5}$/, | ||
'nn-NO': /^(\+?47)?[49]\d{7}$/, | ||
'pl-PL': /^(\+?48)? ?([5-8]\d|45) ?\d{3} ?\d{2} ?\d{2}$/, | ||
'pt-BR': | ||
/^((\+?55\ ?[1-9]{2}\ ?)|(\+?55\ ?\([1-9]{2}\)\ ?)|(0[1-9]{2}\ ?)|(\([1-9]{2}\)\ ?)|([1-9]{2}\ ?))((\d{4}\-?\d{4})|(9[1-9]{1}\d{3}\-?\d{4}))$/, | ||
'pt-PT': /^(\+?351)?9[1236]\d{7}$/, | ||
'pt-AO': /^(\+244)\d{9}$/, | ||
'ro-MD': /^(\+?373|0)((6(0|1|2|6|7|8|9))|(7(6|7|8|9)))\d{6}$/, | ||
'ro-RO': /^(\+?40|0)\s?7\d{2}(\/|\s|\.|-)?\d{3}(\s|\.|-)?\d{3}$/, | ||
'ru-RU': /^(\+?7|8)?9\d{9}$/, | ||
'si-LK': /^(?:0|94|\+94)?(7(0|1|2|4|5|6|7|8)( |-)?)\d{7}$/, | ||
'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}$/, | ||
'so-SO': /^(\+?252|0)((6[0-9])\d{7}|(7[1-9])\d{7})$/, | ||
'sq-AL': /^(\+355|0)6[789]\d{6}$/, | ||
'sr-RS': /^(\+3816|06)[- \d]{5,9}$/, | ||
'sv-SE': /^(\+?46|0)[\s\-]?7[\s\-]?[02369]([\s\-]?\d){7}$/, | ||
'tg-TJ': /^(\+?992)?[5][5]\d{7}$/, | ||
'th-TH': /^(\+66|66|0)\d{9}$/, | ||
'tr-TR': /^(\+?90|0)?5\d{9}$/, | ||
'tk-TM': /^(\+993|993|8)\d{8}$/, | ||
'uk-UA': /^(\+?38)?0(50|6[36-8]|7[357]|9[1-9])\d{7}$/, | ||
'uz-UZ': /^(\+?998)?(6[125-79]|7[1-69]|88|9\d)\d{7}$/, | ||
'vi-VN': | ||
/^((\+?84)|0)((3([2-9]))|(5([25689]))|(7([0|6-9]))|(8([1-9]))|(9([0-9])))([0-9]{7})$/, | ||
'dz-BT': /^(\+?975|0)?(17|16|77|02)\d{6}$/, | ||
'ar-YE': /^(((\+|00)9677|0?7)[0137]\d{7}|((\+|00)967|0)[1-7]\d{6})$/, | ||
'ar-EH': /^(\+?212|0)[\s\-]?(5288|5289)[\s\-]?\d{5}$/, | ||
'fa-AF': /^(\+93|0)?(2{1}[0-8]{1}|[3-5]{1}[0-4]{1})(\d{7})$/, | ||
// aliases | ||
get 'en-CA'() { | ||
return phones['en-US']; | ||
}, | ||
get 'fr-CA'() { | ||
return phones['en-CA']; | ||
}, | ||
get 'fr-BE'() { | ||
return phones['nl-BE']; | ||
}, | ||
get 'zh-HK'() { | ||
return phones['en-HK']; | ||
}, | ||
get 'zh-MO'() { | ||
return phones['en-MO']; | ||
}, | ||
get 'ga-IE'() { | ||
return phones['en-IE']; | ||
}, | ||
get 'fr-CH'() { | ||
return phones['de-CH']; | ||
}, | ||
get 'it-CH'() { | ||
return phones['fr-CH']; | ||
}, | ||
}; | ||
const objectToString = Object.prototype.toString; | ||
@@ -49,2 +245,3 @@ const supportWasm = typeof WebAssembly === 'object'; | ||
const isPromise = (v) => isObject(v) && typeof v.then === 'function'; | ||
const isPort = (n) => Number.isInteger(n) && n >= 0 && n <= 65535; | ||
const isBuffer = (v) => { | ||
@@ -77,2 +274,138 @@ if (!isObject(v)) return false; | ||
}; | ||
const isByteLength = (val, options = {}) => { | ||
const min = options.min || 0; | ||
const max = options.max; | ||
const len = encodeURI(val).split(/%..|./).length - 1; | ||
return len >= min && (typeof max === 'undefined' || len <= max); | ||
}; | ||
const notBase64 = /[^A-Z0-9+\/=]/i; | ||
const urlSafeBase64 = /^[A-Z0-9_\-]*$/i; | ||
const isBase64 = (val, urlSafe = false) => { | ||
const len = val.length; | ||
if (urlSafe) return urlSafeBase64.test(val); | ||
if (len % 4 !== 0 || notBase64.test(val)) return false; | ||
const firstPaddingChar = val.indexOf('='); | ||
return ( | ||
firstPaddingChar === -1 || | ||
firstPaddingChar === len - 1 || | ||
(firstPaddingChar === len - 2 && val[len - 1] === '=') | ||
); | ||
}; | ||
const IPv4SegmentFormat = | ||
'(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'; | ||
const IPv4AddressFormat = `(${IPv4SegmentFormat}[.]){3}${IPv4SegmentFormat}`; | ||
const IPv4AddressRegExp = new RegExp(`^${IPv4AddressFormat}$`); | ||
const IPv6SegmentFormat = '(?:[0-9a-fA-F]{1,4})'; | ||
const IPv6AddressRegExp = new RegExp( | ||
'^(' + | ||
`(?:${IPv6SegmentFormat}:){7}(?:${IPv6SegmentFormat}|:)|` + | ||
`(?:${IPv6SegmentFormat}:){6}(?:${IPv4AddressFormat}|:${IPv6SegmentFormat}|:)|` + | ||
`(?:${IPv6SegmentFormat}:){5}(?::${IPv4AddressFormat}|(:${IPv6SegmentFormat}){1,2}|:)|` + | ||
`(?:${IPv6SegmentFormat}:){4}(?:(:${IPv6SegmentFormat}){0,1}:${IPv4AddressFormat}|(:${IPv6SegmentFormat}){1,3}|:)|` + | ||
`(?:${IPv6SegmentFormat}:){3}(?:(:${IPv6SegmentFormat}){0,2}:${IPv4AddressFormat}|(:${IPv6SegmentFormat}){1,4}|:)|` + | ||
`(?:${IPv6SegmentFormat}:){2}(?:(:${IPv6SegmentFormat}){0,3}:${IPv4AddressFormat}|(:${IPv6SegmentFormat}){1,5}|:)|` + | ||
`(?:${IPv6SegmentFormat}:){1}(?:(:${IPv6SegmentFormat}){0,4}:${IPv4AddressFormat}|(:${IPv6SegmentFormat}){1,6}|:)|` + | ||
`(?::((?::${IPv6SegmentFormat}){0,5}:${IPv4AddressFormat}|(?::${IPv6SegmentFormat}){1,7}|:))` + | ||
')(%[0-9a-zA-Z-.:]{1,})?$', | ||
); | ||
// https://github.com/validatorjs/validator.js/blob/master/src/lib/isIP.js | ||
const isIP = (val, version) => { | ||
if (!version) return isIP(val, '4') || isIP(val, '6'); | ||
if (version === '4') return IPv4AddressRegExp.test(val); | ||
if (version === '6') return IPv6AddressRegExp.test(val); | ||
return false; | ||
}; | ||
const isDomain = (val) => { | ||
const parts = val.split('.'); | ||
if (parts.length < 2) return false; | ||
const tld = parts[parts.length - 1]; | ||
const reg = | ||
/^([a-z\u00A1-\u00A8\u00AA-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]{2,}|xn[a-z0-9-]{2,})$/i; | ||
if (!reg.test(tld)) return false; | ||
if (/\s/.test(tld)) return false; | ||
if (/^\d+$/.test(tld)) return false; | ||
return parts.every((part) => { | ||
if (part.length > 63) return false; | ||
if (!/^[a-z_\u00a1-\uffff0-9-]+$/i.test(part)) return false; | ||
if (/[\uff01-\uff5e]/.test(part)) return false; | ||
if (/^-|-$/.test(part)) return false; | ||
if (/_/.test(part)) return false; | ||
return true; | ||
}); | ||
}; | ||
const isPhone = (val, locale, strictMode) => { | ||
if (strictMode && !val.startsWith('+')) { | ||
return false; | ||
} | ||
if (typeof locale === 'string' && locale in phones) { | ||
return phones[locale].test(val); | ||
} | ||
if (isArray(locale)) { | ||
return locale.some((key) => { | ||
if (phones.hasOwnProperty(key)) { | ||
if (phones[key].test(val)) return true; | ||
} | ||
return false; | ||
}); | ||
} | ||
if (!locale) { | ||
for (const key in phones) { | ||
if (phones.hasOwnProperty(key)) { | ||
const phone = phones[key]; | ||
if (phone.test(val)) return true; | ||
} | ||
} | ||
return false; | ||
} | ||
throw new Error(`Invalid locale '${locale}'`); | ||
}; | ||
const isCNPhone = (val, strictMode) => isPhone(val, 'zh-CN', strictMode); | ||
const emailUserUtf8Part = | ||
/^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~\u00A1-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+$/i; | ||
const quotedEmailUserUtf8 = | ||
/^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*$/i; | ||
/** | ||
* Not verify `display_name` | ||
* */ | ||
const isEmail = (val, options) => { | ||
// If greater than default max email length | ||
if (val.length > 254) return false; | ||
const parts = val.split('@'); | ||
const domain = parts.pop(); | ||
if (!domain) return false; | ||
let user = parts.join('@'); | ||
if ( | ||
!isByteLength(user, { max: 64 }) || | ||
!isByteLength(domain, { max: 254 }) | ||
) { | ||
return false; | ||
} | ||
if (!isDomain(domain)) { | ||
if (options && options.disableIPDomain) { | ||
return false; | ||
} | ||
// user@[192.168.1.1] | ||
// user@[2001:0db8:85a3:0000:0000:8a2e:0370:7334] | ||
if (!isIP(domain)) { | ||
if (!domain.startsWith('[') || !domain.endsWith(']')) { | ||
return false; | ||
} | ||
const noBracketdomain = domain.slice(1, -1); | ||
if (noBracketdomain.length === 0 || !isIP(noBracketdomain)) { | ||
return false; | ||
} | ||
} | ||
} | ||
if (user[0] === '"') { | ||
user = user.slice(1, user.length - 1); | ||
return quotedEmailUserUtf8.test(user); | ||
} | ||
const userParts = user.split('.'); | ||
for (let i = 0; i < userParts.length; i++) { | ||
if (!emailUserUtf8Part.test(userParts[i])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; | ||
@@ -1292,7 +1625,13 @@ class Queue { | ||
exports.isArray = isArray; | ||
exports.isBase64 = isBase64; | ||
exports.isBrowser = isBrowser; | ||
exports.isBuffer = isBuffer; | ||
exports.isByteLength = isByteLength; | ||
exports.isCNPhone = isCNPhone; | ||
exports.isDate = isDate; | ||
exports.isDomain = isDomain; | ||
exports.isEmail = isEmail; | ||
exports.isEmptyObject = isEmptyObject; | ||
exports.isFunction = isFunction; | ||
exports.isIP = isIP; | ||
exports.isInBounds = isInBounds; | ||
@@ -1303,3 +1642,5 @@ exports.isMap = isMap; | ||
exports.isObject = isObject; | ||
exports.isPhone = isPhone; | ||
exports.isPlainObject = isPlainObject; | ||
exports.isPort = isPort; | ||
exports.isPrimitiveValue = isPrimitiveValue; | ||
@@ -1306,0 +1647,0 @@ exports.isPromise = isPromise; |
@@ -25,2 +25,10 @@ export { Queue } from 'small-queue'; | ||
isPromise, | ||
isBase64, | ||
isIP, | ||
isPort, | ||
isDomain, | ||
isEmail, | ||
isPhone, | ||
isCNPhone, | ||
isByteLength, | ||
isWindow, | ||
@@ -59,2 +67,3 @@ isBrowser, | ||
PrimitiveType, | ||
PhoneLocales, | ||
Prettify, | ||
@@ -61,0 +70,0 @@ DeepPrettify, |
@@ -1,2 +0,2 @@ | ||
import type { TypedArray, PrimitiveType } from './types'; | ||
import type { TypedArray, PrimitiveType, PhoneLocales } from './types'; | ||
export declare const supportWasm: boolean; | ||
@@ -31,2 +31,3 @@ export declare const toRawType: (v: unknown) => string; | ||
) => v is PromiseLike<T>; | ||
export declare const isPort: (n: number) => boolean; | ||
export declare const isBuffer: (v: unknown) => boolean; | ||
@@ -38,1 +39,26 @@ export declare const isInBounds: ([a, b]: Array<number>, v: number) => boolean; | ||
export declare const isPrimitiveValue: (v: unknown) => v is PrimitiveType; | ||
export declare const isByteLength: ( | ||
val: string, | ||
options?: { | ||
max?: number; | ||
min?: number; | ||
}, | ||
) => boolean; | ||
export declare const isBase64: (val: string, urlSafe?: boolean) => boolean; | ||
export declare const isIP: (val: string, version?: '4' | '6') => boolean; | ||
export declare const isDomain: (val: string) => boolean; | ||
export declare const isPhone: ( | ||
val: string, | ||
locale?: PhoneLocales | Array<PhoneLocales>, | ||
strictMode?: boolean, | ||
) => boolean; | ||
export declare const isCNPhone: (val: string, strictMode?: boolean) => boolean; | ||
/** | ||
* Not verify `display_name` | ||
* */ | ||
export declare const isEmail: ( | ||
val: string, | ||
options?: { | ||
disableIPDomain?: boolean; | ||
}, | ||
) => boolean; |
@@ -0,1 +1,3 @@ | ||
import { phones } from './phoneRegExp'; | ||
export type PhoneLocales = keyof typeof phones; | ||
export type Awaitable<T> = T | PromiseLike<T>; | ||
@@ -2,0 +4,0 @@ export type Nullable<T> = T | null | undefined; |
{ | ||
"name": "aidly", | ||
"version": "1.3.2", | ||
"version": "1.4.0", | ||
"description": "Tool library.", | ||
@@ -17,3 +17,3 @@ "sideEffects": false, | ||
}, | ||
"files": ["dist"], | ||
"files": ["dist", "README.zh-CN.md"], | ||
"scripts": { | ||
@@ -20,0 +20,0 @@ "test": "jest", |
111
README.md
@@ -8,19 +8,21 @@ <div align="center"> | ||
A small utility function in pure js runtime, capable of effective tree-shaking. | ||
<div align="center"> | ||
English | [简体中文](./README.zh-CN.md) | ||
### Debugging platform | ||
</div> | ||
> https://imtaotao.github.io/aidly/ | ||
<h1></h1> | ||
A collection of pure JavaScript runtime utility functions that is highly tree-shaking. | ||
### API | ||
View type declaration file retrieval api. | ||
### Debugging platform | ||
> https://unpkg.com/browse/aidly/dist/index.d.ts | ||
> https://imtaotao.github.io/aidly/ | ||
### Demo | ||
### Usage | ||
```js | ||
@@ -35,13 +37,86 @@ import { clone } from 'aidly'; | ||
```html | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<body> | ||
<script src="https://unpkg.com/aidly/dist/aidly.umd.js"></script> | ||
<script> | ||
const { clone } = window.Aidly; | ||
console.log(clone(/a/ig)); // `/a/gi` | ||
</script> | ||
</body> | ||
</html> | ||
<script src="https://unpkg.com/aidly/dist/aidly.umd.js"></script> | ||
<script> | ||
const { clone } = window.Aidly; | ||
console.log(clone(/a/ig)); // `/a/gi` | ||
</script> | ||
``` | ||
### API | ||
Here are all the functions. If you need any other functions, please open a discussion in the [Issues](https://github.com/imtaotao/aidly/issues) section. | ||
> https://unpkg.com/browse/aidly/dist/index.d.ts | ||
Api | Description | ||
-------------------- | -------------------------------------- | ||
`throttle` | Throttle function, the first time it triggers the function call immediately. | ||
`debounce` | Debounce function, the first time it is called, it will trigger the function call immediately. | ||
`loopSlice` | Slice the specified number of loops, the default slicing time is `17ms`. | ||
`uuid` | Generate a `uuid`. | ||
`noop` | Empty arrow function. | ||
`qsParse` | Query string parsing. | ||
`qsStringify` | Query string formatted as a string. | ||
`rgbToHsl` | `RGB` to `Hsl`. | ||
`rgbToHex` | `RGB` to `Hexadecimal`. | ||
`rgbToAnsi256` | `RGB` to `Ansi256`. | ||
`hslToRgb` | `Hsl` to `RGB`. | ||
`hexToRgb` | `Hexadecimal` to `RGB`. | ||
`ansi256ToRgb` | `Ansi256` to `RGB`. | ||
`randomColor` | Generate a random color value, the default format is `RGB`. | ||
`assert` | Assert a condition. | ||
`raf` | Compatibility handling for `requestAnimationFrame`. | ||
`now` | Compatibility handling for `Date.now` and `performance.now`. | ||
`idleCallback` | Compatibility handling for `requestIdleCallback`. | ||
`isAbsolute` | Determines whether a path is an absolute path in the browser. | ||
`last` | Returns the nth to last element in an array (default is the last element). | ||
`uniq` | Array deduplication. | ||
`hasOwn` | `Object.hasOwnProperty.call` alias. | ||
`slash` | Replace `\` with `/`. | ||
`makeMap` | Convert an `Array<string>` to a `Record<string, (key) => boolean>`. | ||
`decimalPlaces` | Return the number of decimal places in a number. | ||
`random` | Return a random number in a specified range. If only one parameter `T` is specified, the range defaults to `0 ~ T`. | ||
`once` | Generates a higher-order function that will be executed only once. | ||
`sleep` | Pause for `n` ms, return a promise. | ||
`remove` | Remove the specified element from a `Array` or `Set`. | ||
`map` | Perform a map operation on `Object`, `Array`, `Set` and return a brand new copy. | ||
`toCamelCase` | Convert a string to `camelCase` or `pascalCase` naming, processing `_` and `-` by default. | ||
`getExtname` | Get the `extname` of a url. | ||
`getIteratorFn` | Get an object's iterator function. | ||
`sortKeys` | Sort the keys of an `Object` and return a new copy. | ||
`clearUndef` | Clearing `undefined` values from an object. | ||
`pick` | Select the required attribute values from an object and return a new copy. | ||
`unindent` | Formatting template strings `(unindet(`string`))`. | ||
`defered` | Returns a `defered` object. | ||
`supportWasm` | Check if the current environment supports `WebAssembly`. | ||
`isBrowser` | Check if you are in the browser environment. | ||
`isNil` | Check if a value is `null` or `undefined`. | ||
`isNumber` | Check if a value is a `Number`. | ||
`isString` | Check if a value is a `String`. | ||
`isFunction` | Check if a value is a `Function`. | ||
`isObject` | Check if a value is an `Object`. | ||
`isPlainObject` | Check if a value is a `plainObject`. | ||
`isDate` | Check if a value is a `Date`. | ||
`isRegExp` | Check if a value is a `RegExp`. | ||
`isSet` | Check if a value is a `Set`. | ||
`isWeakSet` | Check if a value is a `WeakSet`. | ||
`isMap` | Check if a value is a `Map`. | ||
`isWeakMap` | Check if a value is a `WeakMap`. | ||
`isPromise` | Check if a value is a `PromiseLike`. | ||
`isPrimitiveValue` | Check if a value is a primitive value. | ||
`isEmptyObject` | Check if an object is empty object. | ||
`isWindow` | Check if an object is a `window` object. | ||
`isBase64` | Check if a string is a valid base64 string. | ||
`isInBounds` | Check if a number is within a specified range `(isInBounds([1, 5], 2))`. | ||
`isIP` | Check if it is a valid `IP` address. | ||
`isDomain` | Check if it is a valid `domain` name. | ||
`isPort` | Check if it is a valid `port`. | ||
`isEmail` | Check if it is a valid `email`. | ||
`isPhone` | Check if it is a valid `mobile number`. | ||
`isCNPhone` | Check if it is a valid `mobile phone` number in `mainland China`. | ||
`root` | Alias for global objects, compatible with different environments. | ||
`jsonStringify` | Format objects as json strings, able to handle reference relationships (including circular references). | ||
`jsonParse` | Parse the json string into an object, able to handle reference relationships (including circular references). | ||
`merge` | Merges two `objects` or `arrays` and returns a new object, without worrying about circular references. | ||
`clone` | Clone an object that allows circular references, types include `Object`, `Array`, `TypeArray`, `Set`, `Map`, `RegExp`, `Date`, `Promise`, `Buffer`, `Response`, `setter/getter`, `Non-enumerable properties (not cloned by default)`. |
Sorry, the diff of this file is not supported yet
178833
20
5395
120