1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.default = isIP;
- var _assertString = _interopRequireDefault(require("./util/assertString"));
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
- var ipv4Maybe = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
- var ipv6Block = /^[0-9A-F]{1,4}$/i;
- function isIP(str) {
- var version = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
- (0, _assertString.default)(str);
- version = String(version);
- if (!version) {
- return isIP(str, 4) || isIP(str, 6);
- } else if (version === '4') {
- if (!ipv4Maybe.test(str)) {
- return false;
- }
- var parts = str.split('.').sort(function (a, b) {
- return a - b;
- });
- return parts[3] <= 255;
- } else if (version === '6') {
- var blocks = str.split(':');
- var foundOmissionBlock = false;
-
-
-
-
- var foundIPv4TransitionBlock = isIP(blocks[blocks.length - 1], 4);
- var expectedNumberOfBlocks = foundIPv4TransitionBlock ? 7 : 8;
- if (blocks.length > expectedNumberOfBlocks) {
- return false;
- }
- if (str === '::') {
- return true;
- } else if (str.substr(0, 2) === '::') {
- blocks.shift();
- blocks.shift();
- foundOmissionBlock = true;
- } else if (str.substr(str.length - 2) === '::') {
- blocks.pop();
- blocks.pop();
- foundOmissionBlock = true;
- }
- for (var i = 0; i < blocks.length; ++i) {
-
-
- if (blocks[i] === '' && i > 0 && i < blocks.length - 1) {
- if (foundOmissionBlock) {
- return false;
- }
- foundOmissionBlock = true;
- } else if (foundIPv4TransitionBlock && i === blocks.length - 1) {
-
- } else if (!ipv6Block.test(blocks[i])) {
- return false;
- }
- }
- if (foundOmissionBlock) {
- return blocks.length >= 1;
- }
- return blocks.length === expectedNumberOfBlocks;
- }
- return false;
- }
- module.exports = exports.default;
- module.exports.default = exports.default;
|