char/avril

(wip) why'd you have to go and make things so complicaaaaated?

git clone https://git.t4t.associates/char/avril

Charlotte Somport console extension to typescript1738b3c

main
1.8 KiB63 linesraw
1import {
2  NumberParseFloat,
3  NumberParseInt,
4  String,
5  TypeError,
6} from "ext:core/primordials";
7import { inspect } from "./inspect.js";
8
9function format(args: readonly unknown[], colors = false): string {
10  if (args.length === 0) return "";
11  let text = "";
12  let consumed = 0;
13  if (typeof args[0] === "string" && args.length > 1) {
14    const template = args[0];
15    consumed = 1;
16    for (let index = 0; index < template.length; index++) {
17      const character = template[index];
18      const token = template[index + 1];
19      if (character === "%" && token === "%") {
20        text += "%";
21        index++;
22        continue;
23      }
24      if (
25        character !== "%" || consumed === args.length ||
26        !(token === "s" || token === "d" || token === "i" || token === "f" ||
27          token === "o" || token === "O" || token === "c")
28      ) {
29        text += character;
30        continue;
31      }
32      const value = args[consumed++];
33      index++;
34      if (token === "s") text += String(value);
35      else if (token === "d" || token === "i") {
36        text += typeof value === "symbol"
37          ? "NaN"
38          : String(NumberParseInt(value, 10));
39      } else if (token === "f") {
40        text += typeof value === "symbol"
41          ? "NaN"
42          : String(NumberParseFloat(value));
43      } else if (token !== "c") {
44        text += inspect(value, { depth: token === "o" ? 3 : 2, colors });
45      }
46    }
47  }
48  for (; consumed < args.length; consumed++) {
49    if (consumed > 0) text += " ";
50    const value = args[consumed];
51    text += typeof value === "string" ? value : inspect(value, { colors });
52  }
53  return text;
54}
55
56function labelString(value: unknown): string {
57  if (typeof value === "symbol") {
58    throw new TypeError("Cannot convert a Symbol value to a string");
59  }
60  return String(value);
61}
62
63export { format, labelString };