Skip to main content
Code Review

Return to Question

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

I tried to implement a definitive, reliable URL query string parser that handles every corner case:

  • it tries to be efficient by avoiding regex
  • it takes full URLs or just query strings (as long as the query string begins with a question mark)
  • it ignores the hash value
  • it handles multiple equal parameter names
  • it handles parameter names that equal built-in JavaScript methods and keywords

What do you think - did I miss something?

function parseURLParams(url) {
 if (url === null) return;
 var queryStart = url.indexOf("?") + 1,
 queryEnd = url.indexOf("#") + 1 || url.length + 1,
 query = url.slice(queryStart, queryEnd - 1);
 if (query === url || query === "") return;
 var params = {}, 
 nvPairs = query.replace(/\+/g, " ").split("&");
 for (var i=0; i<nvPairs.length; i++) {
 var nv = nvPairs[i],
 eq = nv.indexOf("=") + 1 || nv.length + 1,
 n = decodeURIComponent( nv.slice(0, eq - 1) ),
 v = decodeURIComponent( nv.slice(eq) );
 if ( n !== "" ) {
 if ( !Object.prototype.hasOwnProperty.call(params, n) ) {
 params[n] = [];
 }
 params[n].push(v);
 }
 }
 return params;
}

It returns an object of arrays for parsed URLs with query strings and undefined if a query string could not be identified.

I used this in an answer over at SO answer over at SO.

I tried to implement a definitive, reliable URL query string parser that handles every corner case:

  • it tries to be efficient by avoiding regex
  • it takes full URLs or just query strings (as long as the query string begins with a question mark)
  • it ignores the hash value
  • it handles multiple equal parameter names
  • it handles parameter names that equal built-in JavaScript methods and keywords

What do you think - did I miss something?

function parseURLParams(url) {
 if (url === null) return;
 var queryStart = url.indexOf("?") + 1,
 queryEnd = url.indexOf("#") + 1 || url.length + 1,
 query = url.slice(queryStart, queryEnd - 1);
 if (query === url || query === "") return;
 var params = {}, 
 nvPairs = query.replace(/\+/g, " ").split("&");
 for (var i=0; i<nvPairs.length; i++) {
 var nv = nvPairs[i],
 eq = nv.indexOf("=") + 1 || nv.length + 1,
 n = decodeURIComponent( nv.slice(0, eq - 1) ),
 v = decodeURIComponent( nv.slice(eq) );
 if ( n !== "" ) {
 if ( !Object.prototype.hasOwnProperty.call(params, n) ) {
 params[n] = [];
 }
 params[n].push(v);
 }
 }
 return params;
}

It returns an object of arrays for parsed URLs with query strings and undefined if a query string could not be identified.

I used this in an answer over at SO.

I tried to implement a definitive, reliable URL query string parser that handles every corner case:

  • it tries to be efficient by avoiding regex
  • it takes full URLs or just query strings (as long as the query string begins with a question mark)
  • it ignores the hash value
  • it handles multiple equal parameter names
  • it handles parameter names that equal built-in JavaScript methods and keywords

What do you think - did I miss something?

function parseURLParams(url) {
 if (url === null) return;
 var queryStart = url.indexOf("?") + 1,
 queryEnd = url.indexOf("#") + 1 || url.length + 1,
 query = url.slice(queryStart, queryEnd - 1);
 if (query === url || query === "") return;
 var params = {}, 
 nvPairs = query.replace(/\+/g, " ").split("&");
 for (var i=0; i<nvPairs.length; i++) {
 var nv = nvPairs[i],
 eq = nv.indexOf("=") + 1 || nv.length + 1,
 n = decodeURIComponent( nv.slice(0, eq - 1) ),
 v = decodeURIComponent( nv.slice(eq) );
 if ( n !== "" ) {
 if ( !Object.prototype.hasOwnProperty.call(params, n) ) {
 params[n] = [];
 }
 params[n].push(v);
 }
 }
 return params;
}

It returns an object of arrays for parsed URLs with query strings and undefined if a query string could not be identified.

I used this in an answer over at SO.

