isIP.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. /**
  9. 11.3. Examples
  10. The following addresses
  11. fe80::1234 (on the 1st link of the node)
  12. ff02::5678 (on the 5th link of the node)
  13. ff08::9abc (on the 10th organization of the node)
  14. would be represented as follows:
  15. fe80::1234%1
  16. ff02::5678%5
  17. ff08::9abc%10
  18. (Here we assume a natural translation from a zone index to the
  19. <zone_id> part, where the Nth zone of any scope is translated into
  20. "N".)
  21. If we use interface names as <zone_id>, those addresses could also be
  22. represented as follows:
  23. fe80::1234%ne0
  24. ff02::5678%pvc1.3
  25. ff08::9abc%interface10
  26. where the interface "ne0" belongs to the 1st link, "pvc1.3" belongs
  27. to the 5th link, and "interface10" belongs to the 10th organization.
  28. * * */
  29. var IPv4SegmentFormat = '(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])';
  30. var IPv4AddressFormat = "(".concat(IPv4SegmentFormat, "[.]){3}").concat(IPv4SegmentFormat);
  31. var IPv4AddressRegExp = new RegExp("^".concat(IPv4AddressFormat, "$"));
  32. var IPv6SegmentFormat = '(?:[0-9a-fA-F]{1,4})';
  33. 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,})?$');
  34. function isIP(str) {
  35. var version = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
  36. (0, _assertString.default)(str);
  37. version = String(version);
  38. if (!version) {
  39. return isIP(str, 4) || isIP(str, 6);
  40. }
  41. if (version === '4') {
  42. if (!IPv4AddressRegExp.test(str)) {
  43. return false;
  44. }
  45. var parts = str.split('.').sort(function (a, b) {
  46. return a - b;
  47. });
  48. return parts[3] <= 255;
  49. }
  50. if (version === '6') {
  51. return !!IPv6AddressRegExp.test(str);
  52. }
  53. return false;
  54. }
  55. module.exports = exports.default;
  56. module.exports.default = exports.default;