Skip to content

fix: resolve 4 bugs in termui - #3684

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

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

Conversation

@saurabhhhcodes

@saurabhhhcodes saurabhhhcodes commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Description

This PR fixes real bugs found in the codebase:

  • Added explicit radix to parseInt: without 10, strings like '0x1F' or '08' parse in unintended bases.
  • Fixed default sort: .sort() coerces elements to strings, so [10, 9, 2] sorts as [10, 2, 9]; numeric comparator sorts correctly.
  • Added explicit radix to parseInt: without 10, strings like '0x1F' or '08' parse in unintended bases.
  • Added Number.EPSILON to Math.round: prevents floating-point drift (e.g. 1.005 * 100 rounding to 100 instead of 101).

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: #3683

Summary by CodeRabbit

  • Bug Fixes
    • Improved hexadecimal character reference parsing to correctly limit accepted digits.
    • Fixed percentage rounding so values near whole numbers display consistently.
    • Improved numeric keyboard navigation for reliable base-10 handling.
    • Corrected multi-select ordering when options have multi-digit positions.

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

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The changes apply four numeric correctness fixes: bounded hexadecimal entity parsing, stable percentage rounding, explicit decimal key parsing, and numeric sorting of selected option indices.

Changes

Numeric correctness fixes

Layer / File(s) Summary
Numeric input parsing
examples/rss-reader/src/index.tsx, examples/widget-gallery/src/index.ts
Hexadecimal parsing now reads at most eight digits. Numeric tab keys now use an explicit base-10 radix.
Numeric calculation and ordering
examples/todo-app/src/index.ts, packages/ui/src/MultiSelect.ts
Percentage rounding adds Number.EPSILON. Selected option indices now sort numerically in ascending order.

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

Possibly related PRs

Suggested reviewers: karanjot786

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the fixes and testing, but it omits required package information, the Closes # issue format, and most template checklist sections. Add the affected packages, change Ref: #3683 to `Closes `#3683, and complete the required checklist and GSSoC sections.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies a bug-fix change and follows the required type: short description format.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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
🧪 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: 2

🤖 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 `@examples/rss-reader/src/index.tsx`:
- Around line 30-31: Update the entity-decoding logic around codePoint to parse
the captured hexadecimal substring rather than a fixed slice, reject empty
values before calling Number.parseInt, and continue returning the original match
for invalid or non-finite values while correctly decoding 1–8 hex digits such as
&`#x10FFFF`;.

In `@examples/todo-app/src/index.ts`:
- Line 107: Update the percentage calculation in the progress-rendering logic
around pct so Number.EPSILON is multiplied by the same 100 scale as value before
Math.round. Preserve the existing conversion to an integer percentage while
ensuring boundary values such as 0.145 round to 15%.
🪄 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: 1bdb8051-8291-44e7-ac7c-baadd2da6f34

📥 Commits

Reviewing files that changed from the base of the PR and between 6c7584e and 0486dbe.

📒 Files selected for processing (4)
  • examples/rss-reader/src/index.tsx
  • examples/todo-app/src/index.ts
  • examples/widget-gallery/src/index.ts
  • packages/ui/src/MultiSelect.ts

Comment on lines +30 to 31
const codePoint = Number.parseInt(entity.slice(2, 10), 16);
return Number.isFinite(codePoint) ? String.fromCodePoint(codePoint) : match;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

node <<'NODE'
const codePoint = Number.parseInt('FFFFFFFF'.slice(0, 8), 16);
try {
  String.fromCodePoint(codePoint);
  process.exit(1);
} catch (error) {
  if (!(error instanceof RangeError)) throw error;
}
NODE

Repository: Karanjot786/TermUI

Length of output: 156


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Locate and inspect the target file and nearby entity-decoding logic.
if [ -f examples/rss-reader/src/index.tsx ]; then
  wc -l examples/rss-reader/src/index.tsx
  sed -n '1,80p' examples/rss-reader/src/index.tsx
