@chubbyts/chubbyts-decode-encode
Version:
A simple decode/encode solution for json / jsonx / url-encoded / xml / yaml.
39 lines (38 loc) • 1.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createUrlEncodedTypeDecoder = void 0;
const qs_1 = require("qs");
const index_1 = require("../index.cjs");
const decodeValue = (value) => {
if ((0, index_1.isObject)(value)) {
return Object.fromEntries(Object.entries(value).map(([subKey, subValue]) => [subKey, decodeValue(subValue)]));
}
if ((0, index_1.isArray)(value)) {
return value.map(decodeValue);
}
if (value === 'null') {
return null;
}
if (value === 'true') {
return true;
}
if (value === 'false') {
return false;
}
const integer = parseInt(value, 10);
if (!isNaN(integer) && integer.toString() === value) {
return integer;
}
const float = parseFloat(value);
if (!isNaN(float) && float.toString() === value) {
return float;
}
return value;
};
const createUrlEncodedTypeDecoder = () => {
return {
decode: (encodedData) => decodeValue((0, qs_1.parse)(encodedData, { depth: 100 })),
contentType: 'application/x-www-form-urlencoded',
};
};
exports.createUrlEncodedTypeDecoder = createUrlEncodedTypeDecoder;