MCPcopy Create free account
hub / github.com/denoland/std / #printObject

Method #printObject

toml/stringify.ts:49–102  ·  view source on GitHub ↗
(obj: Record<string, unknown>, keys: string[] = [])

Source from the content-addressed store, hash-verified

47 return this.output;
48 }
49 #printObject(obj: Record<string, unknown>, keys: string[] = []): string[] {
50 const out = [];
51 const props = Object.keys(obj);
52 const inlineProps = [];
53 const multilineProps = [];
54 for (const prop of props) {
55 if (this.#isSimplySerializable(obj[prop])) {
56 inlineProps.push(prop);
57 } else {
58 multilineProps.push(prop);
59 }
60 }
61 const sortedProps = inlineProps.concat(multilineProps);
62 for (const prop of sortedProps) {
63 const value = obj[prop];
64 if (value instanceof Date) {
65 out.push(this.#dateDeclaration([prop], value));
66 } else if (typeof value === "string" || value instanceof RegExp) {
67 out.push(this.#strDeclaration([prop], value.toString()));
68 } else if (typeof value === "number") {
69 out.push(this.#numberDeclaration([prop], value));
70 } else if (typeof value === "boolean") {
71 out.push(this.#boolDeclaration([prop], value));
72 } else if (
73 value instanceof Array
74 ) {
75 const arrayType = this.#getTypeOfArray(value);
76 if (arrayType === "ONLY_PRIMITIVE") {
77 out.push(this.#arrayDeclaration([prop], value));
78 } else if (arrayType === "ONLY_OBJECT_EXCLUDING_ARRAY") {
79 // array of objects
80 for (let i = 0; i < value.length; i++) {
81 out.push("");
82 out.push(this.#headerGroup([...keys, prop]));
83 out.push(...this.#printObject(value[i], [...keys, prop]));
84 }
85 } else {
86 // this is a complex array, use the inline format.
87 const str = value.map((x) => this.#printAsInlineValue(x)).join(",");
88 out.push(`${this.#declaration([prop])}[${str}]`);
89 }
90 } else if (typeof value === "object") {
91 out.push("");
92 out.push(this.#header([...keys, prop]));
93 if (value) {
94 const toParse = value as Record<string, unknown>;
95 out.push(...this.#printObject(toParse, [...keys, prop]));
96 }
97 // out.push(...this._parse(value, `${path}${prop}.`));
98 }
99 }
100 out.push("");
101 return out;
102 }
103 #isPrimitive(value: unknown): boolean {
104 return value instanceof Date ||
105 value instanceof RegExp ||

Callers 1

dumpMethod · 0.95

Calls 14

#isSimplySerializableMethod · 0.95
#dateDeclarationMethod · 0.95
#strDeclarationMethod · 0.95
#numberDeclarationMethod · 0.95
#boolDeclarationMethod · 0.95
#getTypeOfArrayMethod · 0.95
#arrayDeclarationMethod · 0.95
#headerGroupMethod · 0.95
#printAsInlineValueMethod · 0.95
#declarationMethod · 0.95
#headerMethod · 0.95
keysMethod · 0.80

Tested by

no test coverage detected