else
  echo "missing examples/rss-reader/src/index.tsx"
  git ls-files | rg 'examples/rss-reader/src/index\.tsx|rss-reader' || true
fi

# Behavioral probe for Number.parseInt/Number.isFinite/String.fromCodePoint without executing repo code.
node <<'NODE'
const inputs = ['&`#xFFFFFFFF`;', '&`#x10FFFF00`;', '&`#x10FFFF`;', '&`#x110000`;', '&`#x012345678ABC`;'];
for (const entity of inputs) {
  const matched = entity.match(/^&`#x`([0-9A-Fa-f]+);$/);
  const codePoint = Number.parseInt(entity.slice(2, 10), 16);
  let s;
  let errClass;
  try {
    s = String.fromCodePoint(codePoint);
  } catch (error) {
    errClass = error.constructor.name;
  }
  console.log(JSON.stringify({
    entity,
    matched: matched && matched[1],
    parsedSlice8: codePoint,
    finite: Number.isFinite(codePoint),
    exceedsMaxPlane: matched && matched[1] ? Number.parseInt(matched[1], 16) > 0x10FFFF : null,
    parsedFull: matched ? Number.parseInt(matched[1], 16) : null,
    finiteFull: matched ? Number.isFinite(Number.parseInt(matched[1], 16)) : null,
    string: s,
    errorClass: errClass,
  }), null, 2);
}
NODE

Repository: Karanjot786/TermUI

Length of output: 3382


Reject invalid hexadecimal entities before decoding.

Hexadecimal entities that do not match exactly 1–8 hex digits cause Number.isFinite(...) to return false, so valid inputs such as &#x10FFFF; fall through to the replacement text. Use the captured hex substring as the parsed value and reject empty values before calling Number.parseInt.

🤖 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 `@examples/rss-reader/src/index.tsx` around lines 30 - 31, Update the
entity-decoding logic around codePoint to parse the captured hexadecimal
substring rather than a fixed slice, reject empty values before calling
Number.parseInt, and continue returning the original match for invalid or
non-finite values while correctly decoding 1–8 hex digits such as &`#x10FFFF`;.

const filled = Math.round(barWidth * value);

const pct = Math.round(value * 100);
const pct = Math.round(value * 100 + Number.EPSILON);

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 | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

node <<'NODE'
const value = 0.145;
const current = Math.round(value * 100 + Number.EPSILON);
if (current !== 15) {
  process.exit(1);
}
NODE

Repository: Karanjot786/TermUI

Length of output: 156


🏁 Script executed:

#!/bin/bash
set -euo pipefail

cat -n examples/todo-app/src/index.ts | sed -n '90,115p'
node --version
node - <<'NODE'
console.log({
  value: 0.145,
  product: 0.145 * 100,
  current: Math.round(0.145 * 100 + Number.EPSILON),
  beforeScale: Math.round((0.145 + Number.EPSILON) * 100),
  scaledEpsilon: Math.round(0.145 * 100 + Number.EPSILON * 100),
});
NODE

Repository: Karanjot786/TermUI

Length of output: 1605


Scale the epsilon before rounding the percentage.

Adding Number.EPSILON after value * 100 leaves boundary values like 0.145 * 100 at 14.499999999999998, and Math.round(... + Number.EPSILON) rounds them to 14% instead of 15%. Scale the epsilon with the percentage instead.

Proposed fix
-            const pct = Math.round(value * 100 + Number.EPSILON);
+            const pct = Math.round(value * 100 + Number.EPSILON * 100);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const pct = Math.round(value * 100 + Number.EPSILON);
const pct = Math.round(value * 100 + Number.EPSILON * 100);
🤖 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 `@examples/todo-app/src/index.ts` at line 107, Update the percentage
calculation in the progress-rendering logic around pct so Number.EPSILON is
multiplied by the same 100 scale as value before Math.round. Preserve the
existing conversion to an integer percentage while ensuring boundary values such
as 0.145 round to 15%.

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

Labels

area:examples Example apps. 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