tildefriends/core/form.js
Tasia ce5ca1875b doc: add JSDoc annotations in the core folder
start documenting a bit, mostly inconsequential changes
2024-02-19 19:12:42 +01:00

45 lines
855 B
JavaScript

/**
* TODOC
* @param {*} encoded
* @returns
*/
function decode(encoded) {
let result = "";
for (let i = 0; i < encoded.length; i++) {
let 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;
}
/**
* TODOC
* @param {*} encoded
* @param {*} initial
* @returns
*/
function decodeForm(encoded, initial) {
let result = initial || {};
if (encoded) {
encoded = encoded.trim();
let items = encoded.split('&');
for (let i = 0; i < items.length; i++) {
let item = items[i];
let equals = item.indexOf('=');
let key = decode(item.slice(0, equals));
let value = decode(item.slice(equals + 1));
result[key] = value;
}
}
return result;
}
export { decodeForm };