UNPKG

@yoyo-org/progressive-json

Version:

Stream and render JSON data as it arrives - perfect for AI responses, large datasets, and real-time updates

31 lines (30 loc) 1.05 kB
import { isPlaceholder } from "./is-placeholder"; import { extractPlaceholderId } from "./extract-placeholder-id"; /** * Recursively finds all placeholders in the value and returns a mapping from refId to their path. * @param value The object/array to search * @param currentPath The path so far (for recursion) * @param result The result map (for recursion) */ export function findPlaceholders(value, currentPath = [], result = {}) { if (typeof value === "string" && isPlaceholder(value)) { const refId = extractPlaceholderId(value); if (refId !== -1) { result[refId] = [...currentPath]; } return result; } if (typeof value !== "object" || value === null) { return result; } if (Array.isArray(value)) { value.forEach((item, idx) => { findPlaceholders(item, [...currentPath, idx], result); }); return result; } for (const key in value) { findPlaceholders(value[key], [...currentPath, key], result); } return result; }