import { toString } from 'ut2';
// ipv6正则
const reg = /^((([0-9A-F]{1,4}:){7}([0-9A-F]{1,4}|:))|(([0-9A-F]{1,4}:){6}(:[0-9A-F]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|\d{1,2})(\.(25[0-5]|2[0-4]\d|1\d\d|\d{1,2})){3})|:))|(([0-9A-F]{1,4}:){5}(((:[0-9A-F]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|\d{1,2})(\.(25[0-5]|2[0-4]\d|1\d\d|\d{1,2})){3})|:))|(([0-9A-F]{1,4}:){4}(((:[0-9A-F]{1,4}){1,3})|((:[0-9A-F]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|\d{1,2})(\.(25[0-5]|2[0-4]\d|1\d\d|\d{1,2})){3}))|:))|(([0-9A-F]{1,4}:){3}(((:[0-9A-F]{1,4}){1,4})|((:[0-9A-F]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|\d{1,2})(\.(25[0-5]|2[0-4]\d|1\d\d|\d{1,2})){3}))|:))|(([0-9A-F]{1,4}:){2}(((:[0-9A-F]{1,4}){1,5})|((:[0-9A-F]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|\d{1,2})(\.(25[0-5]|2[0-4]\d|1\d\d|\d{1,2})){3}))|:))|(([0-9A-F]{1,4}:){1}(((:[0-9A-F]{1,4}){1,6})|((:[0-9A-F]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|\d{1,2})(\.(25[0-5]|2[0-4]\d|1\d\d|\d{1,2})){3}))|:))|(:(((:[0-9A-F]{1,4}){1,7})|((:[0-9A-F]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|\d{1,2})(\.(25[0-5]|2[0-4]\d|1\d\d|\d{1,2})){3}))|:)))(%.+)?$/i;
/**
* 检测值是否为ipv6
*
* @alias module:Validator.isIPv6
* @since 1.1.0
* @param {*} value 要检测的值
* @returns {boolean} 值是否为ipv6
* @example
*
* // 冒分十六进制表示法
* isIPv6('2001:0DB8:0000:0023:0008:0800:200C:417A'); // true
*
* // 前导0省略
* isIPv6('2001:DB8:0:23:8:800:200C:417A'); // true
* isIPv6('FF01:0:0:0:0:0:0:1101'); // true
*
* // 0位压缩表示法
* isIPv6('FF01::1101'); // true
* isIPv6('::1'); // true
* isIPv6('::'); // true
* isIPv6('0:0:0:0:0:0:0:1'); // true
* isIPv6('0:0:0:0:0:0:0:0'); // true
*
* // 内嵌IPv4地址表示法
* isIPv6('::192.168.1.1'); // true
* isIPv6('::FFFF:192.168.1.1'); // true
* isIPv6('192.168.1.1'); // false
*
*/
function isIPv6(value) {
const valueStr = toString(value);
return reg.test(valueStr);
}
export default isIPv6;