Skip to content

Refactor generateTSVString to use pure transformations and remove mutation#748

Open
SarthakJagota wants to merge 1 commit intoneurobagel:mainfrom
SarthakJagota:refactor/generate-tsv-pure-transformation
Open

Refactor generateTSVString to use pure transformations and remove mutation#748
SarthakJagota wants to merge 1 commit intoneurobagel:mainfrom
SarthakJagota:refactor/generate-tsv-pure-transformation

Conversation

@SarthakJagota
Copy link
Copy Markdown

@SarthakJagota SarthakJagota commented Feb 28, 2026

Summary

This PR refactors generateTSVString to remove mutation-based accumulation and adopt a functional transformation approach.

Changes

Replaced forEach + push with flatMap/map

Removed the TODO comment related to mutation

Eliminated @ts-expect-error by adding proper type narrowing

Preserved the existing TSV output format and column ordering

No behavioral changes

Motivation

The previous implementation relied on mutating an array (tsvRows) inside a loop. This refactor makes the function purely transformation-based, improving clarity and maintainability while maintaining identical output.

Testing

Verified that both labeled and URI TSV downloads produce identical output compared to the previous implementation.

No changes to UI behavior or data formatting.

#747

Summary by Sourcery

Refactor TSV generation in ResultContainer to use a pure transformation-based approach instead of mutating intermediate state.

Enhancements:

  • Replace mutation-based TSV row accumulation with flatMap/map transformations while preserving the existing TSV format and column order.
  • Introduce explicit type narrowing for subject_data to remove the need for TypeScript expectation errors.

@netlify
Copy link
Copy Markdown

netlify bot commented Feb 28, 2026

Deploy Preview for neurobagel-query ready!

Name Link
🔨 Latest commit 9f5a8b8
🔍 Latest deploy log https://app.netlify.com/projects/neurobagel-query/deploys/69a2932e3288510008ad07f2
😎 Deploy Preview https://deploy-preview-748--neurobagel-query.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai bot commented Feb 28, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Refactors ResultContainer.generateTSVString to build the TSV output via pure array transformations (map/flatMap) instead of mutating an accumulator array, while preserving the existing TSV format and behavior and tightening type safety around subject_data.

Flow diagram for the refactored generateTSVString transformation pipeline

flowchart TD
  A[generateTSVString called with subjectsResponse and buttonIndex] --> B[Compute isFileWithLabels from buttonIndex]
  B --> C[Build headers as tab separated string]
  C --> D[Iterate with flatMap over subjectsResponse.responses]
  D --> E[For each subResp, find datasetMetadata in datasetsResponse.responses]
  E --> F[Destructure dataset fields with default values]
  F --> G{isProtected?}

  G -- Yes --> H[Build single TSV row for protected dataset]
  H --> I[Return array containing single row string]

  G -- No --> J{Is subResp.subject_data an array?}
  J -- No --> K[Return empty array for this subResp]
  J -- Yes --> L[Map subject_data to TSV row strings]
  L --> M[Return array of row strings for this subResp]

  I --> N[flatMap collects all row strings into dataRows]
  M --> N

  N --> O[Prepend headers to dataRows]
  O --> P[Join all rows with newline characters]
  P --> Q[Return final TSV string]
Loading

File-Level Changes

Change Details Files
Refactor TSV generation from a mutating accumulator to a pure transformation pipeline.
  • Removed the mutable tsvRows accumulator and the forEach-based row construction
  • Introduced dataRows computed via flatMap over subjectsResponse.responses to produce all TSV data rows
  • Kept the header row composition logic but now combine headers and dataRows at the end into the final TSV string
src/components/ResultContainer.tsx
Tighten typing and control flow around protected vs unprotected subject data.
  • Replaced the ts-expect-error on subject_data with an explicit Array.isArray guard before iterating
  • Used conditional branches to return a single row for protected datasets and multiple rows for unprotected datasets via map
  • Ensured the TSV output structure uses the same columns and ordering for both protected and unprotected cases
src/components/ResultContainer.tsx
Remove obsolete TODO and clarify non-mutating approach.
  • Deleted the TODO about refactoring away from mutation since the function now uses pure transformations
  • Kept comments minimal while relying on the new functional structure to convey intent
src/components/ResultContainer.tsx

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@github-actions github-actions bot added the _community [BOT ONLY] PR label for community contributions. Used for tracking label Feb 28, 2026
Copy link
Copy Markdown

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've reviewed this pull request using the Sourcery rules engine

@neurobagel-bot neurobagel-bot bot moved this to Community in Neurobagel Mar 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

_community [BOT ONLY] PR label for community contributions. Used for tracking

Projects

Status: Community

Development

Successfully merging this pull request may close these issues.

1 participant