Skip to content

Comments

refactor: use replaceAll in breadcrumbs#3282

Merged
kasya merged 8 commits intoOWASP:mainfrom
saichethana28:fix-breadcrumb-only
Jan 10, 2026
Merged

refactor: use replaceAll in breadcrumbs#3282
kasya merged 8 commits intoOWASP:mainfrom
saichethana28:fix-breadcrumb-only

Conversation

@saichethana28
Copy link
Contributor

Proposed change

Resolves #3253

I noticed a SonarCloud "Code Smell" regarding the use of global regular expressions for simple string replacements in our breadcrumb utility. This PR refactors that logic to use the modern .replaceAll() method.

What I did:

  • Swapped out .replace(/-/g, ...) and new RegExp(DATE_TOKEN, 'g') for the much cleaner .replaceAll().
  • Kept the "date protection" logic intact so that hyphens within dates (like 2026-01-10) aren't accidentally turned into spaces.

Why it’s better:
It makes the code a lot easier to read and gets rid of the extra complexity of the RegExp constructor. Plus, it moves us toward more modern ES2021 standards which are safer and more performant for this kind of work.

Checklist

  • Required: I read and followed the contributing guidelines
  • Required: I ran make check-test locally and all tests passed
  • I used AI for code, documentation, or tests in this PR

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 10, 2026

Summary by CodeRabbit

  • Refactor
    • Optimized breadcrumb utility code for improved maintainability and performance.

✏️ Tip: You can customize this high-level summary in your review settings.

Walkthrough

Introduced top-level constants DATE_TOKEN and DATE_PATTERN and updated formatBreadcrumbTitle to use DATE_PATTERN and String.replaceAll() for masking/restoring date hyphens; capitalization logic and function signature remain unchanged.

Changes

Cohort / File(s) Summary
Breadcrumb utility
frontend/src/utils/breadcrumb.ts
Add DATE_TOKEN and DATE_PATTERN; replace regex-based masking/restoration with String.replaceAll() using DATE_PATTERN and DATE_TOKEN; preserve capitalization and existing return type/behavior.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested reviewers

  • arkid15r
  • kasya
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: refactoring breadcrumb code to use the modern replaceAll method instead of global regex patterns.
Description check ✅ Passed The description provides context about the changes, explains the rationale, and outlines the specific improvements made to the breadcrumb utility.
Linked Issues check ✅ Passed The PR successfully addresses all requirements from #3253: replaces global regex replacements with replaceAll in breadcrumb.ts, maintains existing date-protection logic, and resolves the SonarCloud maintainability flag.
Out of Scope Changes check ✅ Passed All changes in the PR are scoped to breadcrumb.ts and directly address the linked issue; no unrelated or out-of-scope modifications are present.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

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

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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 and usage tips.

coderabbitai[bot]
coderabbitai bot previously approved these changes Jan 10, 2026
Copy link
Contributor

@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

🤖 Fix all issues with AI agents
In @frontend/src/utils/breadcrumb.ts:
- Around line 1-6: Fix the JSDoc block for the breadcrumb formatter by removing
the extra asterisk on the parameter line: change the malformed "* * @param" to
the correct "* @param" in the JSDoc for the function that formats breadcrumb
titles (the comment above the breadcrumb formatting function), ensuring the rest
of the comment lines keep a single leading asterisk and proper spacing.
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 655730f and a794c8a.

📒 Files selected for processing (1)
  • frontend/src/utils/breadcrumb.ts
🔇 Additional comments (2)
frontend/src/utils/breadcrumb.ts (2)

13-13: LGTM: Clean refactoring to replaceAll.

The change from match.replace(/-/g, DATE_TOKEN) to match.replaceAll('-', DATE_TOKEN) is functionally equivalent and improves readability. This aligns with the ES2021 standard and addresses the SonarCloud code-smell as intended.


18-18: LGTM: Cleaner token restoration with replaceAll.

Replacing segment.replace(new RegExp(DATE_TOKEN, 'g'), '-') with segment.replaceAll(DATE_TOKEN, '-') removes the RegExp constructor complexity while maintaining identical behavior. This is exactly the improvement the PR set out to achieve.

Copy link
Contributor

@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: 0

