You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

url-parser.js 531 B

8 years ago
123456789101112131415161718
  1. 'use strict';
  2. import tld from 'tldjs';
  3. function _ipIsValid(ipAddress) {
  4. return Boolean(/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ipAddress));
  5. }
  6. function getDomainName(urlStr) {
  7. const domain = tld.getDomain(urlStr);
  8. const subDomain = tld.getSubdomain(urlStr);
  9. const ip = `${subDomain}.${domain}`;
  10. if (_ipIsValid(ip)) {
  11. return ip;
  12. }
  13. return domain;
  14. }
  15. export {getDomainName, _ipIsValid};