@chubbyts/chubbyts-decode-encode
Version:
A simple decode/encode solution for json / jsonx / url-encoded / xml / yaml.
31 lines (30 loc) • 865 B
JavaScript
import { parse } from 'qs';
import { isArray, isObject } from '../data.js';
const decodeValue = (value) => {
if (isObject(value)) {
return Object.fromEntries(Object.entries(value).map(([subKey, subValue]) => [subKey, decodeValue(subValue)]));
}
if (isArray(value)) {
return value.map(decodeValue);
}
if (value === 'null') {
return null;
}
if (value === 'true') {
return true;
}
if (value === 'false') {
return false;
}
const float = parseFloat(value); // handles integers as well
if (float.toString() === value) {
return float;
}
return value;
};
export const createUrlEncodedTypeDecoder = () => {
return {
decode: (encodedData) => decodeValue(parse(encodedData, { depth: 100 })),
contentType: 'application/x-www-form-urlencoded',
};
};