Normalize nested API entities
Turn nested documents into stable entity maps and ordered ID references.Description
Requirements
Export a callable normalizeDocuments function from src/App.js.
Return document, user, and attachment maps keyed by each entity ID.
Store normalized documents as id, title, ownerId, and attachmentIds.
Store a shared owner or attachment once and reference the same ID everywhere.
Preserve input document order in documentIds and nested order in attachmentIds.
Keep the first occurrence when a later entity repeats an ID with different data.
Return four distinct empty containers for an empty document array.
Create new maps, arrays, and normalized entity records on every invocation.
Do not mutate documents, owners, attachments, or their arrays.
Shared owner
Two documents contain owner u1
Both documents reference ownerId u1 and usersById contains one u1
Shared nested records become one canonical entity.
Conflicting duplicate
Later owner u1 has a different name
The first u1 name remains
First occurrence wins deterministically across conflicting shared entities.
Constraints
Every entity has a nonempty string IDOwners and attachments retain only id and nameDuplicate root document IDs also use first occurrence
Hints
Hint 1
Walk documents once and insert into each map only when the ID is not already present.
Hint 2
Build attachmentIds while visiting a document's attachments in order.
Hint 3
Construct normalized records explicitly instead of deleting fields from input objects.