-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
format.ts
36 lines (36 loc) · 907 Bytes
/
format.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
export function format(f: string, ...args: unknown[]) {
let i = 0;
let len = args.length;
let str = String(f).replace(/%[sdjoO%]/g, (x: string): string => {
if (x === "%%") return "%";
if (i >= len) return x;
switch (x) {
case "%s":
return String(args[i++]);
case "%d":
return Number(args[i++]).toString();
case "%o":
return Deno.inspect(args[i++]).split("\n").map((_) => _.trim()).join(
" ",
);
case "%O":
return Deno.inspect(args[i++]);
case "%j":
try {
return JSON.stringify(args[i++]);
} catch {
return "[Circular]";
}
default:
return x;
}
});
for (const x of args.splice(i)) {
if (x === null || !(typeof x === "object" && x !== null)) {
str += " " + x;
} else {
str += " " + Deno.inspect(x);
}
}
return str;
}