noxon
Version:
Better JSON parsing and manipulation in TypeScript.
78 lines (77 loc) • 2.69 kB
JavaScript
import { format } from "../formats/formatter";
import { stringifyJSON } from "../utils/stringify";
import hljs from "highlight.js/lib/core";
import json from "highlight.js/lib/languages/json";
import javascript from "highlight.js/lib/languages/javascript";
import typescript from "highlight.js/lib/languages/typescript";
import xml from "highlight.js/lib/languages/xml";
import "highlight.js/styles/atom-one-light.min.css";
import { themes } from "../@helpers/themelib";
hljs.registerLanguage("javascript", javascript);
hljs.registerLanguage("json", json);
hljs.registerLanguage("typescript", typescript);
hljs.registerLanguage("xml", xml);
const consoleDisplay = (value) => {
try {
if (typeof value !== "object")
throw new Error("Passed value must be an object in consoleDisplay()");
console.log(format(value, 2));
} catch (error) {
console.error(error);
return void 0;
}
};
const display = (value, selector) => {
try {
if (value === null) throw new Error("Value passed in display is null");
if (typeof selector !== "string")
throw new Error("Selector must be a string");
let wrapper = document.querySelector(selector);
let preElement = document.createElement("pre");
if (typeof value === "object") {
let stringified = stringifyJSON(value);
if (stringified === void 0)
throw new Error("Object value is undefined");
preElement.innerText += stringified;
wrapper.appendChild(preElement);
return;
}
preElement.innerText += value;
wrapper.appendChild(preElement);
} catch (error) {
console.error(error);
return void 0;
}
};
const colormatic = async (value, lang, selector, theme = "atom-one-light") => {
try {
const ThemeLoader = async () => {
if (!themes[theme]) throw new Error("This theme is not supported");
await themes[theme]();
};
ThemeLoader();
const wrapper = document.querySelector(selector);
if (!wrapper) throw new Error("Wrapper element not found");
if (value === void 0 || value === null)
throw new Error("Value is undefined or null");
if (!lang) throw new Error("Language is undefined or null");
const stringified = typeof value === "string" ? value : format(value, 10);
const highlighted = hljs.highlight(stringified, {
language: lang
}).value;
const pre = document.createElement("pre");
const code = document.createElement("code");
code.classList.add("hljs", lang);
code.innerHTML += highlighted;
pre.appendChild(code);
wrapper.appendChild(pre);
} catch (error) {
console.error(error);
return void 0;
}
};
export {
colormatic,
consoleDisplay,
display
};