Skip to content

Extract duplicated reverse/1 to shared utility #21

Description

@StephanMeijer

Problem

Three modules each define their own private reverse/1 function that recursively reverses nested list structures. The implementations are nearly identical with minor clause variations:

  • BlockNote.Writer — 5 clauses (handles :content, :rows, :cells)
  • Tiptap.Writer — 3 clauses (handles :content)
  • Tiptap.Reader — 3 clauses (handles :children)

The BlockNote version is a superset of the other two.

Why these exist

The readers/writers build lists using [head | tail] prepend patterns for performance, then reverse at the end. This is standard Elixir, but the reversal logic for nested tree structures is non-trivial and shouldn't be duplicated.

Proposal

Extract to a shared utility, e.g. DocSpec.Core.Util.Tree.reverse/1, that handles all known key patterns (:children, :content, :rows, :cells). The function should be generic enough to work with any nested struct/map that uses list-valued keys.

Files

  • lib/docspec/core/blocknote/writer.ex (lines 714–731)
  • lib/docspec/core/tiptap/writer.ex (lines 275–283)
  • lib/docspec/core/tiptap/reader.ex (lines 353–361)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions