isIP.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = isIP;
  6. var _assertString = _interopRequireDefault(require("./util/assertString"));
  7. function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
  8. function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
  9. /**
  10. 11.3. Examples
  11. The following addresses
  12. fe80::1234 (on the 1st link of the node)
  13. ff02::5678 (on the 5th link of the node)
  14. ff08::9abc (on the 10th organization of the node)
  15. would be represented as follows:
  16. fe80::1234%1
  17. ff02::5678%5
  18. ff08::9abc%10
  19. (Here we assume a natural translation from a zone index to the
  20. <zone_id> part, where the Nth zone of any scope is translated into
  21. "N".)
  22. If we use interface names as <zone_id>, those addresses could also be
  23. represented as follows:
  24. fe80::1234%ne0
  25. ff02::5678%pvc1.3
  26. ff08::9abc%interface10
  27. where the interface "ne0" belongs to the 1st link, "pvc1.3" belongs
  28. to the 5th link, and "interface10" belongs to the 10th organization.
  29. * * */
  30. var IPv4SegmentFormat = '(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])';
  31. var IPv4AddressFormat = "(".concat(IPv4SegmentFormat, "[.]){3}").concat(IPv4SegmentFormat);
  32. var IPv4AddressRegExp = new RegExp("^".concat(IPv4AddressFormat, "$"));
  33. var IPv6SegmentFormat = '(?:[0-9a-fA-F]{1,4})';
  34. var IPv6AddressRegExp = new RegExp('^(' + "(?:".concat(IPv6SegmentFormat, ":){7}(?:").concat(IPv6SegmentFormat, "|:)|") + "(?:".concat(IPv6SegmentFormat, ":){6}(?:").concat(IPv4AddressFormat, "|:").concat(IPv6SegmentFormat, "|:)|") + "(?:".concat(IPv6SegmentFormat, ":){5}(?::").concat(IPv4AddressFormat, "|(:").concat(IPv6SegmentFormat, "){1,2}|:)|") + "(?:".concat(IPv6SegmentFormat, ":){4}(?:(:").concat(IPv6SegmentFormat, "){0,1}:").concat(IPv4AddressFormat, "|(:").concat(IPv6SegmentFormat, "){1,3}|:)|") + "(?:".concat(IPv6SegmentFormat, ":){3}(?:(:").concat(IPv6SegmentFormat, "){0,2}:").concat(IPv4AddressFormat, "|(:").concat(IPv6SegmentFormat, "){1,4}|:)|") + "(?:".concat(IPv6SegmentFormat, ":){2}(?:(:").concat(IPv6SegmentFormat, "){0,3}:").concat(IPv4AddressFormat, "|(:").concat(IPv6SegmentFormat, "){1,5}|:)|") + "(?:".concat(IPv6SegmentFormat, ":){1}(?:(:").concat(IPv6SegmentFormat, "){0,4}:").concat(IPv4AddressFormat, "|(:").concat(IPv6SegmentFormat, "){1,6}|:)|") + "(?::((?::".concat(IPv6SegmentFormat, "){0,5}:").concat(IPv4AddressFormat, "|(?::").concat(IPv6SegmentFormat, "){1,7}|:))") + ')(%[0-9a-zA-Z.]{1,})?$');
  35. function isIP(ipAddress) {
  36. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  37. (0, _assertString.default)(ipAddress);
  38. // accessing 'arguments' for backwards compatibility: isIP(ipAddress [, version])
  39. // eslint-disable-next-line prefer-rest-params
  40. var version = (_typeof(options) === 'object' ? options.version : arguments[1]) || '';
  41. if (!version) {
  42. return isIP(ipAddress, {
  43. version: 4
  44. }) || isIP(ipAddress, {
  45. version: 6
  46. });
  47. }
  48. if (version.toString() === '4') {
  49. return IPv4AddressRegExp.test(ipAddress);
  50. }
  51. if (version.toString() === '6') {
  52. return IPv6AddressRegExp.test(ipAddress);
  53. }
  54. return false;
  55. }
  56. module.exports = exports.default;
  57. module.exports.default = exports.default;