|
1 | | -export{inspect,format,inherits,promisify}from'node:util'; |
| 1 | +export{inherits,promisify}from'node:util'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Minimal implementation of Node.js util.inspect function. |
| 5 | + * Converts a value to a string representation for debugging. |
| 6 | + */ |
| 7 | +exportfunctioninspect(value:any):string{ |
| 8 | +if(value===null)return'null'; |
| 9 | +if(value===undefined)return'undefined'; |
| 10 | +if(typeofvalue==='string')return`'${value}'`; |
| 11 | +if(typeofvalue==='number'||typeofvalue==='boolean')returnString(value); |
| 12 | +if(Array.isArray(value)){ |
| 13 | +constitems=value.map(item=>inspect(item)).join(', '); |
| 14 | +return`[${items} ]`; |
| 15 | +} |
| 16 | +if(typeofvalue==='object'){ |
| 17 | +constentries=Object.entries(value) |
| 18 | +.map(([key,val])=>`${key}:${inspect(val)}`) |
| 19 | +.join(', '); |
| 20 | +return`{${entries} }`; |
| 21 | +} |
| 22 | +returnString(value); |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Minimal implementation of Node.js util.format function. |
| 27 | + * Provides printf-style string formatting with basic placeholder support. |
| 28 | + */ |
| 29 | +exportfunctionformat(template:string, ...args:any[]):string{ |
| 30 | +if(args.length===0)returntemplate; |
| 31 | + |
| 32 | +letresult=template; |
| 33 | +letargIndex=0; |
| 34 | + |
| 35 | +// Replace %s (string), %d (number), %j (JSON) placeholders |
| 36 | +result=result.replace(/%[sdj%]/g,match=>{ |
| 37 | +if(argIndex>=args.length)returnmatch; |
| 38 | + |
| 39 | +constarg=args[argIndex++]; |
| 40 | +switch(match){ |
| 41 | +case'%s': |
| 42 | +returnString(arg); |
| 43 | +case'%d': |
| 44 | +returnNumber(arg).toString(); |
| 45 | +case'%j': |
| 46 | +try{ |
| 47 | +returnJSON.stringify(arg); |
| 48 | +}catch{ |
| 49 | +return'[Circular]'; |
| 50 | +} |
| 51 | +case'%%': |
| 52 | +return'%'; |
| 53 | +default: |
| 54 | +returnmatch; |
| 55 | +} |
| 56 | +}); |
| 57 | + |
| 58 | +// Append remaining arguments |
| 59 | +while(argIndex<args.length){ |
| 60 | +result+=' '+String(args[argIndex++]); |
| 61 | +} |
| 62 | + |
| 63 | +returnresult; |
| 64 | +} |