From 46f3f243c76396593da7ea6a24270edbcd02e25d Mon Sep 17 00:00:00 2001 From: yosion-p Date: Fri, 24 Sep 2021 14:26:45 +0800 Subject: [PATCH] feat: first commit - add user callback for decode --- lib/index.js | 4 +++- test/main-test.js | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 657701c3..d6cf529e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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) { diff --git a/test/main-test.js b/test/main-test.js index 060bd91d..05fdfec4 100644 --- a/test/main-test.js +++ b/test/main-test.js @@ -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() {