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

feat: Add user callback for handling invalid characters. #283

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ iconv.decode = function decode(buf, encoding, options) {
var res = decoder.write(buf);
var trail = decoder.end();

return trail ? (res + trail) : res;
var res = trail ? (res + trail) : res;
if (res.indexOf('�') !== -1 && options && options.invalidCharHandler) res = options.invalidCharHandler()
return res
}

iconv.encodingExists = function encodingExists(enc) {
Expand Down
14 changes: 14 additions & 0 deletions test/main-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ var testStringLatin1 = "Hello123!£Å÷×çþÿ¿®";
var testStringBase64 = "SGVsbG8xMjMh";
var testStringHex = "48656c6c6f31323321";

describe("user callback", function () {
it("Return nothing for issue#210#83", function () {
// when bad characters, return nothing instead of the ?.
// Like these:
// iconv.decode(Buffer.from([128]), 'utf8')
// iconv.decode(iconv.encode('ça va','utf8'), 'utf7')
assert.strictEqual(iconv.decode(Buffer.from([128]), 'utf8', {
invalidCharHandler: function () {
return ''
}
}), '');
})
})

describe("Generic UTF8-UCS2 tests", function() {

it("Return values are of correct types", function() {
Expand Down