added 35 characters in body
Source Link
Tomalak
  • 276
  • 1
  • 6

I tried to implement a definitive, reliable URL query string parser that handles every corner case:

  • it tries to be efficient by avoiding regex
  • it takes full URLs or just query strings (as long as the query string begins with a question mark)
  • it ignores the hash value
  • it handles multiple equal parameter names
  • it handles parameter names that equal built-in JavaScript methods and keywords

What do you think - did I miss something?

function parseURLParams(url) {
 if (url === null) return;
 var queryStart = url.indexOf("?") + 1,
 queryEnd = url.indexOf("#") + 1 || url.length + 1,
 query = url.slice(queryStart, queryEnd - 1);
 if (query === url || query === "") return;
 var params = {}, 
 nvPairs = query.replace(/\+/g, " ").split("&");
 for (var i=0; i<nvPairs.length; i++) {
 var nv = nvPairs[i],
 eq = nv.indexOf("=") + 1 || nv.length + 1,
 n = decodeURIComponent( nv.slice(0, eq - 1) ),
 v = decodeURIComponent( nv.slice(eq) );
 if ( n !== "" ) {
 if ( !Object.prototype.hasOwnProperty.call(params, n) ) {
 params[n] = [];
 }
 params[n].push(v);
 }
 }
 return params;
}

It returns an object of arrays for parsed URLs with query strings and undefined if a query string could not be identified.

I used this in an answer over at SO.

I tried to implement a definitive, reliable URL query string parser that handles every corner case:

  • it tries to be efficient by avoiding regex
  • it takes full URLs or just query strings (as long as the query string begins with a question mark)
  • it ignores the hash value
  • it handles multiple equal parameter names
  • it handles parameter names that equal built-in JavaScript methods and keywords

What do you think - did I miss something?

function parseURLParams(url) {
 var queryStart = url.indexOf("?") + 1,
 queryEnd = url.indexOf("#") + 1 || url.length + 1,
 query = url.slice(queryStart, queryEnd - 1);
 if (query === url || query === "") return;
 var params = {}, 
 nvPairs = query.replace(/\+/g, " ").split("&");
 for (var i=0; i<nvPairs.length; i++) {
 var nv = nvPairs[i],
 eq = nv.indexOf("=") + 1 || nv.length + 1,
 n = decodeURIComponent( nv.slice(0, eq - 1) ),
 v = decodeURIComponent( nv.slice(eq) );
 if ( n !== "" ) {
 if ( !Object.prototype.hasOwnProperty.call(params, n) ) {
 params[n] = [];
 }
 params[n].push(v);
 }
 }
 return params;
}

It returns an object of arrays for parsed URLs with query strings and undefined if a query string could not be identified.

I used this in an answer over at SO.

I tried to implement a definitive, reliable URL query string parser that handles every corner case:

  • it tries to be efficient by avoiding regex
  • it takes full URLs or just query strings (as long as the query string begins with a question mark)
  • it ignores the hash value
  • it handles multiple equal parameter names
  • it handles parameter names that equal built-in JavaScript methods and keywords

What do you think - did I miss something?

function parseURLParams(url) {
 if (url === null) return;
 var queryStart = url.indexOf("?") + 1,
 queryEnd = url.indexOf("#") + 1 || url.length + 1,
 query = url.slice(queryStart, queryEnd - 1);
 if (query === url || query === "") return;
 var params = {}, 
 nvPairs = query.replace(/\+/g, " ").split("&");
 for (var i=0; i<nvPairs.length; i++) {
 var nv = nvPairs[i],
 eq = nv.indexOf("=") + 1 || nv.length + 1,
 n = decodeURIComponent( nv.slice(0, eq - 1) ),
 v = decodeURIComponent( nv.slice(eq) );
 if ( n !== "" ) {
 if ( !Object.prototype.hasOwnProperty.call(params, n) ) {
 params[n] = [];
 }
 params[n].push(v);
 }
 }
 return params;
}

It returns an object of arrays for parsed URLs with query strings and undefined if a query string could not be identified.

I used this in an answer over at SO.

