wurl.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. module.exports = function (arg, url) {
  2. function _t() {
  3. return new RegExp(/(.*?)\.?([^\.]*?)\.(gl|com|net|org|biz|ws|in|me|co\.uk|co|org\.uk|ltd\.uk|plc\.uk|me\.uk|edu|mil|br\.com|cn\.com|eu\.com|hu\.com|no\.com|qc\.com|sa\.com|se\.com|se\.net|us\.com|uy\.com|ac|co\.ac|gv\.ac|or\.ac|ac\.ac|af|am|as|at|ac\.at|co\.at|gv\.at|or\.at|asn\.au|com\.au|edu\.au|org\.au|net\.au|id\.au|be|ac\.be|adm\.br|adv\.br|am\.br|arq\.br|art\.br|bio\.br|cng\.br|cnt\.br|com\.br|ecn\.br|eng\.br|esp\.br|etc\.br|eti\.br|fm\.br|fot\.br|fst\.br|g12\.br|gov\.br|ind\.br|inf\.br|jor\.br|lel\.br|med\.br|mil\.br|net\.br|nom\.br|ntr\.br|odo\.br|org\.br|ppg\.br|pro\.br|psc\.br|psi\.br|rec\.br|slg\.br|tmp\.br|tur\.br|tv\.br|vet\.br|zlg\.br|br|ab\.ca|bc\.ca|mb\.ca|nb\.ca|nf\.ca|ns\.ca|nt\.ca|on\.ca|pe\.ca|qc\.ca|sk\.ca|yk\.ca|ca|cc|ac\.cn|com\.cn|edu\.cn|gov\.cn|org\.cn|bj\.cn|sh\.cn|tj\.cn|cq\.cn|he\.cn|nm\.cn|ln\.cn|jl\.cn|hl\.cn|js\.cn|zj\.cn|ah\.cn|gd\.cn|gx\.cn|hi\.cn|sc\.cn|gz\.cn|yn\.cn|xz\.cn|sn\.cn|gs\.cn|qh\.cn|nx\.cn|xj\.cn|tw\.cn|hk\.cn|mo\.cn|cn|cx|cz|de|dk|fo|com\.ec|tm\.fr|com\.fr|asso\.fr|presse\.fr|fr|gf|gs|co\.il|net\.il|ac\.il|k12\.il|gov\.il|muni\.il|ac\.in|co\.in|org\.in|ernet\.in|gov\.in|net\.in|res\.in|is|it|ac\.jp|co\.jp|go\.jp|or\.jp|ne\.jp|ac\.kr|co\.kr|go\.kr|ne\.kr|nm\.kr|or\.kr|li|lt|lu|asso\.mc|tm\.mc|com\.mm|org\.mm|net\.mm|edu\.mm|gov\.mm|ms|nl|no|nu|pl|ro|org\.ro|store\.ro|tm\.ro|firm\.ro|www\.ro|arts\.ro|rec\.ro|info\.ro|nom\.ro|nt\.ro|se|si|com\.sg|org\.sg|net\.sg|gov\.sg|sk|st|tf|ac\.th|co\.th|go\.th|mi\.th|net\.th|or\.th|tm|to|com\.tr|edu\.tr|gov\.tr|k12\.tr|net\.tr|org\.tr|com\.tw|org\.tw|net\.tw|ac\.uk|uk\.com|uk\.net|gb\.com|gb\.net|vg|sh|kz|ch|info|ua|gov|name|pro|ie|hk|com\.hk|org\.hk|net\.hk|edu\.hk|us|tk|cd|by|ad|lv|eu\.lv|bz|es|jp|cl|ag|mobi|eu|co\.nz|org\.nz|net\.nz|maori\.nz|iwi\.nz|io|la|md|sc|sg|vc|tw|travel|my|se|tv|pt|com\.pt|edu\.pt|asia|fi|com\.ve|net\.ve|fi|org\.ve|web\.ve|info\.ve|co\.ve|tel|im|gr|ru|net\.ru|org\.ru|hr|com\.hr|ly|xyz)$/);
  4. }
  5. function _d(s) {
  6. return decodeURIComponent(s.replace(/\+/g, ' '));
  7. }
  8. function _i(arg, str) {
  9. var sptr = arg.charAt(0),
  10. split = str.split(sptr);
  11. if (sptr === arg) { return split; }
  12. arg = parseInt(arg.substring(1), 10);
  13. return split[arg < 0 ? split.length + arg : arg - 1];
  14. }
  15. function _f(arg, str) {
  16. var sptr = arg.charAt(0),
  17. split = str.split('&'),
  18. field = [],
  19. params = {},
  20. tmp = [],
  21. arg2 = arg.substring(1);
  22. for (var i = 0, ii = split.length; i < ii; i++) {
  23. field = split[i].match(/(.*?)=(.*)/);
  24. // TODO: regex should be able to handle this.
  25. if ( ! field) {
  26. field = [split[i], split[i], ''];
  27. }
  28. if (field[1].replace(/\s/g, '') !== '') {
  29. field[2] = _d(field[2] || '');
  30. // If we have a match just return it right away.
  31. if (arg2 === field[1]) { return field[2]; }
  32. // Check for array pattern.
  33. tmp = field[1].match(/(.*)\[([0-9]+)\]/);
  34. if (tmp) {
  35. params[tmp[1]] = params[tmp[1]] || [];
  36. params[tmp[1]][tmp[2]] = field[2];
  37. }
  38. else {
  39. params[field[1]] = field[2];
  40. }
  41. }
  42. }
  43. if (sptr === arg) { return params; }
  44. return params[arg2];
  45. }
  46. var _l = {}, tmp, tmp2;
  47. if (arg === 'tld?') { return _t(); }
  48. url = url || window.location.toString();
  49. if ( ! arg) { return url; }
  50. arg = arg.toString();
  51. if (tmp = url.match(/^mailto:([^\/].+)/)) {
  52. _l.protocol = 'mailto';
  53. _l.email = tmp[1];
  54. }
  55. else {
  56. // Ignore Hashbangs.
  57. if (tmp = url.match(/(.*?)\/#\!(.*)/)) {
  58. url = tmp[1] + tmp[2];
  59. }
  60. // Hash.
  61. if (tmp = url.match(/(.*?)#(.*)/)) {
  62. _l.hash = tmp[2];
  63. url = tmp[1];
  64. }
  65. // Return hash parts.
  66. if (_l.hash && arg.match(/^#/)) { return _f(arg, _l.hash); }
  67. // Query
  68. if (tmp = url.match(/(.*?)\?(.*)/)) {
  69. _l.query = tmp[2];
  70. url = tmp[1];
  71. }
  72. // Return query parts.
  73. if (_l.query && arg.match(/^\?/)) { return _f(arg, _l.query); }
  74. // Protocol.
  75. if (tmp = url.match(/(.*?)\:?\/\/(.*)/)) {
  76. _l.protocol = tmp[1].toLowerCase();
  77. url = tmp[2];
  78. }
  79. // Path.
  80. if (tmp = url.match(/(.*?)(\/.*)/)) {
  81. _l.path = tmp[2];
  82. url = tmp[1];
  83. }
  84. // Clean up path.
  85. _l.path = (_l.path || '').replace(/^([^\/])/, '/$1');
  86. // Return path parts.
  87. if (arg.match(/^[\-0-9]+$/)) { arg = arg.replace(/^([^\/])/, '/$1'); }
  88. if (arg.match(/^\//)) { return _i(arg, _l.path.substring(1)); }
  89. // File.
  90. tmp = _i('/-1', _l.path.substring(1));
  91. if (tmp && (tmp = tmp.match(/(.*?)\.([^.]+)$/))) {
  92. _l.file = tmp[0];
  93. _l.filename = tmp[1];
  94. _l.fileext = tmp[2];
  95. }
  96. // Port.
  97. if (tmp = url.match(/(.*)\:([0-9]+)$/)) {
  98. _l.port = tmp[2];
  99. url = tmp[1];
  100. }
  101. // Auth.
  102. if (tmp = url.match(/(.*?)@(.*)/)) {
  103. _l.auth = tmp[1];
  104. url = tmp[2];
  105. }
  106. // User and pass.
  107. if (_l.auth) {
  108. tmp = _l.auth.match(/(.*)\:(.*)/);
  109. _l.user = tmp ? tmp[1] : _l.auth;
  110. _l.pass = tmp ? tmp[2] : undefined;
  111. }
  112. // Hostname.
  113. _l.hostname = url.toLowerCase();
  114. // Return hostname parts.
  115. if (arg.charAt(0) === '.') { return _i(arg, _l.hostname); }
  116. // Domain, tld and sub domain.
  117. if (_t()) {
  118. tmp = _l.hostname.match(_t());
  119. if (tmp) {
  120. _l.tld = tmp[3];
  121. _l.domain = tmp[2] ? tmp[2] + '.' + tmp[3] : undefined;
  122. _l.sub = tmp[1] || undefined;
  123. }
  124. }
  125. // Set port and protocol defaults if not set.
  126. _l.port = _l.port || (_l.protocol === 'https' ? '443' : '80');
  127. _l.protocol = _l.protocol || (_l.port === '443' ? 'https' : 'http');
  128. }
  129. // Return arg.
  130. if (arg in _l) { return _l[arg]; }
  131. // Return everything.
  132. if (arg === '{}') { return _l; }
  133. // Default to undefined for no match.
  134. return undefined;
  135. };