tildefriends/core/form.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

45 lines
850 B
JavaScript
Raw Normal View History

/**
* TODOC
2024-02-22 15:23:39 -05:00
* @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
2024-02-22 15:23:39 -05:00
* @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 };