Skip to content

Commit

Permalink
Merge pull request #291 from jstastny/js/fix-utf8-size
Browse files Browse the repository at this point in the history
Correctly calculate byte size of the utf8 strings
  • Loading branch information
perry-mitchell authored May 16, 2022
2 parents 1f9316f + 529c34f commit cb9e9c1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion source/tools/size.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function calculateDataLength(data: string | BufferLike): number {
} else if (isBuffer(data)) {
return (<Buffer>data).length;
} else if (typeof data === "string") {
return (<string>data).length;
return Buffer.byteLength(<string>data);
}
throw new Layerr(
{
Expand Down
8 changes: 8 additions & 0 deletions test/node/tools/size.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { calculateDataLength } = require("../../../dist/node/tools/size.js");

describe("calculateDataLength", () => {
it("Correctly calculates length for utf-8 string", () => {
const utf8String = "řeřicha";
expect(calculateDataLength(utf8String)).to.equal(9);
});
});

0 comments on commit cb9e9c1

Please sign in to comment.