mirror of
https://github.com/swc-project/swc.git
synced 2024-12-23 05:32:09 +03:00
29 lines
1.2 KiB
TypeScript
29 lines
1.2 KiB
TypeScript
// Loaded from https://deno.land/x/isIP@1.0.0/mod.ts
|
|
|
|
|
|
const regIP4 = new RegExp(
|
|
"^([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\\.([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\\.([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\\.([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])$",
|
|
);
|
|
|
|
const regIP6 = new RegExp(
|
|
"^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$",
|
|
);
|
|
|
|
export function isIPv4(ip: string): boolean {
|
|
return regIP4.test(ip);
|
|
}
|
|
|
|
export function isIPv6(ip: string): boolean {
|
|
return regIP6.test(ip);
|
|
}
|
|
|
|
export function isIP(ip: string): number {
|
|
if (isIPv4(ip)) {
|
|
return 4;
|
|
} else if (isIPv6(ip)) {
|
|
return 6;
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|