added 410 characters in body; deleted 24 characters in body
Source Link
Tomalak
  • 276
  • 1
  • 6

I tried to implement a definitive, reliable URL query string parser that handles every corner case:

  • it tries to be efficient by avoiding regex
  • it takes full URLs or just query strings (as long as the query string begins with a question mark)
  • it ignores the hash value
  • it handles multiple equal parameter names
  • it handles parameter names that equal built-in JavaScript methods and keywords

What do you think - did I miss something?

function parseURLParams(url) {
 var queryStart = url.indexOf("?") + 1,
 queryEnd = url.indexOf("#") + 1 || url.length + 1,
 query = url.slice(queryStart, queryEnd - 1);
 if (query === url || query === "") return;
 var params = {}, 
 nvPairs = query.replace(/\+/g, " ").split("&");
 for (var i=0; i<nvPairs.length; i++) {
 var nv = nvPairs[i],
 eq = nv.indexOf("=") + 1 || nv.length + 1,
 n = decodeURIComponent( nv.slice(0, eq - 1) ),
 v = decodeURIComponent( nv.slice(eq) );console.log(n, ": ", v);
 if ( n !== "" ) {
 if ( !Object.prototype.hasOwnProperty.call(params, n) ) {
 params[n] = [];
 }
 params[n].push(v);
 }
 }
 return params;
}

It returns an object of arrays for parsed URLs with query strings and undefined if a query string could not be identified.

I used this in an answer over at SO.

What do you think - did I miss something?

function parseURLParams(url) {
 var queryStart = url.indexOf("?") + 1,
 queryEnd = url.indexOf("#") + 1 || url.length + 1,
 query = url.slice(queryStart, queryEnd - 1);
 if (query === url || query === "") return;
 var params = {}, 
 nvPairs = query.replace(/\+/g, " ").split("&");
 for (var i=0; i<nvPairs.length; i++) {
 var nv = nvPairs[i],
 eq = nv.indexOf("=") + 1 || nv.length + 1,
 n = decodeURIComponent( nv.slice(0, eq - 1) ),
 v = decodeURIComponent( nv.slice(eq) );console.log(n, ": ", v);
 if ( n !== "" ) {
 if ( !Object.prototype.hasOwnProperty.call(params, n) ) {
 params[n] = [];
 }
 params[n].push(v);
 }
 }
 return params;
}

It returns an object of arrays for parsed URLs with query strings and undefined if a query string could not be identified.

I used this in an answer over at SO.

I tried to implement a definitive, reliable URL query string parser that handles every corner case:

  • it tries to be efficient by avoiding regex
  • it takes full URLs or just query strings (as long as the query string begins with a question mark)
  • it ignores the hash value
  • it handles multiple equal parameter names
  • it handles parameter names that equal built-in JavaScript methods and keywords

What do you think - did I miss something?

function parseURLParams(url) {
 var queryStart = url.indexOf("?") + 1,
 queryEnd = url.indexOf("#") + 1 || url.length + 1,
 query = url.slice(queryStart, queryEnd - 1);
 if (query === url || query === "") return;
 var params = {}, 
 nvPairs = query.replace(/\+/g, " ").split("&");
 for (var i=0; i<nvPairs.length; i++) {
 var nv = nvPairs[i],
 eq = nv.indexOf("=") + 1 || nv.length + 1,
 n = decodeURIComponent( nv.slice(0, eq - 1) ),
 v = decodeURIComponent( nv.slice(eq) );
 if ( n !== "" ) {
 if ( !Object.prototype.hasOwnProperty.call(params, n) ) {
 params[n] = [];
 }
 params[n].push(v);
 }
 }
 return params;
}

It returns an object of arrays for parsed URLs with query strings and undefined if a query string could not be identified.

I used this in an answer over at SO.

added 126 characters in body
Source Link
Tomalak
  • 276
  • 1
  • 6
Loading
Tweeted twitter.com/#!/StackCodeReview/status/80289501230022657
Source Link
Tomalak
  • 276
  • 1
  • 6
Loading
default

AltStyle によって変換されたページ (->オリジナル) /