@gulibs/react-vintl
Version:
Type-safe i18n library for React with Vite plugin and automatic type inference
1 lines • 2.55 kB
JavaScript
var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));function c(){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 l=class{constructor(e={}){this.deduplicationCache=new Map;let t=c();this.config={debug:e.debug??!t,info:e.info??!t,warn:e.warn??!t,error:e.error??!0,deduplicate:e.deduplicate??!0,deduplicationWindow:e.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 u=new l;Object.defineProperty(exports,`Logger`,{enumerable:!0,get:function(){return l}}),Object.defineProperty(exports,`__toESM`,{enumerable:!0,get:function(){return s}}),Object.defineProperty(exports,`logger`,{enumerable:!0,get:function(){return u}});