Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit894819e

Browse files
Copilotstreamich
andcommitted
feat: implement inline inspect and format functions to replace node:util imports
Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
1 parent5af4252 commit894819e

File tree

1 file changed

+64
-1
lines changed

1 file changed

+64
-1
lines changed

‎src/vendor/node/util.ts‎

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,64 @@
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+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp