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.

.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>
@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! 🙏

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