tildefriends/core/form.js

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;