From f52cca92149bde12b4797d483de364bc75817b8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Am=C3=A9rico?= Date: Sat, 16 Feb 2019 20:25:02 -0300 Subject: [PATCH] Use valueOf when converting String object values --- lib/object.js | 4 ++-- tests/unit/object.spec.js | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 tests/unit/object.spec.js diff --git a/lib/object.js b/lib/object.js index 16ba8606b..8b62d4ca8 100644 --- a/lib/object.js +++ b/lib/object.js @@ -56,9 +56,9 @@ class PDFObject { // If so, encode it as big endian UTF-16 let stringBuffer; if (isUnicode) { - stringBuffer = swapBytes(new Buffer(`\ufeff${string}`, 'utf16le')); + stringBuffer = swapBytes(Buffer.from(`\ufeff${string}`, 'utf16le')); } else { - stringBuffer = new Buffer(string, 'ascii'); + stringBuffer = Buffer.from(string.valueOf(), 'ascii'); } // Encrypt the string when necessary diff --git a/tests/unit/object.spec.js b/tests/unit/object.spec.js new file mode 100644 index 000000000..21c7b64ef --- /dev/null +++ b/tests/unit/object.spec.js @@ -0,0 +1,23 @@ +const PDFObject = require('../../lib/object').default; + +describe('PDFObject', () => { + describe('convert', () => { + test('string literal', () => { + expect(PDFObject.convert('test')).toEqual('/test'); + }); + + test('string literal with unicode', () => { + expect(PDFObject.convert('αβγδ')).toEqual('/αβγδ'); + }); + + test('String object', () => { + expect(PDFObject.convert(new String('test'))).toEqual('(test)'); + }); + + test('String object with unicode', () => { + const result = PDFObject.convert(new String('αβγδ')); + expect(result.length).toEqual(12); + expect(result).toMatchInlineSnapshot(`"(þÿ±²³´)"`); + }); + }); +});