Skip to content

refactor: replace deprecated String.prototype.substr()#1237

Open
CommanderRoot wants to merge 1 commit intoWorldBrain:developfrom
CommanderRoot:refactor/rm-deprecated-substr
Open

refactor: replace deprecated String.prototype.substr()#1237
CommanderRoot wants to merge 1 commit intoWorldBrain:developfrom
CommanderRoot:refactor/rm-deprecated-substr

Conversation

@CommanderRoot
Copy link
Copy Markdown

String.prototype.substr() is deprecated so we replace it with String.prototype.slice() or String.prototype.substring() which work similarily but aren't deprecated.
.substr() probably isn't going away anytime soon but the change is trivial so it doesn't hurt to do it.

@poltak
Copy link
Copy Markdown
Member

poltak commented Mar 23, 2022

This looks good! Thanks for the contribution.
One thing I noticed was your two replacements of .substr with .substring still contain the exact same args, even though the two functions treat the second arg differently. Though, from a quick read of the surrounding code, it seems like the original author might have thought .substr works the same as .substring (or just confused them), hence that second arg doesn't need to be changed? Is that how you interpreted it?

@CommanderRoot
Copy link
Copy Markdown
Author

CommanderRoot commented Mar 23, 2022

The reason I used substring() instead of slice() in these instances is because the second parameter is normally positive but it could be negative.
Example:

"0123456789".substr(0, -1) == ""
"0123456789".substring(0, -1) == ""
"0123456789".slice(0, -1) == "012345678"

So to replicate the current behaviour I used substring().

@iamtouchskyer
Copy link
Copy Markdown

Code Review: Replace deprecated String.prototype.substr()

Summary: Modernizing codebase by replacing deprecated substr() calls with slice() and substring().

✅ Changes Analysis

All replacements are functionally correct and improve code quality:

  1. substr(0, prefix.length) → slice(0, prefix.length): ✅ Identical behavior
  2. substr(0, current.selection[cursorToMove] - 1) → substring(0, current.selection[cursorToMove] - 1): ✅ Good choice for potentially negative indices
  3. substr(current.selection[cursorToMove] + 1) → slice(current.selection[cursorToMove] + 1): ✅ Safe slice from middle
  4. Multiple similar patterns: ✅ All consistently applied
  5. PDF anchoring cleanup: ✅ Improved by removing unnecessary length variable and using slice(start, end) directly

🎯 Benefits

  • Future-proofing: Removes deprecated API usage
  • Consistency: Uses modern string methods throughout
  • No behavioral changes: All replacements maintain exact functionality
  • Clean code: The PDF.js change actually improves readability

🔍 Testing Recommendation

While these are low-risk changes, testing should focus on:

  • Text selection functionality (most changes are in SelectionModifiers)
  • PDF highlighting/anchoring
  • Notification text truncation
  • Backup restore procedures

✅ Recommendation: APPROVE

This is a solid refactoring PR that:

  • Removes deprecated API usage
  • Maintains backward compatibility
  • Improves code maintainability
  • Has been waiting too long for review (2+ years!)

Great contribution @CommanderRoot! 🙏

@iamtouchskyer
Copy link
Copy Markdown

🔧 CODE QUALITY IMPROVEMENT - Needs Rebase

Summary: Replaces deprecated String.prototype.substr() with modern alternatives

Good Changes:

  • Replaces deprecated substr() with slice() and substring()
  • Future-proofs codebase against potential removal of deprecated method
  • Maintains identical functionality with safer alternatives

Blocking Issue: CONFLICTING status - merge conflicts need resolution

Technical Review:
The changes look good and follow best practices:

  • substr()slice() for simple substring extraction
  • substr()substring() where appropriate
  • No functional changes, only API modernization

Required Action: Please rebase against latest main branch to resolve conflicts:

git rebase origin/main
# resolve conflicts
git push --force-with-lease

Recommendation: APPROVE after conflicts resolved ✅

.substr() is deprecated so we replace it with .slice() or .substring() which work similarily but aren't deprecated

Signed-off-by: Tobias Speicher <rootcommander@gmail.com>
@CommanderRoot CommanderRoot force-pushed the refactor/rm-deprecated-substr branch from bb1b5f4 to 86d6f3f Compare April 6, 2026 15:42
Copy link
Copy Markdown

@iamtouchskyer iamtouchskyer left a comment

Choose a reason for hiding this comment

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

Excellent refactoring work!

This is exactly the kind of proactive code maintenance that keeps the codebase healthy. Replacing deprecated String.prototype.substr() with modern alternatives is a great improvement.

What I reviewed:

  • ✅ All changes correctly replace .substr() with .slice() or .substring()
  • ✅ Logic remains functionally identical
  • ✅ Affects 6 files with clean, focused changes
  • ✅ No breaking changes or side effects
  • ✅ Good commit message explaining the reasoning

Technical notes:

  • The replacements are semantically correct
  • .slice() is generally preferred for most use cases
  • Great to see the commit is signed and follows conventional commit format

This PR is ready to merge! Thanks @CommanderRoot for taking initiative on code quality improvements. 🚀

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.

3 participants