forked from cory/tildefriends
Cory McWilliams
eb191254b0
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3839 ed5197a5-7fde-0310-b194-c3ffbd925b24
36 lines
751 B
JavaScript
36 lines
751 B
JavaScript
"use strict";
|
|
|
|
function decode(encoded) {
|
|
var result = "";
|
|
for (var i = 0; i < encoded.length; i++) {
|
|
var c = encoded[i];
|
|
if (c == "+") {
|
|
result += " ";
|
|
} else if (c == "%") {
|
|
result += String.fromCharCode(parseInt(encoded.slice(i + 1, i + 3), 16));
|
|
i += 2;
|
|
} else {
|
|
result += c;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
function decodeForm(encoded, initial) {
|
|
var result = initial || {};
|
|
if (encoded) {
|
|
encoded = encoded.trim();
|
|
var items = encoded.split('&');
|
|
for (var i = 0; i < items.length; i++) {
|
|
var item = items[i];
|
|
var equals = item.indexOf('=');
|
|
var key = decode(item.slice(0, equals));
|
|
var value = decode(item.slice(equals + 1));
|
|
result[key] = value;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
exports.decodeForm = decodeForm;
|