Skip to content

Fix supressing leading zeros in Collection Object's Catalog Number in data entry#8027

Open
alesan99 wants to merge 3 commits intomainfrom
issue-1707
Open

Fix supressing leading zeros in Collection Object's Catalog Number in data entry#8027
alesan99 wants to merge 3 commits intomainfrom
issue-1707

Conversation

@alesan99
Copy link
Copy Markdown
Contributor

@alesan99 alesan99 commented Apr 27, 2026

Fixes #1707

Checklist

  • Self-review the PR after opening it to make sure the changes look good and
    self-explanatory (or properly documented)
  • Add relevant issue to release milestone
  • Add pr to documentation list
  • Add automated tests

Testing instructions

  • Go to app resources -> Record formatters -> Collection Object. Click the gear next to the catalogNumber field formatter and enable Trim Zeros.
  • On data entry, the title of the unsaved CO should read "Collection Object"
  • Save the collection object
  • On data entry, the title of the saved CO should read something like "Collection Object: 1"
  • Disable Trim Zeros
  • On data entry, the title of the unsaved CO should read "Collection Object"
  • Save the collection object
  • On data entry, the title of the saved CO should read something like "Collection Object: 000000001"
  • Generally test table formatters in the preview to make sure they are okay. You don't need to test formatters in query results.

Summary by CodeRabbit

  • Bug Fixes
    • Improved trimming of trailing zeros for formatted numbers: empty, whitespace-only, non-numeric, or unsafe-integer values are now preserved instead of being coerced, preventing incorrect or inconsistent displays.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 27, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 0204c812-45e2-4cf8-9482-f9cf29c14592

📥 Commits

Reviewing files that changed from the base of the PR and between a744beb and a397f71.

📒 Files selected for processing (1)
  • specifyweb/frontend/js_src/lib/components/Formatters/formatters.ts

📝 Walkthrough

Walkthrough

Refines the numeric zero-trimming logic in formatField so that trimming/normalization only occurs when formatted is non-empty/non-whitespace, parses to a number that is not NaN, and is a safe integer; otherwise the original formatted value is preserved.

Changes

Cohort / File(s) Summary
Numeric Formatter Logic
specifyweb/frontend/js_src/lib/components/Formatters/formatters.ts
Updated trimZeros handling: compute num from formatted and only apply num.toString() when formatted is present and not whitespace, num is not NaN, and Number.isSafeInteger(num); avoid coercion for null/undefined/whitespace/non-numeric/unsafe-integer inputs.
🚥 Pre-merge checks | ✅ 4 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Automatic Tests ⚠️ Warning The PR modifies trimZeros logic but lacks automatic tests verifying the new safety checks with trimZeros enabled. Add comprehensive unit tests covering trimZeros behavior including numeric values, non-numeric inputs, NaN cases, whitespace, unsafe integers, and null/undefined values.
Testing Instructions ⚠️ Warning Testing instructions are incomplete with empty placeholder comments and missing automated tests for trimZeros edge cases. Document step-by-step UI navigation for Trim Zeros configuration, add manual verification steps with example catalog numbers, implement automated unit tests covering edge cases, and specify all UI components requiring verification.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: fixing suppression of leading zeros in the Catalog Number formatter, which aligns with the PR objectives.
Linked Issues check ✅ Passed The code changes address issue #1707 by modifying the trimZeros logic to conditionally suppress leading zeros based on numeric safety, preventing precision loss.
Out of Scope Changes check ✅ Passed All changes in the formatters.ts file are directly related to fixing the leading zeros suppression behavior as specified in issue #1707.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch issue-1707

Review rate limit: 9/10 reviews remaining, refill in 6 minutes.

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

@alesan99 alesan99 marked this pull request as ready for review April 27, 2026 20:40
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
specifyweb/frontend/js_src/lib/components/Formatters/formatters.ts (1)

199-202: Add tests for trimZeros: true edge cases introduced by this change.

