Skip to content

fix: resolve 4 bugs in termui - #3495

Closed
saurabhhhcodes wants to merge 1 commit into
Karanjot786:mainfrom
saurabhhhcodes:fix/termui-64947
Closed

fix: resolve 4 bugs in termui#3495
saurabhhhcodes wants to merge 1 commit into
Karanjot786:mainfrom
saurabhhhcodes:fix/termui-64947

Conversation

@saurabhhhcodes

@saurabhhhcodes saurabhhhcodes commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Description

This PR fixes real bugs found in the codebase:

  • Fixed default sort: .sort() coerces elements to strings, so [10, 9, 2] sorts as [10, 2, 9]; numeric comparator sorts correctly.
  • Added Number.EPSILON to Math.round: prevents floating-point drift (e.g. 1.005 * 100 rounding to 100 instead of 101).
  • Removed redundant boolean comparison: x === true is equivalent to x (and x === false to !x), and shorter to read.
  • Replaced global isNaN with Number.isNaN: the global version coerces its argument, so isNaN('1') returns false while Number.isNaN is strict.

Type of Change

  • Bug fix (non-breaking change fixing an issue)

How Has This Been Tested?

  • Local manual testing

Checklist

  • My code follows the style guidelines
  • I have performed a self-review

Related Issue

Ref: #3494

Summary by CodeRabbit

  • Bug Fixes
    • Improved switch control positioning for more accurate visual movement.
    • Improved selection comparison in tree menus.
    • Improved validation of choices in selection prompts.

@github-actions github-actions Bot added type:bug +10 pts. Bug fix. area:ui @termuijs/ui labels Aug 4, 2026
@coderabbitai

coderabbitai Bot commented Aug 4, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The PR updates numeric handling in three UI paths: switch knob rounding, tree selection comparison, and prompt choice validation.

Changes

Numeric handling corrections

Layer / File(s) Summary
Selection comparison and prompt validation
packages/ui/src/TreeSelect.ts, packages/ui/src/prompts.ts
TreeSelect sorts array a numerically during comparison. promptSelect uses Number.isNaN to validate parsed choices.
Switch position rounding
packages/ui/src/Switch.ts
The switch adds Number.EPSILON before rounding animation progress for knob positioning.

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

Possibly related PRs

Suggested reviewers: karanjot786

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the fixes and testing, but it omits required package, GSSoC, build, and issue-closing details from the template. Add the package names, use Closes #3494``, complete the required checklist and GSSoC section, and include reviewer notes if applicable.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies a bug-fix pull request and matches the stated objective of fixing four TermUI bugs.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/ui/src/TreeSelect.ts`:
- Line 185: Replace the numeric comparator `a - b` in the sort function for
sortedA with a typed numeric-string comparator that converts string values to
numbers for comparison, then apply the same comparator to sortedB to ensure both
arrays use consistent comparison logic. This will properly handle numeric string
values like '10' and '2' so they compare correctly in strict type checking mode.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: e0e209ce-b500-4e3e-ab0a-9c0454ac0861

📥 Commits

Reviewing files that changed from the base of the PR and between 6c7584e and 3d6ca1e.

📒 Files selected for processing (3)
  • packages/ui/src/Switch.ts
  • packages/ui/src/TreeSelect.ts
  • packages/ui/src/prompts.ts

function _valuesEqual(a: string[], b: string[]): boolean {
if (a.length !== b.length) return false;
const sortedA = [...a].sort();
const sortedA = [...a].sort((a, b) => a - b);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

rg -n -C 3 'function _valuesEqual|sortedA|sortedB' packages/ui/src/TreeSelect.ts
fd -a 'tsconfig*.json' -x rg -n '"strict"\s*:\s*true' '{}'

Repository: Karanjot786/TermUI

Length of output: 713


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Inspect the helper and package TypeScript config/package.json metadata without executing repository code.
sed -n '170,195p' packages/ui/src/TreeSelect.ts
printf '\n--- relevant tsconfig files ---\n'
for f in $(fd 'tsconfig.*\.json' .); do
  printf '\n### %s\n' "$f"
  python3 - <<'PY' "$f"
import json, sys
p=sys.argv[1]
try:
    data=json.load(open(p))
except Exception as e:
    print(f"ERR: {e}")
    raise SystemExit(2)
print(p, data.get("compilerOptions", {}).get("strict"))
PY
done

Repository: Karanjot786/TermUI

Length of output: 5161


Use a typed comparator for both sorted arrays.

packages/ui is configured with strict type checking, and a - b on string[] values does not type-check. Sort both sortedA and sortedB with the same numeric-string comparator so values such as ['10', '2'] and ['2', '10'] compare equal.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/ui/src/TreeSelect.ts` at line 185, Replace the numeric comparator `a
- b` in the sort function for sortedA with a typed numeric-string comparator
that converts string values to numbers for comparison, then apply the same
comparator to sortedB to ensure both arrays use consistent comparison logic.
This will properly handle numeric string values like '10' and '2' so they
compare correctly in strict type checking mode.

Source: Coding guidelines

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:ui @termuijs/ui type:bug +10 pts. Bug fix.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant