Skip to content

[refactor]: split result/builder.ts into classes.ts and functions.ts #308

@codewizdave

Description

@codewizdave

Problem

The result/builder.ts file mixes internal classes (Ok, Err) with standalone utility functions (map, flatMap, isOk, etc.). This violates the project's file-organization rule which states "Types and functions must not be mixed in the same file."

Why It Matters

  • Harder to navigate - related code is mixed together
  • Violates project conventions for file structure
  • Makes it harder to maintain and test each concern separately
  • Blurs the line between internal implementation and public API

Current vs Expected DX

// Current - mixed in single file
result/builder.ts  // Contains both classes AND functions

// Expected - separated by concern
result/classes.ts   // Ok and Err internal classes
result/functions.ts // Standalone utility functions
result/index.ts     // Re-exports only

Current Situation

Currently result/builder.ts contains:

  • Internal Ok and Err classes (lines 15-73)
  • Factory functions ok(), err() (lines 82, 90)
  • Standalone functions isOk(), isErr(), map(), flatMap(), etc.

This violates the file organization rule.

What Would Change

Split result/builder.ts into:

  1. result/classes.ts - Internal Ok/Err classes

    • class Ok<T, E>
    • class Err<E>
  2. result/functions.ts - All standalone functions

    • Factory functions: ok(), err()
    • Type guards: isOk(), isErr()
    • Operations: map(), flatMap(), mapErr(), tap(), tapErr(), etc.
    • Combinators: all(), traverse(), swap(), etc.
    • Conversions: toNullable(), toUndefined(), unwrap()
  3. result/index.ts - Re-exports only (already exists)

Code Example

Current structure:

result/
├── index.ts      // Re-exports
├── types.ts      // Type definitions only
├── builder.ts    // MIXED: classes + functions together
└── ...

Expected structure:

result/
├── index.ts      // Re-exports
├── types.ts      // Type definitions only
├── classes.ts    // Internal Ok/Err classes
├── functions.ts  // All standalone functions
└── ...

Implementation Notes

  • Both classes.ts and functions.ts should export the same public API
  • Internal classes in classes.ts should not be exported from index.ts
  • Factory functions ok() and err() should be in functions.ts and use classes from classes.ts
  • No changes to the public API - only internal file reorganization

Co-Authored-By: martyy-code nesalia.inc@gmail.com

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions