json-oneline-stringify
Version:
A little function for stringifying into a single line, in a readable form.
41 lines (40 loc) • 1.34 kB
JavaScript
/* MAIN */
function stringify(input) {
const type = typeof input;
if (type === 'string' || type === 'number')
return JSON.stringify(input);
if (type === 'boolean')
return input ? 'true' : 'false';
if (type === 'object') {
if (input === null) {
return 'null';
}
else if (Array.isArray(input)) {
let children = '';
for (let i = 0, l = input.length; i < l; i++) {
const value = input[i];
const child = stringify(value);
if (child === undefined)
continue;
children += children ? `, ${child}` : child;
}
return `[${children}]`;
}
else {
let children = '';
for (const prop in input) {
const value = stringify(input[prop]);
if (value === undefined)
continue;
const key = stringify(prop);
children += children ? `, ${key}: ${value}` : `${key}: ${value}`;
}
return children ? `{ ${children} }` : '{}';
}
}
if (type === 'undefined' || type === 'function' || type === 'symbol')
return;
throw new Error(`Unsupported type: "${type}"`);
}
/* EXPORT */
export default stringify;