Skip to content
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
3 changes: 2 additions & 1 deletion src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,8 @@ function dynamicReplacement(_file_rr, _config_rr, _data_rr) {
}

if (needsByteOrSize && -1 === _bytes) {
_bytes = Buffer.from(_text).length;
// The size in the encoding of the file, not of the text as utf8
_bytes = Buffer.byteLength(_text, _config_rr.encoding);
_size = readableSize(_bytes);
}

Expand Down
26 changes: 13 additions & 13 deletions test/js/encoding.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,18 @@ describe('--encoding', () => {
expect((await rr('æ', 'text.length + text', config)).out).toBe('f3fæoo\n');
});

test.todo(
'BUG #688: with --replacement-js `bytes` is the size of the file in its encoding',
async () => {
fs.writeFileSync('my.file', latin1());
const config = {
includeGlob: ['my.file'],
encoding: 'latin1',
replacementJs: true,
output: true,
};
expect((await rr('^', 'bytes + ":"', config)).out).toBe('3:fæo');
}
);
test('with --replacement-js `bytes` and `size` are the size of the file in its encoding (#688)', async () => {
// 1500 times `æ`: one byte each in latin1, but two in utf8 (that would give 3000 and 2.9 KB)
fs.writeFileSync('my.file', Buffer.alloc(1500, 0xe6));
const config = {
includeGlob: ['my.file'],
encoding: 'latin1',
replacementJs: true,
output: true,
};
expect((await rr('^', 'bytes + ":" + size + ":"', config)).out).toBe(
'1500:1.5 KB:' + 'æ'.repeat(1500) + '\n'
);
});
});
});
Loading