Created
March 19, 2016 22:05
-
-
Save racztiborzoltan/b54fd96fbb77979ea1a4 to your computer and use it in GitHub Desktop.
object to query string and query string to object conversation
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
/** | |
* Convert Object to URL query string | |
* | |
* Usage: | |
* var object = {param1:123,param2:'something',param3:'another',param4:null,param5:'',bla:{param10:'I am nested!',array2:[20,'test',40.56],ja:{param6:'really_nested'}},test_array:[10,'foobar',30.5]}; | |
* var query = object.toQueryString(); | |
* Result: "param1=123¶m2=something¶m3=another¶m4¶m5=&bla[param10]=I%20am%20nested!&bla[array2][]=20&bla[array2][]=test&bla[array2][]=40.56&bla[ja][param6]=really_nested&test_array[]=10&test_array[]=foobar&test_array[]=30.5" | |
* | |
* @version 1.0 | |
* | |
* @param Array keys only for recursion!!! | |
* @param Array queryParts only for recursion!!! | |
* @return string | |
*/ | |
Object.prototype.toQueryString = function(keys, queryParts) { | |
keys = keys || []; | |
queryParts = queryParts || []; | |
for ( var prop in this) { | |
if (!this.hasOwnProperty(prop)) { | |
continue; | |
} | |
var temp = keys.slice(0); | |
var value = this[prop]; | |
var temp_keys = keys.slice(0); | |
temp_keys.push(prop); | |
temp_keys.forEach(function(value, index, array) { | |
array[index] = index === 0 ? value : '['+value+']'; | |
}); | |
if (value instanceof Array) { | |
value.forEach(function(v, k, a){ | |
queryParts.push( temp_keys.join('') + '[]' + (v === null ? '' : '=' + encodeURIComponent(v) )); | |
}); | |
} else if (value instanceof Object) { | |
value.toQueryString(temp_keys, queryParts); | |
} else { | |
queryParts.push( temp_keys.join('') + (value === null ? '' : '=' + encodeURIComponent(value) )); | |
} | |
} | |
return queryParts.join('&'); | |
} |
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
/** | |
* String convert to Object | |
* | |
* @return Object | |
*/ | |
String.prototype.toObject = function(){ | |
var out = {}; | |
var parts = this.indexOf('&') !== -1 ? this.split('&') : this.split('&'); | |
parts.forEach(function(value, index, array) { | |
value = value.split('='); | |
if (value.length === 1) { | |
value[1] = null; | |
} | |
var keys_str = value[0]; | |
if (keys_str == '') { | |
return; | |
} | |
value = value[1]; | |
// Convert to boolean or number if possible: | |
if (['true', 'false'].indexOf(value) > -1) { | |
value = value === 'true' | |
} else if (parseFloat(value) !== NaN && parseFloat(value) == value) { | |
value = parseFloat(value); | |
} | |
var keys = keys_str.replace(/^([^\[]+)/, '[$1]').slice(1, -1).split(']['); | |
var temp_out = out; | |
keys.forEach(function(k, i, a){ | |
if (i === a.length - 1) { | |
temp_out[k] = value; | |
} else { | |
if (i < a.length - 1 && a[i+1] === '') { | |
temp_out[k] = temp_out[k] instanceof Array ? temp_out[k] : []; | |
temp_out[k].push(value); | |
} else { | |
temp_out[k] = temp_out[k] instanceof Object ? temp_out[k] : {}; | |
} | |
temp_out = temp_out[k]; | |
} | |
}); | |
}); | |
return out; | |
}; |
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
var test = { | |
param1: 123, | |
param2: 'something', | |
param3: 'another', | |
param4: null, | |
param5: '', | |
param6: true, | |
param7: false, | |
bla: { | |
param10: 'I am nested!', | |
array2: [20, 'test', 40.56], | |
ja: { | |
param6: 'really_n=ested' | |
} | |
}, | |
test_array: [10,'foobar',30.5] | |
}; | |
var test_value = test.toQueryString() | |
console.log(test_value); | |
console.log(test_value.toObject()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment