nsole
Version:
A drop-in isomorphic console.* replacement that's pretty, small, fast, and flexible
77 lines (75 loc) • 3.02 kB
JavaScript
// src/browser/_tmpl_.ltag.ts
var ltag = (id, level, definitions, pluginFn, colorSpace, i = "_id", s = "_def", l = "_lvl", G = "PTAG_", F = "_FORCE", eobj = {}, lnoop = () => {}, tof = (v, k = "s") => (typeof v)[0] == k, gthis = tof(globalThis, "o") ? globalThis : eobj, getEnvDefVal = ([key, param, def], idx) => [param ?? gthis[G + key] ?? def, gthis[G + key + F]].map((v) => idx > 2 ? tof(v) ? JSON.parse(v) : v : v), [[idG, idF], [lvlG, lvlF], [pluginG, pluginF], [defsG, defsF]] = [
["ID", id, "{}"],
["LVL", level, 5],
["PLG", pluginFn],
["DEF", null, eobj]
].map(getEnvDefVal), _defs = {
debug: [5, ">", "#89A"],
log: [4, ">", "#2AE"],
info: [4, "i", "#1AA"],
check: [3, "?", "#D6D"],
warn: [2, "!", "#F77"],
error: [1, "X", "#F36"],
_dl: [
"|",
"|",
"|"
],
_css: [
" ",
" ",
" ",
" ",
"color:^;font-weight:900;font-family:Menlo,Consolas,monospace;"
],
...defsG,
...definitions ?? eobj,
...defsF ?? eobj
}, setStyle = (value, dvalue = _defs) => ({ ...dvalue, ...value ?? _defs }), isForceRerun = 0 > colorSpace, gconsole = console) => new Proxy({
[i]: idG,
[s]: setStyle(),
[l]: lvlG
}, {
get: (target, prop) => {
let defs = target[s] ?? _defs;
let idef = defs[prop];
lvlF = gthis[G + "LVL" + F];
let ilvl = ~~(lvlF ?? target[l]);
let rtn = prop == l || prop == i || prop == s ? target[prop] : ilvl && (ilvl == -1 || !idef && (prop in gconsole)) ? gconsole[prop] ?? gconsole.log : !ilvl || idef?.[0] > ilvl ? lnoop : null;
if (!rtn && ((target[prop] ?? lnoop) === lnoop || isForceRerun)) {
target[s] = idef ? defs : setStyle({ [prop]: [1, prop, "#A58"] }, defs);
idef = target[s][prop];
let cid = (idF ?? target[i] ?? "").replace("{}", prop);
let plug = pluginF ?? pluginG;
let [nid, tag, bg, fg, cm, css, dl] = plug ? plug(idef, prop, cid, target) : idef;
if (nid !== idef[0]) {
if (tof(nid, "f")) {
return nid;
}
cid = tof(nid) ? nid : cid;
}
dl = defsF?._dl ?? dl ?? defs._dl;
css = defsF?._css ?? css ?? defs._css;
let nc = "NO_COLOR" in gthis || (fg ?? defs._fg) == "";
nc = isForceRerun ? !nc : colorSpace ?? !nc;
let [ds, dm, de] = dl.map((v) => nc ? `%c${v}%c` : v);
let [dcss, tcss, xcss, ecss, acss] = css;
target[prop] = (gconsole[cm ?? prop] ?? gconsole.log).bind(gconsole, ds + (prop == tag ? "*" : tag) + dm + cid + de, ...nc ? [dcss, tcss, dcss, xcss, dcss + ecss, ""].map((v) => v ? ((acss ?? "") + v).replace(/\^+/g, (match) => (match == "^" ? fg ?? bg : match == "^^" ? fg ?? defs._fg : bg) ?? "#000C") : v) : []);
}
return rtn || target[prop];
},
set: (target, prop, value) => {
target[prop] = prop == l ? value ?? lvlG : prop == i ? value ?? idG : setStyle(value, target[s]);
for (let k in target[s]) {
target[k] = lnoop;
}
return true;
}
});
// src/browser/ltag.ts
var ltag_default = ltag;
export {
ltag,
ltag_default as default
};