function stringifyJsJsonRecursive()

in src/lib/utils/snippets.ts [3:23]


function stringifyJsJsonRecursive(value: unknown, currentIndent: string): string {
	if (typeof value === "string") return `"${value}"`;
	if (value === null) return "null";
	if (typeof value === "boolean" || 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 => stringifyJsJsonRecursive(v, nextIndent));
			return "[\n" + items.map(item => nextIndent + item).join(",\n") + "\n" + currentIndent + "]";
		}

		const entries = Object.entries(value);
		if (entries.length === 0) return "{}";
		const properties = entries.map(([k, v]) => `${nextIndent}"${k}": ${stringifyJsJsonRecursive(v, nextIndent)}`);

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