| 17 | } |
| 18 | |
| 19 | private static stringifyWalk(value: Value, indent: string | null, currentIndent = ''): string { |
| 20 | switch (value.type) { |
| 21 | case 'bool': return value.value ? 'true' : 'false'; |
| 22 | case 'null': return 'null'; |
| 23 | case 'num': return value.value.toString(); |
| 24 | case 'str': return JSON.stringify(value.value); |
| 25 | case 'arr': { |
| 26 | if (value.value.length === 0) return '[]'; |
| 27 | const items = value.value.map(item => this.stringifyWalk(item, indent, currentIndent + (indent ?? ''))); |
| 28 | if (indent != null && indent !== '') { |
| 29 | return `[\n${currentIndent + indent}${items.join(`,\n${currentIndent + indent}`)}\n${currentIndent}]`; |
| 30 | } else { |
| 31 | return `[${items.join(', ')}]`; |
| 32 | } |
| 33 | } |
| 34 | case 'obj': { |
| 35 | const keys = [...value.value.keys()]; |
| 36 | if (keys.length === 0) return '{}'; |
| 37 | const items = keys.map(key => { |
| 38 | const val = value.value.get(key)!; |
| 39 | return `${key}: ${this.stringifyWalk(val, indent, currentIndent + (indent ?? ''))}`; |
| 40 | }); |
| 41 | if (indent != null && indent !== '') { |
| 42 | return `{\n${currentIndent + indent}${items.join(`,\n${currentIndent + indent}`)}\n${currentIndent}}`; |
| 43 | } else { |
| 44 | return `{${items.join(', ')}}`; |
| 45 | } |
| 46 | } |
| 47 | default: |
| 48 | throw new Error(`Cannot stringify value of type: ${value.type}`); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | public static stringify(value: JsValue, _unused = null, indent: number | string = 0): string { |
| 53 | let _indent: string | null = null; |