isFQDN.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = isFDQN;
  6. var _assertString = require('./util/assertString');
  7. var _assertString2 = _interopRequireDefault(_assertString);
  8. var _merge = require('./util/merge');
  9. var _merge2 = _interopRequireDefault(_merge);
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. var default_fqdn_options = {
  12. require_tld: true,
  13. allow_underscores: false,
  14. allow_trailing_dot: false
  15. };
  16. function isFDQN(str, options) {
  17. (0, _assertString2.default)(str);
  18. options = (0, _merge2.default)(options, default_fqdn_options);
  19. /* Remove the optional trailing dot before checking validity */
  20. if (options.allow_trailing_dot && str[str.length - 1] === '.') {
  21. str = str.substring(0, str.length - 1);
  22. }
  23. var parts = str.split('.');
  24. if (options.require_tld) {
  25. var tld = parts.pop();
  26. if (!parts.length || !/^([a-z\u00a1-\uffff]{2,}|xn[a-z0-9-]{2,})$/i.test(tld)) {
  27. return false;
  28. }
  29. }
  30. for (var part, i = 0; i < parts.length; i++) {
  31. part = parts[i];
  32. if (options.allow_underscores) {
  33. part = part.replace(/_/g, '');
  34. }
  35. if (!/^[a-z\u00a1-\uffff0-9-]+$/i.test(part)) {
  36. return false;
  37. }
  38. if (/[\uff01-\uff5e]/.test(part)) {
  39. // disallow full-width chars
  40. return false;
  41. }
  42. if (part[0] === '-' || part[part.length - 1] === '-') {
  43. return false;
  44. }
  45. }
  46. return true;
  47. }
  48. module.exports = exports['default'];