🧹 Nitpick comments (1)
frontend/src/utils/breadcrumb.ts (1)

17-20: (Optional) Consider hardening against DATE_TOKEN appearing in input.
If a user-provided breadcrumb contains __DATE_HYPHEN__, it’ll be “restored” into -. If that’s possible in your domain, use a less-collidable sentinel (e.g., include uncommon delimiters) or generate a token that’s guaranteed absent.

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a794c8a and aed9569.

📒 Files selected for processing (1)
  • frontend/src/utils/breadcrumb.ts
🔇 Additional comments (2)
frontend/src/utils/breadcrumb.ts (2)

1-6: Doc comment examples match the function behavior.
Nice to have the “date protection” example inline; it mirrors the implementation.


10-13: No action needed. The TypeScript configuration targets ES2023 (frontend/tsconfig.json), which fully supports replaceAll() (introduced in ES2021). No polyfill or transpilation is required for this code.

Likely an incorrect or invalid review comment.

coderabbitai[bot]
coderabbitai bot previously approved these changes Jan 10, 2026
@saichethana28
Copy link
Contributor Author

@coderabbitai review

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 10, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

coderabbitai[bot]
coderabbitai bot previously approved these changes Jan 10, 2026
coderabbitai[bot]
coderabbitai bot previously approved these changes Jan 10, 2026
@saichethana28
Copy link
Contributor Author

@coderabbitai review

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 10, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Contributor

@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: 0

🧹 Nitpick comments (1)
frontend/src/utils/breadcrumb.ts (1)

1-2: LGTM! Constants correctly defined.

The top-level DATE_TOKEN and DATE_PATTERN constants improve readability and align with the refactoring objectives. The g flag on DATE_PATTERN is required by replaceAll() when using a regex—without it, a TypeError would be thrown.

📝 Optional: Document the global flag requirement

Consider adding a brief comment to clarify why the g flag is necessary:

+// Note: The 'g' flag is required by String.replaceAll() when using a RegExp
 const DATE_PATTERN = /\d{4}-\d{1,2}(?:-\d{1,2})?/g

This can help future maintainers understand the constraint.

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 582b463 and 3f8006f.

📒 Files selected for processing (1)
  • frontend/src/utils/breadcrumb.ts
🔇 Additional comments (3)
frontend/src/utils/breadcrumb.ts (3)

14-14: LGTM! Date protection logic correctly refactored.

The use of replaceAll(DATE_PATTERN, ...) with a callback successfully protects date hyphens by replacing them with DATE_TOKEN. The nested match.replaceAll('-', DATE_TOKEN) using string search is clean and correct. This refactor achieves the objective of improved readability while preserving the original date-protection behavior.


19-19: LGTM! Date hyphen restoration is correct.

The replaceAll(DATE_TOKEN, '-') correctly restores original hyphens within date segments after splitting. This is cleaner and more readable than the previous new RegExp(DATE_TOKEN, 'g') approach, and it preserves the intended behavior.


14-14: ES2021 support is confirmed and requires no action.

The project's TypeScript configuration targets ES2023, which includes full support for String.prototype.replaceAll() (introduced in ES2021). No transpilation layer or polyfills are needed, confirming the refactor is compatible with the target environment.

@sonarqubecloud
Copy link

Copy link
Collaborator

@kasya kasya left a comment

Choose a reason for hiding this comment

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

@saichethana28 this works great! Thank you!

@kasya kasya enabled auto-merge January 10, 2026 19:54
@kasya kasya added this pull request to the merge queue Jan 10, 2026
Merged via the queue into OWASP:main with commit d912c99 Jan 10, 2026
27 checks passed
hussainjamal760 pushed a commit to hussainjamal760/Nest that referenced this pull request Jan 14, 2026
* refactor: use replaceAll in breadcrumbs

* docs: add comprehensive JSDoc and refactor logic to replaceAll

* docs: fix JSDoc formatting and remove extra asterisk

* refactor: final polish on spacing and docs

* chore: trigger final fresh CI scan

* refactor: optimize constants placement and use replaceAll

* refactor: achieve full replaceAll consistency and optimize regex
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replace String.replace with String.replaceAll in breadcrumb.ts

2 participants