Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: Add an "error" fallback option to Encoding.convert to fail loudly if unsupported characters are encountered #43

Closed
ojii opened this issue Jun 6, 2024 · 3 comments · Fixed by #44

Comments

@ojii
Copy link

ojii commented Jun 6, 2024

We have a use-case where we have to check if a given string/content is valid ShiftJIS, currently we use this:

function isShiftJISConvertible(s) {
  return Encoding.convert(Encoding.convert(s, "SJIS", "UNICODE"), "UNICODE", "SJIS") === s;
}

but that is wasteful since we encode twice. It would be nice if there was an option so we could do

function isShiftJISConvertible(s) {
  try {
    Encoding.convert(s, {from: "UNICODE", to: "SJIS", fallback: "error"})
    return true
  } catch {
    return false
  }
}

Would you accept a pull request to implement such a feature?

@polygonplanet
Copy link
Owner

polygonplanet commented Jun 7, 2024

@ojii
encoding.js is primarily designed to simply ignore errors internally and avoid throwing unnecessary errors for most processes. But seeing the need for this feature, I plan to add a fallback: 'error' option in the next release.

@polygonplanet
Copy link
Owner

@ojii
I added this option in PR #44 . thanks for the issues!

@ojii
Copy link
Author

ojii commented Jun 10, 2024

thank you so much

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 a pull request may close this issue.

2 participants