Skip to content

fix(sort): coerce NaN date times to epoch in sortByDate - #30

Merged
ManningWorks merged 1 commit into
mainfrom
issue-29-sortbydate-nan
Aug 18, 2026
Merged

fix(sort): coerce NaN date times to epoch in sortByDate#30
ManningWorks merged 1 commit into
mainfrom
issue-29-sortbydate-nan

Conversation

@ManningWorks

@ManningWorks ManningWorks commented Aug 18, 2026

Copy link
Copy Markdown
Owner

Summary

  • A truthy-but-unparseable date string (e.g. "not-a-date") yields NaN from getTime(), so the comparator returned NaN and the sort order was unspecified per the ECMAScript contract.
  • Coerce NaN to 0 in the comparator (per the issue's proposal) so unparseable dates degrade deterministically to epoch instead of poisoning the sort.
  • Added focused tests: unparseable dates sort as epoch between dateless and real dates in both orders, and all-unparseable input keeps a stable order.

Fixes #29.

Notes for review

The issue was written against pre-#27 code where dateless resolved to 0; this branch uses -Infinity for dateless. With the literal || 0 coercion, unparseable dates sort as epoch — older than every real date but newer than dateless. This matches the issue's "epoch-equivalent" wording.

Verification

pnpm build && pnpm typecheck && pnpm lint && pnpm test — all green (1013 tests, incl. 2 new).

Summary by CodeRabbit

  • Bug Fixes
    • Improved date sorting when values cannot be parsed, ensuring consistent ascending and descending results.
    • Preserved the original order when all date values are unparseable.
    • Continued handling missing dates consistently.

…poison sort

A truthy-but-unparseable date string yields NaN from getTime(), making
the comparator return NaN and the resulting order unspecified. Coerce
NaN to 0 so such projects degrade deterministically (epoch-equivalent)
instead. Fixes #29.
@vercel

vercel Bot commented Aug 18, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
projex-docs Ready Ready Preview Aug 18, 2026 10:05pm

@coderabbitai

coderabbitai Bot commented Aug 18, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 53f0468c-870a-4d92-82e1-015b461717bf

📥 Commits

Reviewing files that changed from the base of the PR and between e017d8e and d6ac60c.

📒 Files selected for processing (2)
  • packages/core/src/lib/__tests__/sortByDate.test.ts
  • packages/core/src/lib/sortByDate.ts

Included review availability: Your plan provides up to 3 included reviews per hour; 2 remain after this review.


📝 Walkthrough

Walkthrough

sortByDate now converts unparseable dates to epoch time. Tests cover deterministic ascending and descending ordering, plus stable ordering for multiple unparseable dates.

Changes

Date sorting behavior

Layer / File(s) Summary
Invalid date handling and coverage
packages/core/src/lib/sortByDate.ts, packages/core/src/lib/__tests__/sortByDate.test.ts
sortByDate maps invalid dates to timestamp 0 and preserves negative infinity for missing dates. Tests verify ordering in both directions and stable input order for equal invalid dates.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to d6ac6

This localized sorting fix deterministically handles unparseable dates and includes focused tests; no actionable merge-blocking risk remains after normal checks and review.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: coercing invalid date times to epoch values in sortByDate.
Linked Issues check ✅ Passed The implementation coerces unparseable dates to epoch time and adds tests for deterministic ascending, descending, and stable sorting required by issue #29.
Out of Scope Changes check ✅ Passed The code and tests directly support issue #29 and the stated objective without unrelated changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch issue-29-sortbydate-nan

Comment @coderabbitai help to get the list of available commands.

@greptile-apps

greptile-apps Bot commented Aug 18, 2026

Copy link
Copy Markdown

Greptile Summary

The PR prevents sortByDate comparators from returning NaN by treating unparseable selected dates as epoch timestamps.

  • Coerces invalid parsed timestamps to 0 while retaining -Infinity for missing dates.
  • Adds ascending, descending, and stable-order coverage for unparseable date strings.

Confidence Score: 5/5

The PR appears safe to merge with no actionable correctness, security, or repository-rule issues identified.

The comparator now maps invalid parsed timestamps to the intentionally documented epoch value, and the added tests cover ordering in both directions and equality stability.

Important Files Changed

Filename Overview
packages/core/src/lib/sortByDate.ts Normalizes unparseable dates to epoch so the comparator always returns a deterministic numeric result.
packages/core/src/lib/tests/sortByDate.test.ts Adds focused coverage for invalid-date placement in both sort directions and stable ordering when all dates are invalid.

Reviews (1): Last reviewed commit: "fix(sort): coerce NaN date times to epoc..." | Re-trigger Greptile

@ManningWorks
ManningWorks merged commit 7de451b into main Aug 18, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sortByDate: invalid date strings produce NaN comparator results (undefined sort order)

1 participant