diff --git a/.gitignore b/.gitignore index e9b4ce13..6a8a90b5 100644 --- a/.gitignore +++ b/.gitignore @@ -35,3 +35,4 @@ dist dev .nyc_output test-results.json +.idea/ \ No newline at end of file diff --git a/src/6_branch.js b/src/6_branch.js index 65222f4c..94fb647e 100644 --- a/src/6_branch.js +++ b/src/6_branch.js @@ -1340,20 +1340,22 @@ Branch.prototype['qrCode'] = wrap( this._api( resources.qrCode, utils.cleanLinkData(linkData), - function(error, rawBuffer) { - function QrCode() { } - - if (!error) { - QrCode['rawBuffer'] = rawBuffer; - QrCode['base64'] = function() { - // First Encode array buffer as UTF-8 String, then Base64 Encode - if (this['rawBuffer']) { - return btoa(String.fromCharCode.apply(null, new Uint8Array(this['rawBuffer']))); - } - throw Error('QrCode.rawBuffer is empty.'); - }; - } - return done(error || null, QrCode || null); + (error, rawBuffer) => { + if (error) return done(error, null); + + const QrCode = { + rawBuffer, + base64() { + if (!this.rawBuffer) throw new Error('QrCode.rawBuffer is empty.'); + // Encode the Uint8Array to a binary string, then convert to base64 + const binaryString = Array.from(new Uint8Array(this.rawBuffer)) + .map(byte => String.fromCharCode(byte)) + .join(''); + return btoa(binaryString); + } + }; + + return done(null, QrCode); } ); }