Skip to content

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

@ojii

Description

@ojii

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions