function stringifyPythonRecursive()

in src/lib/utils/snippets.ts [29:51]


function stringifyPythonRecursive(value: unknown, currentIndent: string): string {
	if (typeof value === "string") return `"${value}"`;
	if (typeof value === "boolean") return value ? "True" : "False";
	if (value === null) return "None";
	if (typeof value === "number") return String(value);

	if (typeof value === "object" && value !== null) {
		const nextIndent = currentIndent + INDENT_STEP;
		if (Array.isArray(value)) {
			if (value.length === 0) return "[]";
			const items = value.map(v => stringifyPythonRecursive(v, nextIndent));
			return "[\n" + items.map(item => nextIndent + item).join(",\n") + "\n" + currentIndent + "]";
		}

		const entries = Object.entries(value);
		if (entries.length === 0) return "{}";
		// In Python, dictionary keys are typically strings.
		const properties = entries.map(([k, v]) => `${nextIndent}"${k}": ${stringifyPythonRecursive(v, nextIndent)}`);

		return "{\n" + properties.join(",\n") + "\n" + currentIndent + "}";
	}
	return String(value); // Fallback
}