noxon
Version:
Better JSON parsing and manipulation in TypeScript.
59 lines (58 loc) • 1.71 kB
JavaScript
const format = (value, spaces) => {
try {
if (value === void 0) {
console.warn("Warning: Value is undefined");
return;
}
if (typeof value !== "object" || value === null)
throw new Error("Passed value in formatJSON() is not an object");
if (typeof spaces != "number" && typeof spaces != "string")
throw new Error("Passed number of spaces can only be string or a number");
let parsedSpaces = Number(spaces);
if (Number.isNaN(parsedSpaces))
throw new Error("Number of spaces has to be a number");
const JSONFormatted = JSON.stringify(value, null, parsedSpaces);
return JSONFormatted;
} catch (error) {
console.error(error);
return void 0;
}
};
const formatFill = (value, fill) => {
try {
if (value === void 0) {
console.warn("Warning: Value is undefined");
return;
}
if (typeof value !== "object" || value === null)
throw new Error("Passed value in formatJSON() is not an object");
if (typeof fill !== "string")
throw new Error("Passed value can only be string");
const JSONFormatted = JSON.stringify(value, null, fill);
return JSONFormatted;
} catch (error) {
console.error(error);
return void 0;
}
};
const minifyJSON = (value) => {
try {
if (value === void 0) {
console.warn("Warning: Value is undefined");
return;
}
if (typeof value !== "string" && typeof value !== "object" || value === null)
throw new Error(
"Passed value in minifyJSON() must be string or an object"
);
return JSON.stringify(value);
} catch (error) {
console.error(error);
return void 0;
}
};
export {
format,
formatFill,
minifyJSON
};