UNPKG

@chubbyts/chubbyts-decode-encode

Version:

A simple decode/encode solution for json / jsonx / url-encoded / xml / yaml.

21 lines (20 loc) 635 B
import { stringify } from 'qs'; import { isArray, isNull, isObject } from '../data.js'; const encodeValue = (value) => { if (isObject(value)) { return Object.fromEntries(Object.entries(value).map(([subKey, subValue]) => [subKey, encodeValue(subValue)])); } if (isArray(value)) { return value.map(encodeValue); } if (isNull(value)) { return 'null'; } return value; }; export const createUrlEncodedTypeEncoder = () => { return { encode: (data) => stringify(encodeValue(data), { encodeValuesOnly: true }), contentType: 'application/x-www-form-urlencoded', }; };