(obj: Record<string, unknown>, keys: string[] = [])
| 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 || |
no test coverage detected