UNPKG

@gulibs/react-vintl

Version:

Type-safe i18n library for React with Vite plugin and automatic type inference

73 lines (72 loc) 2.3 kB
function isProduction() { return typeof process < "u" && process.env.NODE_ENV ? process.env.NODE_ENV === "production" : typeof window < "u" ? !window.location.hostname.includes("localhost") && !window.location.hostname.includes("127.0.0.1") && !window.location.hostname.includes("0.0.0.0") : !1; } var Logger = class { constructor(t = {}) { this.deduplicationCache = /* @__PURE__ */ new Map(); let n = isProduction(); this.config = { debug: t.debug ?? !n, info: t.info ?? !n, warn: t.warn ?? !n, error: t.error ?? !0, deduplicate: t.deduplicate ?? !0, deduplicationWindow: t.deduplicationWindow ?? 5e3 }; } shouldLog(e) { if (!this.config.deduplicate) return !0; let t = Date.now(), n = this.deduplicationCache.get(e); return n ? t - n.timestamp < this.config.deduplicationWindow ? !1 : (n.timestamp = t, this.cleanupDeduplicationCache(t), !0) : (this.deduplicationCache.set(e, { timestamp: t }), this.cleanupDeduplicationCache(t), !0); } cleanupDeduplicationCache(e) { if (Math.random() < .01) for (let [t, n] of this.deduplicationCache.entries()) e - n.timestamp > this.config.deduplicationWindow * 2 && this.deduplicationCache.delete(t); } getMessageKey(...e) { try { return JSON.stringify(e); } catch { return e.map((e) => String(e)).join(" "); } } debug(...e) { if (this.config.debug) { let t = this.getMessageKey("debug", ...e); this.shouldLog(t) && console.log("[react-vintl]", ...e); } } info(...e) { if (this.config.info) { let t = this.getMessageKey("info", ...e); this.shouldLog(t) && console.log("[react-vintl]", ...e); } } warn(...e) { if (this.config.warn) { let t = this.getMessageKey("warn", ...e); this.shouldLog(t) && console.warn("[react-vintl]", ...e); } } error(...e) { if (this.config.error) { let t = this.config.deduplicationWindow; this.config.deduplicationWindow = t * 2; let n = this.getMessageKey("error", ...e); this.shouldLog(n) && console.error("[react-vintl]", ...e), this.config.deduplicationWindow = t; } } setConfig(e) { this.config = { ...this.config, ...e }; } clearDeduplicationCache() { this.deduplicationCache.clear(); } getConfig() { return { ...this.config }; } }; const logger = new Logger(); export { logger as n, Logger as t };