UNPKG

contexify

Version:

A TypeScript library providing a powerful dependency injection container with context-based IoC capabilities, inspired by LoopBack's Context system.

158 lines 4.53 kB
var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); const config = { colors: true, timestamp: true, formatters: { // eslint-disable-next-line @typescript-eslint/no-explicit-any o: /* @__PURE__ */ __name((v) => JSON.stringify(v), "o"), // eslint-disable-next-line @typescript-eslint/no-explicit-any O: /* @__PURE__ */ __name((v) => JSON.stringify(v, null, 2), "O") } }; const enabledNamespaces = /* @__PURE__ */ new Set(); const skippedNamespaces = /* @__PURE__ */ new Set(); function parseDebugEnv() { let namespaces = ""; if (typeof process !== "undefined" && process.env && process.env.DEBUG) { namespaces = process.env.DEBUG; } else if (typeof window !== "undefined") { try { namespaces = localStorage.getItem("debug") || ""; } catch { } } if (!namespaces) return; enabledNamespaces.clear(); skippedNamespaces.clear(); namespaces.split(/[\s,]+/).forEach((ns) => { if (!ns) return; if (ns === "*") { enabledNamespaces.add("*"); } else if (ns.startsWith("-")) { skippedNamespaces.add(ns.slice(1)); } else { enabledNamespaces.add(ns); } }); } __name(parseDebugEnv, "parseDebugEnv"); const colors = [ "#0000CC", "#CC0000", "#00CC00", "#CCCC00", "#00CCCC", "#CC00CC" ]; const nodeColors = [ "\x1B[34m", "\x1B[31m", "\x1B[32m", "\x1B[33m", "\x1B[36m", "\x1B[35m" ]; const reset = "\x1B[0m"; const namespaceColors = /* @__PURE__ */ new Map(); let colorIndex = 0; function getColorIndex(namespace) { if (!namespaceColors.has(namespace)) { namespaceColors.set(namespace, colorIndex++ % colors.length); } return namespaceColors.get(namespace); } __name(getColorIndex, "getColorIndex"); function format(formatter, args) { if (typeof formatter !== "string") { return [ formatter, ...args ].map((arg) => typeof arg === "object" ? JSON.stringify(arg) : String(arg)).join(" "); } let index = 0; return formatter.replace(/%([a-z%])/gi, (match, format2) => { if (match === "%%") return "%"; if (index >= args.length) return match; const arg = args[index++]; if (config.formatters && format2 in config.formatters) { return config.formatters[format2](arg); } switch (format2) { case "s": return String(arg); case "d": return Number(arg).toString(); case "j": return JSON.stringify(arg); case "o": return JSON.stringify(arg); case "O": return JSON.stringify(arg, null, 2); default: return String(arg); } }); } __name(format, "format"); function ensureEnvParsed() { parseDebugEnv(); } __name(ensureEnvParsed, "ensureEnvParsed"); function createDebugger(namespace) { ensureEnvParsed(); const isEnabled = /* @__PURE__ */ __name(() => { if (skippedNamespaces.has(namespace)) return false; if (enabledNamespaces.has("*")) return true; if (enabledNamespaces.has(namespace)) return true; return [ ...enabledNamespaces ].some((ns) => { if (ns.endsWith("*")) { const prefix = ns.slice(0, -1); return namespace.startsWith(prefix); } return false; }); }, "isEnabled"); const debug = /* @__PURE__ */ __name((formatter, ...args) => { if (!debug.enabled) return; const colorIdx = getColorIndex(namespace); const time = config.timestamp ? (/* @__PURE__ */ new Date()).toISOString() + " " : ""; const message = format(formatter, args); if (typeof process !== "undefined" && process.stdout && process.stdout.write) { if (config.colors) { process.stdout.write(`${nodeColors[colorIdx]}${time}${namespace}${reset} ${message} `); } else { process.stdout.write(`${time}${namespace} ${message} `); } } else { if (config.colors) { console.log(`%c${time}${namespace}`, `color: ${colors[colorIdx]}`, message); } else { console.log(`${time}${namespace}`, message); } } }, "debug"); Object.defineProperty(debug, "enabled", { get: isEnabled }); debug.extend = (suffix) => { return createDebugger(`${namespace}:${suffix}`); }; return debug; } __name(createDebugger, "createDebugger"); function configure(options) { Object.assign(config, options); } __name(configure, "configure"); var debug_default = createDebugger; export { configure, createDebugger, debug_default as default }; //# sourceMappingURL=debug.js.map