isIPRange.js 890 B

12345678910111213141516171819202122232425262728293031323334353637
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = isIPRange;
  6. var _assertString = _interopRequireDefault(require("./util/assertString"));
  7. var _isIP = _interopRequireDefault(require("./isIP"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. var subnetMaybe = /^\d{1,2}$/;
  10. function isIPRange(str) {
  11. (0, _assertString.default)(str);
  12. var parts = str.split('/'); // parts[0] -> ip, parts[1] -> subnet
  13. if (parts.length !== 2) {
  14. return false;
  15. }
  16. if (!subnetMaybe.test(parts[1])) {
  17. return false;
  18. } // Disallow preceding 0 i.e. 01, 02, ...
  19. if (parts[1].length > 1 && parts[1].startsWith('0')) {
  20. return false;
  21. }
  22. return (0, _isIP.default)(parts[0], 4) && parts[1] <= 32 && parts[1] >= 0;
  23. }
  24. module.exports = exports.default;
  25. module.exports.default = exports.default;