Skip to content

Commit

Permalink
Merge 00dd29f into 1d454bd
Browse files Browse the repository at this point in the history
  • Loading branch information
jgarciabengochea committed Dec 15, 2018
2 parents 1d454bd + 00dd29f commit 4b78279
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,33 @@ exports.getAllRegexMatches = function (string, regex) {
if (index > 0) return url.substr(0, index);
return url;
} */

exports.urlSlice = function (url) {
var result = {
url : url
};
for (var i = 0, len = url.length; i < len; i++) {
if( url[i] === '?' ) {
result.url = url.substr(0, i)
result.queryStr = url.substr(i+1);
if ( url[i] === '?' ) {
var fragmentIndex = url.indexOf('#', i + 1);
if (fragmentIndex < 0) {
fragmentIndex = len;
} else {
result.hashStr = url.substring(fragmentIndex + 1);
}
result.url = url.substring(0, i);
result.queryStr = url.substring(i + 1, fragmentIndex);
break;
}else if( url[i] === '#' ) {
result.url = url.substr(0, i)
result.hashStr = url.substr(i+1);
} else if (url[i] === '#') {
result.url = url.substring(0, i)
result.hashStr = url.substring(i + 1);
break;
}else if( url[i] === ';' ) {
result.url = url.substr(0, i)
result.queryStr = url.substr(i+1);
} else if (url[i] === ';') {
result.url = url.substring(0, i)
result.queryStr = url.substring(i + 1);
break;
}
}
}
return result;
}

exports.urlSlice("/this/is/sample?q1=val&q2=val3#q2=val2&q3=val4");
51 changes: 51 additions & 0 deletions tests/util_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
var util = require('../src/util');

describe("urlSlice", function() {
var { urlSlice } = util;

it("should handle urls with query string", function() {

var result = urlSlice("/this/is/sample?q1=val&q2=val3");
expect(result.url).toEqual("/this/is/sample");
expect(result.queryStr).toEqual("q1=val&q2=val3");

});

it("should handle urls with hash", function() {

var result = urlSlice("/this/is/sample#q1=val&q2=val3");
expect(result.url).toEqual("/this/is/sample");
expect(result.hashStr).toEqual("q1=val&q2=val3");

});

it("should handle urls with both query string and hash", function() {

var result = urlSlice("/this/is/sample?q1=val&q2=val3#q2=val2&q3=val4");
expect(result.url).toEqual("/this/is/sample");
expect(result.queryStr).toEqual("q1=val&q2=val3");
expect(result.hashStr).toEqual("q2=val2&q3=val4");

});

it("should only slice the hash string after the first fragment identifier if there are multiple", function() {

var result = urlSlice("/this/is/sample?q1=val&q2=val3#q1=val#q2=val3");
expect(result.hashStr).toEqual("q1=val#q2=val3");

});

it("should only slice the query string after the first query params identifier if there are multiple", function() {

var result = urlSlice("/this/is/sample?q1=val&q2=val3?q1=val#q2=val3");
expect(result.queryStr).toEqual("q1=val&q2=val3?q1=val");

});

it("should slice the hash string correctly if a query params identifier appears in the fragment string", function() {

var result = urlSlice("/this/is/sample#q1=val&q2=val3&q1=val?q2=val3");
expect(result.hashStr).toEqual("q1=val&q2=val3&q1=val?q2=val3");

});
});

0 comments on commit 4b78279

Please sign in to comment.