From acc318761e2a24982ddc12e5055e489134213acd Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 12 Feb 2019 11:28:52 +0100 Subject: [PATCH] Fix encodedLength for non-ascii strings (#684) --- packages/types/src/Text.spec.ts | 14 ++++++++++++++ packages/types/src/Text.ts | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/types/src/Text.spec.ts b/packages/types/src/Text.spec.ts index 9654d16ce0..68f77f0027 100644 --- a/packages/types/src/Text.spec.ts +++ b/packages/types/src/Text.spec.ts @@ -43,5 +43,19 @@ describe('Text', () => { it('compares actual non-string values (fails)', () => { expect(new Text('123').eq(123)).toBe(false); }); + + it('calulates the length & encoded length correctly for ASCII', () => { + const test = new Text('abcde'); + + expect(test.encodedLength).toEqual(6); + expect(test).toHaveLength(5); + }); + + it('calulates the length & encoded length correctly for non-ASCII', () => { + const test = new Text('中文'); + + expect(test.encodedLength).toEqual(7); + expect(test).toHaveLength(2); + }); }); }); diff --git a/packages/types/src/Text.ts b/packages/types/src/Text.ts index 746b2de518..42a3acbc99 100644 --- a/packages/types/src/Text.ts +++ b/packages/types/src/Text.ts @@ -43,7 +43,7 @@ export default class Text extends String implements Codec { * @description The length of the value when encoded as a Uint8Array */ get encodedLength (): number { - return this.length + Compact.encodeU8a(this.length).length; + return this.toU8a().length; } /**