auto-console-group
Version:
Automagically group console messages
14 lines (12 loc) • 2.9 kB
JavaScript
/*!
* @module auto-console-group v1.2.9
*
* @description Automagically group console logs in the browser console.
*
* @author David J. Bradshaw <info@iframe-resizer.com>
* @see {@link https://github.com/davidjbradshaw/auto-console-group#readme}
* @license MIT
*
* @copyright (c) 2025, David J. Bradshaw. All rights reserved.
*/
(function(s,i){typeof exports=="object"&&typeof module<"u"?i(exports):typeof define=="function"&&define.amd?define(["exports"],i):(s=typeof globalThis<"u"?globalThis:s||self,i(s.createAutoConsoleGroup={}))})(this,function(s){"use strict";const i="font-weight: normal;",T="font-weight: bold;",L="font-style: italic;",$=i+L,I="color: #135CD2;",M="color: #A9C7FB;",D="color: #1F1F1F;",R="color: #E3E3E3;",l="default",y="error",C="log",H=Object.freeze({assert:!0,error:!0,warn:!0}),O={expand:!0,defaultEvent:void 0,event:void 0,label:"AutoConsoleGroup",showTime:!0},S={profile:0,profileEnd:0,timeStamp:0,trace:0},B=o=>{const t=o.event||o.defaultEvent;return t?`${t}`:""},f=Object.assign(console);function F(){const o=new Date,t=(p,m)=>o[p]().toString().padStart(m,"0"),c=t("getHours",2),u=t("getMinutes",2),r=t("getSeconds",2),a=t("getMilliseconds",3);return`@ ${c}:${u}:${r}.${a}`}const{fromEntries:N,keys:U}=Object,j=o=>[o,f[o]],_=o=>t=>[t,function(c){o[t]=c}],h=(o,t)=>N(U(o).map(t));function b(o={}){const t={},c={},u=[],r={...O,expand:!o.collapsed||O.expanded,...o};let a="";function p(){u.length=0,a=""}function m(){delete r.event,p()}const Q=()=>u.some(([e])=>e in H),q=()=>Q()?!0:!!r.expand,x=()=>r.showTime?a:"";function v(){if(u.length===0){m();return}f[q()?"group":"groupCollapsed"](`%c${r.label}%c ${B(r)} %c${x()}`,i,T,$);for(const[e,...n]of u)f.assert(e in f,`Unknown console method: ${e}`),e in f&&f[e](...n);f.groupEnd(),m()}function g(){a===""&&(a=F())}function V(e){g(),r.event=e}function W(){g(),queueMicrotask(()=>queueMicrotask(v))}function d(e,...n){u.length===0&&W(),u.push([e,...n])}const z=e=>(...n)=>{let E;try{E=e(...n)}catch(w){if(Error.prototype.isPrototypeOf(w))d(y,w);else throw w}return E};function K(e,...n){e||d("assert",e,...n)}function X(e=l){c[e]?c[e]+=1:c[e]=1,d(C,`${e}: ${c[e]}`)}function J(e=l){delete c[e]}function Y(e=l){g(),t[e]=performance.now()}function A(e=l,...n){if(!t[e]){d("timeLog",e,...n);return}const E=performance.now()-t[e];d(C,`${e}: ${E} ms`,...n)}function Z(e=l){A(e),delete t[e]}const ee=e=>[e,(...n)=>d(e,...n)];return{...h(r,_(r)),...h(console,ee),...h(S,j),assert:K,count:X,countReset:J,endAutoGroup:v,errorBoundary:z,event:V,purge:p,time:Y,timeEnd:Z,timeLog:A,touch:g}}const G=typeof window>"u"||typeof window.matchMedia!="function"?!1:window.matchMedia("(prefers-color-scheme: dark)").matches,k=G?M:I,P=G?R:D;s.BOLD=T,s.FOREGROUND=P,s.HIGHLIGHT=k,s.ITALIC=L,s.NORMAL=i,s.default=b,Object.defineProperties(s,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});