This branch now treats whitespace-only values differently, but current formatField tests (see specifyweb/frontend/js_src/lib/components/Formatters/__tests__/formatters.test.ts:79-150) only cover trimZeros: false. Please add cases for trimZeros: true (whitespace-only, normal zero-padded numeric strings, and long numeric strings).

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@specifyweb/frontend/js_src/lib/components/Formatters/formatters.ts` around
lines 199 - 202, Add unit tests in
specifyweb/frontend/js_src/lib/components/Formatters/__tests__/formatters.test.ts
that exercise the new trimZeros:true behavior in formatField/formatters (cover
whitespace-only, zero-padded numeric strings, and very long numeric strings).
Specifically, create tests calling formatField (or the exported formatter that
uses the trimZeros branch) with trimZeros: true and assert that: a) a
whitespace-only input (e.g., "   ") is returned unchanged, b) a zero-padded
numeric string (e.g., "000123.4500") is normalized to Number(...).toString()
(e.g., "123.45"), and c) a very long numeric string is converted to the same
value produced by Number(longString).toString() (assert using
Number(longString).toString() to avoid precision assumptions). Use the existing
test file structure and naming pattern to add these three cases.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@specifyweb/frontend/js_src/lib/components/Formatters/formatters.ts`:
- Around line 199-202: The code uses Number(formatted).toString() when trimZeros
is true which can lose precision for long numeric IDs; switch to string-based
trimming: treat formatted as a string (String(formatted)), and if it contains a
decimal point remove only trailing fractional zeros and a trailing dot (e.g.
formatted = String(formatted).replace(/(\.\d*?[1-9])0+$|\.0+$/,'$1')), leaving
integer strings untouched so large integers are preserved; update the logic
around the trimZeros branch (the variable formatted and its handling) and add
unit tests covering trimZeros: true for integer-like long IDs and decimal cases
to ensure no precision loss and correct zero trimming.

---

Nitpick comments:
In `@specifyweb/frontend/js_src/lib/components/Formatters/formatters.ts`:
- Around line 199-202: Add unit tests in
specifyweb/frontend/js_src/lib/components/Formatters/__tests__/formatters.test.ts
that exercise the new trimZeros:true behavior in formatField/formatters (cover
whitespace-only, zero-padded numeric strings, and very long numeric strings).
Specifically, create tests calling formatField (or the exported formatter that
uses the trimZeros branch) with trimZeros: true and assert that: a) a
whitespace-only input (e.g., "   ") is returned unchanged, b) a zero-padded
numeric string (e.g., "000123.4500") is normalized to Number(...).toString()
(e.g., "123.45"), and c) a very long numeric string is converted to the same
value produced by Number(longString).toString() (assert using
Number(longString).toString() to avoid precision assumptions). Use the existing
test file structure and naming pattern to add these three cases.
🪄 Autofix (Beta)

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: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 0b121e1f-c917-4242-b761-2e5ad6f83bc0

📥 Commits

Reviewing files that changed from the base of the PR and between b50a592 and a744beb.

📒 Files selected for processing (1)
  • specifyweb/frontend/js_src/lib/components/Formatters/formatters.ts

Comment thread specifyweb/frontend/js_src/lib/components/Formatters/formatters.ts Outdated
@github-project-automation github-project-automation Bot moved this from 📋Back Log to Dev Attention Needed in General Tester Board Apr 27, 2026
@alesan99 alesan99 requested review from a team May 1, 2026 18:55
@alesan99 alesan99 added this to the 7.12.2 milestone May 1, 2026
Copy link
Copy Markdown
Collaborator

@emenslin emenslin left a comment

Choose a reason for hiding this comment

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

  • On data entry, the title of the unsaved CO should read "Collection Object"
  • On data entry, the title of the saved CO should read something like "Collection Object: 1"
  • On data entry, the title of the unsaved CO should read "Collection Object"
  • On data entry, the title of the saved CO should read something like "Collection Object: 000000001"
  • Generally test table formatters in the preview to make sure they are okay. You don't need to test formatters in query results.

Looks good!

@emenslin emenslin requested a review from a team May 4, 2026 13:48
Copy link
Copy Markdown
Collaborator

@bhumikaguptaa bhumikaguptaa left a comment

Choose a reason for hiding this comment

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

  • On data entry, the title of the unsaved CO should read "Collection Object"
  • On data entry, the title of the saved CO should read something like "Collection Object: 1"
  • On data entry, the title of the unsaved CO should read "Collection Object"
  • On data entry, the title of the saved CO should read something like "Collection Object: 000000001"
  • Generally test table formatters in the preview to make sure they are okay. You don't need to test formatters in query results.

Works as expected!

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

Labels

None yet

Projects

Status: Dev Attention Needed

Development

Successfully merging this pull request may close these issues.

Catalog Number Numeric formatter should suppress leading zeros in UI.

3 participants