Last active
September 30, 2015 15:53
-
-
Save WillSquire/dae271b733a72c8f3ad4 to your computer and use it in GitHub Desktop.
Url query parser (encode and decoding should be done separately)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* URI Query Parse | |
* | |
* By Will Squire <will_squire@hotmail.co.uk> | |
*/ | |
/** | |
* Parses uri query string into a key/value | |
* array. | |
* | |
* Takes the query part of a uri only (anything after and including the '?' | |
* character) | |
* | |
* @param {string} uri_query | |
* @returns {Array} | |
*/ | |
export function uriQueryParse(uri_query) { | |
let query_object = []; | |
uri_query.replace('?', '').split('&').forEach(function (val) { | |
let split = val.split("=", 2); | |
query_object[split[0]] = split[1]; | |
}); | |
return query_object; | |
} | |
/** | |
* Updates a uri query string with new parameters. | |
* | |
* Pass in the uri query part of a uri only (anything after and including the '?' | |
* character) | |
* | |
* @param {string} uri_query | |
* @param {Object<string,string>} params | |
* @returns {string} | |
*/ | |
export function uriQueryUpdate(uri_query, params) { | |
let current_params = uriQueryParse(uri_query); | |
// Build url from params | |
for (let param in params) { | |
if (params.hasOwnProperty(param)) | |
current_params[param] = params[param]; | |
} | |
// Return stringified uri query | |
return uriQueryStringify(current_params); | |
} | |
/** | |
* Parses an object (associative array) containing key/value pairs | |
* into uri query format. | |
* | |
* @param {Object<string,string>} params | |
* @returns {string} | |
*/ | |
export function uriQueryStringify(params) { | |
let uri_query = '?'; // Add params indicator | |
// Build uri from params | |
for (let param in params) { | |
if (params.hasOwnProperty(param)) { | |
// If value is array, put multiple values under same key with '[]' suffix to key | |
if (Array.isArray(params[param])) { | |
params[param].forEach(function(element, index) { | |
uri_query += param + '[' + index + ']=' + element + '&'; | |
}); | |
} | |
// If object, build assosative array with '[property_name]' suffix to array key | |
else if (typeof params[param] === 'object') { | |
for (let object_property in params[param]) { | |
if (params[param].hasOwnProperty(object_property)) | |
uri_query += param + '[' + object_property + ']=' + params[param][object_property] + '&'; | |
} | |
} | |
else | |
uri_query += param + '=' + params[param] + '&'; | |
} | |
} | |
// Remove '&' off final param in string | |
return uri_query.slice(0, uri_query.length - 1); | |
} | |
/** | |
* Strips the query part of a uri. Anything after (and including) | |
* the '?' character will be removed from the string. | |
* | |
* @param {string} uri | |
* @returns {*} | |
*/ | |
export function uriQueryStrip(uri) { | |
let query_start_index = uri.indexOf('?'); | |
if (query_start_index > -1) | |
uri = uri.slice(0, query_start_index); | |
return uri; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* URI Query Parse | |
* | |
* By Will Squire <will_squire@hotmail.co.uk> | |
*/ | |
/** | |
* Parses uri query string into a key/value | |
* array. | |
* | |
* Takes the query part of a uri only (anything after and including the '?' | |
* character) | |
* | |
* @param {string} uri_query | |
* @returns {Array} | |
*/ | |
function uriQueryParse(uri_query) { | |
var query_object = []; | |
uri_query.replace('?', '').split('&').forEach(function (val) { | |
var split = val.split("=", 2); | |
query_object[split[0]] = split[1]; | |
}); | |
return query_object; | |
} | |
/** | |
* Updates a uri query string with new parameters. | |
* | |
* Pass in the uri query part of a uri only (anything after and including the '?' | |
* character) | |
* | |
* @param {string} uri_query | |
* @param {Object<string,string>} params | |
* @returns {string} | |
*/ | |
function uriQueryUpdate(uri_query, params) { | |
var current_params = uriQueryParse(uri_query); | |
// Build url from params | |
for (var param in params) { | |
if (params.hasOwnProperty(param)) | |
current_params[param] = params[param]; | |
} | |
// Return stringified uri query | |
return uriQueryStringify(current_params); | |
} | |
/** | |
* Parses an object (associative array) containing key/value pairs | |
* into uri query format. | |
* | |
* @param {Object<string,string>} params | |
* @returns {string} | |
*/ | |
function uriQueryStringify(params) { | |
var uri_query = '?'; // Add params indicator | |
// Build uri from params | |
for (var param in params) { | |
if (params.hasOwnProperty(param)) { | |
// If value is array, put multiple values under same key with '[]' suffix to key | |
if (Array.isArray(params[param])) { | |
params[param].forEach(function(element, index) { | |
uri_query += param + '[' + index + ']=' + element + '&'; | |
}); | |
} | |
// If object, build assosative array with '[property_name]' suffix to array key | |
else if (typeof params[param] === 'object') { | |
for (var object_property in params[param]) { | |
if (params[param].hasOwnProperty(object_property)) | |
uri_query += param + '[' + object_property + ']=' + params[param][object_property] + '&'; | |
} | |
} | |
else | |
uri_query += param + '=' + params[param] + '&'; | |
} | |
} | |
// Remove '&' off final param in string | |
return uri_query.slice(0, uri_query.length - 1); | |
} | |
/** | |
* Strips the query part of a uri. Anything after (and including) | |
* the '?' character will be removed from the string. | |
* | |
* @param {string} uri | |
* @returns {*} | |
*/ | |
function uriQueryStrip(uri) { | |
var query_start_index = uri.indexOf('?'); | |
if (query_start_index > -1) | |
uri = uri.slice(0, query_start_index); | |
return uri; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment