Skip to content

Commit

Permalink
Update regex splits to use regex matches for support in ie8.
Browse files Browse the repository at this point in the history
  • Loading branch information
websanova committed Sep 17, 2015
1 parent 6b679fa commit 6489e9e
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 49 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "js-url",
"version": "2.0.1",
"version": "2.0.2",
"description": "A simple, lightweight url parser for JavaScript (~1.7 Kb minified, ~0.7Kb gzipped).",
"main": "url.js",
"repository": {
Expand Down
25 changes: 17 additions & 8 deletions tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ else {
});
}

test('mailto', function() {
deepEqual( window.url( 'email', 'mailto:[email protected]' ), '[email protected]' );
deepEqual( window.url( 'protocol', 'mailto:[email protected]' ), 'mailto' );
deepEqual( window.url( 'email', 'mailto:/[email protected]' ), undefined );
deepEqual( window.url( 'email', 'mailto://[email protected]' ), undefined );
deepEqual( window.url( 'protocol', 'mailto:/[email protected]' ), 'http' );
deepEqual( window.url( 'mailto:', 'mailto:/[email protected]' ), undefined );
});

test('hostname', function() {
deepEqual( window.url( 'hostname', url ), 'www.domain.com' );
deepEqual( window.url( 'hostname', urlIp ), '1.2.3.4' );
Expand All @@ -101,14 +110,7 @@ test('domain parts', function() {

test('auth', function() {
deepEqual( window.url( 'auth', url ), 'rob:abcd1234' );
});

test('user', function() {
deepEqual( window.url( 'user', url ), 'rob' );
deepEqual( window.url( 'email', 'mailto:[email protected]' ), '[email protected]' );
});

test('pass', function() {
deepEqual( window.url( 'pass', url ), 'abcd1234' );
});

Expand All @@ -124,6 +126,7 @@ test('port', function() {
deepEqual( window.url( 'port', 'http://domain.com:443' ), '443' );
deepEqual( window.url( 'port', 'http://domain.com' ), '80' );
deepEqual( window.url( 'port', 'https://domain.com' ), '443' );
deepEqual( window.url( 'port', '[email protected]' ), '80' );
});

test('protocol', function() {
Expand All @@ -135,7 +138,7 @@ test('protocol', function() {
deepEqual( window.url( 'protocol', 'domain.com:443' ), 'https' );
deepEqual( window.url( 'protocol', 'https://domain.com:443' ), 'https' );
deepEqual( window.url( 'protocol', 'https://domain.com:80' ), 'https' );
deepEqual( window.url( 'protocol', 'mailto:rob@websanova.com' ), 'mailto' );
deepEqual( window.url( 'protocol', 'rob@domain.com' ), 'http' );
});

test('path', function() {
Expand Down Expand Up @@ -166,6 +169,9 @@ test('file', function() {
deepEqual( window.url( 'file', url ), 'index.html' );
deepEqual( window.url( 'filename', url ), 'index' );
deepEqual( window.url( 'fileext', url ), 'html' );
deepEqual( window.url( 'file', 'http://domain.com' ), undefined );
deepEqual( window.url( 'filename', 'http://domain.com' ), undefined );
deepEqual( window.url( 'fileext', 'http://domain.com' ), undefined );
});

test('url parts', function() {
Expand Down Expand Up @@ -198,6 +204,8 @@ test('query string', function() {
deepEqual( window.url( '?key', 'http://domain.com?key=value=va?key2=value' ), 'value=va?key2=value');
deepEqual( window.url( '?poo', 'http://domain.com:400?poo=a:b' ), 'a:b' );
deepEqual( window.url( '?poo', 'http://domain.com:400? & & &' ), undefined );
deepEqual( window.url( '?test', 'http://domain.com?test=45#5' ), '45' );
deepEqual( window.url( '?test', 'http://domain.com?test=45?5' ), '45?5' );

deepEqual( window.url( '?field[0]', 'http://domain.com?field[0]=zero&firled[1]=one' ), 'zero' );
deepEqual( window.url( '?field', 'http://domain.com?field[0]=zero&field[1]=one&var=test' ), ['zero', 'one'] );
Expand All @@ -216,6 +224,7 @@ test('hash string', function() {
deepEqual( window.url( '#poo', 'http://domain.com#poo' ), '' );
deepEqual( window.url( '#poo', 'http://domain.com#' ), undefined );
deepEqual( window.url( '#poo', 'http://domain.com' ), undefined );
deepEqual( window.url( '#test', 'http://domain.com#test=45#5' ), '45#5' );

deepEqual( window.url( '#field[0]', 'http://domain.com#field[0]=zero&firled[1]=one' ), 'zero' );
deepEqual( window.url( '#field', 'http://domain.com#field[0]=zero&field[1]=one&var=test' ), ['zero', 'one'] );
Expand Down
2 changes: 1 addition & 1 deletion url-tld.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion url.jquery.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"websanova",
"url"
],
"version": "2.0.1",
"version": "2.0.2",
"author": {
"name": "Websanova",
"email": "[email protected]",
Expand Down
90 changes: 53 additions & 37 deletions url.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,29 @@ window.url = (function() {
arg2 = arg.substring(1);

for (var i in split) {
field = split[i].split(/=(.*)/);
field = split[i].match(/(.*?)=(.*)/);

if (field[0].replace(/\s/g, '') !== '') {
field[1] = _d(field[1] || '');
// TODO: regex should be able to handle this.
if ( ! field) {
field = [split[i], split[i], ''];
}

if (field[1].replace(/\s/g, '') !== '') {
field[2] = _d(field[2] || '');

// If we have a match just return it right away.
if (arg2 === field[0]) { return field[1]; }
if (arg2 === field[1]) { return field[2]; }

// Check for array pattern.
tmp = field[0].match(/(.*)\[([0-9]+)\]/);
tmp = field[1].match(/(.*)\[([0-9]+)\]/);

if (tmp) {
params[tmp[1]] = params[tmp[1]] || [];

params[tmp[1]][tmp[2]] = field[1];
params[tmp[1]][tmp[2]] = field[2];
}
else {
params[field[0]] = field[1];
params[field[1]] = field[2];
}
}
}
Expand All @@ -66,69 +71,80 @@ window.url = (function() {

arg = arg.toString();

if (url.match(/^mailto:[^\/]/)) {
if (tmp = url.match(/^mailto:([^\/].+)/)) {
_l.protocol = 'mailto';
_l.email = url.split(/mailto\:/)[1];
_l.email = tmp[1];
}
else {

// Anchor.
tmp = url.split(/#(.*)/);
_l.hash = tmp[1] ? tmp[1] : undefined;
// Hash.
if (tmp = url.match(/(.*?)#(.*)/)) {
_l.hash = tmp[2];
url = tmp[1];
}

// Return anchor parts.
// Return hash parts.
if (_l.hash && arg.match(/^#/)) { return _f(arg, _l.hash); }

// Query
tmp = tmp[0].split(/\?(.*)/);
_l.query = tmp[1] ? tmp[1] : undefined;
if (tmp = url.match(/(.*?)\?(.*)/)) {
_l.query = tmp[2];
url = tmp[1];
}

// Return query parts.
if (_l.query && arg.match(/^\?/)) { return _f(arg, _l.query); }

// Protocol.
tmp = tmp[0].split(/\:?\/\//);
_l.protocol = tmp[1] ? tmp[0].toLowerCase() : undefined;
if (tmp = url.match(/(.*?)\:?\/\/(.*)/)) {
_l.protocol = tmp[1].toLowerCase();
url = tmp[2];
}

// Path.
tmp = (tmp[1] ? tmp[1] : tmp[0]).split(/(\/.*)/);
_l.path = tmp[1] ? tmp[1] : '';
if (tmp = url.match(/(.*?)(\/.*)/)) {
_l.path = tmp[2];
url = tmp[1];
}

// Clean up path.
_l.path = _l.path.replace(/^([^\/])/, '/$1').replace(/\/$/, '');
_l.path = (_l.path || '').replace(/^([^\/])/, '/$1').replace(/\/$/, '');

// Return path parts.
if (arg.match(/^[\-0-9]+$/)) { arg = arg.replace(/^([^\/])/, '/$1'); }
if (arg.match(/^\//)) { return _i(arg, _l.path.substring(1)); }

// File.
tmp2 = _i('/-1', _l.path.substring(1));
tmp2 = tmp2.split(/\.(.*)/);

// Filename and fileext.
if (tmp2[1]) {
_l.file = tmp2[0] + '.' + tmp2[1];
_l.filename = tmp2[0];
_l.fileext = tmp2[1];
tmp = _i('/-1', _l.path.substring(1));

if (tmp && (tmp = tmp.match(/(.*?)\.(.*)/))) {
_l.file = tmp[0];
_l.filename = tmp[1];
_l.fileext = tmp[2];
}

// Port.
tmp = tmp[0].split(/\:([0-9]+)$/);
_l.port = tmp[1] ? tmp[1] : undefined;
if (tmp = url.match(/(.*)\:([0-9]+)$/)) {
_l.port = tmp[2];
url = tmp[1];
}

// Auth.
tmp = tmp[0].split(/@/);
_l.auth = tmp[1] ? tmp[0] : undefined;
if (tmp = url.match(/(.*?)@(.*)/)) {
_l.auth = tmp[1];
url = tmp[2];
}

// User and pass.
if (_l.auth) {
tmp2 = _l.auth.split(/\:(.*)/);
_l.user = tmp2[0];
_l.pass = tmp2[1];
tmp = _l.auth.match(/(.*)\:(.*)/);

_l.user = tmp ? tmp[1] : _l.auth;
_l.pass = tmp ? tmp[2] : undefined;
}

// Hostname.
_l.hostname = (tmp[1] ? tmp[1] : tmp[0]).toLowerCase();
_l.hostname = url.toLowerCase();

// Return hostname parts.
if (arg.charAt(0) === '.') { return _i(arg, _l.hostname); }
Expand Down
Loading

0 comments on commit 6489e9e

Please sign in to comment.