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

Use valueOf when converting String object values #925

Merged
merged 1 commit into from
Feb 16, 2019
Merged
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: 2 additions & 2 deletions lib/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/object.spec.js
Original file line number Diff line number Diff line change
@@ -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(`"(þÿ±²³´)"`);
});
});
});