Instantly share code, notes, and snippets.

Embed
What would you like to do?
JS utility function that: - Breaks down url to an object with accessible properties: protocol, parameters object, host, hash, etc... - Converts url parameters to key/value pairs - Convert parameter numeric values to their base types instead of strings - Store multiple values of a parameter in an array - Unescape parameter values
function urlObject(options) {
"use strict";
/*global window, document*/
var url_search_arr,
option_key,
i,
urlObj,
get_param,
key,
val,
url_query,
url_get_params = {},
a = document.createElement('a'),
default_options = {
'url': window.location.href,
'unescape': true,
'convert_num': true
};
if (typeof options !== "object") {
options = default_options;
} else {
for (option_key in default_options) {
if (default_options.hasOwnProperty(option_key)) {
if (options[option_key] === undefined) {
options[option_key] = default_options[option_key];
}
}
}
}
a.href = options.url;
url_query = a.search.substring(1);
url_search_arr = url_query.split('&');
if (url_search_arr[0].length > 1) {
for (i = 0; i < url_search_arr.length; i += 1) {
get_param = url_search_arr[i].split("=");
if (options.unescape) {
key = decodeURI(get_param[0]);
val = decodeURI(get_param[1]);
} else {
key = get_param[0];
val = get_param[1];
}
if (options.convert_num) {
if (val.match(/^\d+$/)) {
val = parseInt(val, 10);
} else if (val.match(/^\d+\.\d+$/)) {
val = parseFloat(val);
}
}
if (url_get_params[key] === undefined) {
url_get_params[key] = val;
} else if (typeof url_get_params[key] === "string") {
url_get_params[key] = [url_get_params[key], val];
} else {
url_get_params[key].push(val);
}
get_param = [];
}
}
urlObj = {
protocol: a.protocol,
hostname: a.hostname,
host: a.host,
port: a.port,
hash: a.hash.substr(1),
pathname: a.pathname,
search: a.search,
parameters: url_get_params
};
return urlObj;
}
@jadjoubran

This comment has been minimized.

Show comment
Hide comment
@jadjoubran

jadjoubran May 20, 2013

I suggested an edit, kindly check it

jadjoubran commented May 20, 2013

I suggested an edit, kindly check it

@aymanfarhat

This comment has been minimized.

Show comment
Hide comment
@aymanfarhat

aymanfarhat May 21, 2013

Checked it, makes more sense. I'll update mine, thanks for the suggestion!

Owner

aymanfarhat commented May 21, 2013

Checked it, makes more sense. I'll update mine, thanks for the suggestion!

@bpletzer

This comment has been minimized.

Show comment
Hide comment
@bpletzer

bpletzer Jun 11, 2013

Needed an implementation, stumbled across yours. Thanks!

Don't know how to do a pull request, but I have another feature- hashParams (see my fork)

greetings

p.s.: also, I actually I needed it generic, so I changed the interface to accept url before options

bpletzer commented Jun 11, 2013

Needed an implementation, stumbled across yours. Thanks!

Don't know how to do a pull request, but I have another feature- hashParams (see my fork)

greetings

p.s.: also, I actually I needed it generic, so I changed the interface to accept url before options

@aymanfarhat

This comment has been minimized.

Show comment
Hide comment
@aymanfarhat

aymanfarhat Jun 12, 2013

Just checked your addition, cool work! Glad that you found the script helpful :)

Owner

aymanfarhat commented Jun 12, 2013

Just checked your addition, cool work! Glad that you found the script helpful :)

@ryankane

This comment has been minimized.

Show comment
Hide comment
@ryankane

ryankane Jan 15, 2015

How do you handle negative numbers?:

Use Case

Test Input

urlquery = "?id=%2D42"

Expected Output

parameters : { 'id' : -42 }

Solution

function normalizeParamVal(val, decode, convert) { 
    if (decode) {
        val = decodeURI(val);
    }
    if (convert) {
        if (val.match(/^-?\d+$/)) {
            return parseInt(val, 10);
        } else if (val.match(/^-?\d+\.\d+$/)) {
            return parseFloat(val);
        }
    }
    return val;
}

console.log(normalizeParamVal("%2D42", true, true)); // -42

ryankane commented Jan 15, 2015

How do you handle negative numbers?:

Use Case

Test Input

urlquery = "?id=%2D42"

Expected Output

parameters : { 'id' : -42 }

Solution

function normalizeParamVal(val, decode, convert) { 
    if (decode) {
        val = decodeURI(val);
    }
    if (convert) {
        if (val.match(/^-?\d+$/)) {
            return parseInt(val, 10);
        } else if (val.match(/^-?\d+\.\d+$/)) {
            return parseFloat(val);
        }
    }
    return val;
}

console.log(normalizeParamVal("%2D42", true, true)); // -42
@csanviriya

This comment has been minimized.

Show comment
Hide comment
@csanviriya

csanviriya Mar 9, 2017

Could you be so kind as to license your snippets with some sorta no nonsense license for edits and reuse? This one is really helpful.

csanviriya commented Mar 9, 2017

Could you be so kind as to license your snippets with some sorta no nonsense license for edits and reuse? This one is really helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment