@@ -78,4 +78,46 @@ describe('Utf8Tools', () => {
7878
7979 it ( 'can validate utf-8 bytes.' , testIsValidUtf8 ) ;
8080 } ) ;
81+
82+ it ( 'can truncate to utf8 byte lengths' , ( ) => {
83+ expect ( ( ) => Utf8Tools . truncateToUtf8ByteLength ( asciiString , - 1 ) ) . toThrow ( ) ;
84+
85+ const expected = {
86+ didTruncate : false ,
87+ truncatedString : hanziString ,
88+ truncatedBytes : hanziBytes ,
89+ } ;
90+ expect ( Utf8Tools . truncateToUtf8ByteLength ( hanziString , hanziBytes . length , true ) ) . toEqual ( expected ) ;
91+ expect ( Utf8Tools . truncateToUtf8ByteLength ( hanziString , hanziBytes . length , false ) ) . toEqual ( expected ) ;
92+ expect ( Utf8Tools . truncateToUtf8ByteLength ( hanziString , hanziBytes . length + 1 , true ) ) . toEqual ( expected ) ;
93+ expect ( Utf8Tools . truncateToUtf8ByteLength ( hanziString , hanziBytes . length + 1 , false ) ) . toEqual ( expected ) ;
94+
95+ expected . didTruncate = true ;
96+ expected . truncatedString = asciiString . substring ( 0 , asciiString . length - 1 ) ;
97+ expected . truncatedBytes = Utf8Tools . stringToUtf8ByteArray ( expected . truncatedString ) ;
98+ expect ( Utf8Tools . truncateToUtf8ByteLength ( asciiString , asciiBytes . length - 1 , false ) ) . toEqual ( expected ) ;
99+ expect ( Utf8Tools . truncateToUtf8ByteLength ( asciiBytes , asciiBytes . length - 1 , false ) ) . toEqual ( expected ) ;
100+ expect ( Utf8Tools . truncateToUtf8ByteLength ( asciiString , asciiBytes . length - 1 , true ) ) . toEqual ( expected ) ;
101+ expect ( Utf8Tools . truncateToUtf8ByteLength ( asciiBytes , asciiBytes . length - 1 , true ) ) . toEqual ( expected ) ;
102+
103+ expected . truncatedString = hanziString . substring ( 0 , hanziString . length - 1 ) ;
104+ expected . truncatedBytes = Utf8Tools . stringToUtf8ByteArray ( expected . truncatedString ) ;
105+ expect ( Utf8Tools . truncateToUtf8ByteLength ( hanziString , hanziBytes . length - 1 , false ) ) . toEqual ( expected ) ;
106+ expect ( Utf8Tools . truncateToUtf8ByteLength ( hanziBytes , hanziBytes . length - 1 , false ) ) . toEqual ( expected ) ;
107+
108+ expected . truncatedString = `${ hanziString . substring ( 0 , hanziString . length - 2 ) } …` ;
109+ expected . truncatedBytes = Utf8Tools . stringToUtf8ByteArray ( expected . truncatedString ) ;
110+ expect ( Utf8Tools . truncateToUtf8ByteLength ( hanziString , hanziBytes . length - 1 , true ) ) . toEqual ( expected ) ;
111+ expect ( Utf8Tools . truncateToUtf8ByteLength ( hanziBytes , hanziBytes . length - 1 , true ) ) . toEqual ( expected ) ;
112+
113+ expected . truncatedString = '' ;
114+ expected . truncatedBytes = Utf8Tools . stringToUtf8ByteArray ( expected . truncatedString ) ;
115+ expect ( Utf8Tools . truncateToUtf8ByteLength ( astralString , astralBytes . length - 1 , false ) ) . toEqual ( expected ) ;
116+ expect ( Utf8Tools . truncateToUtf8ByteLength ( astralBytes , astralBytes . length - 1 , false ) ) . toEqual ( expected ) ;
117+
118+ expected . truncatedString = '…' ;
119+ expected . truncatedBytes = Utf8Tools . stringToUtf8ByteArray ( expected . truncatedString ) ;
120+ expect ( Utf8Tools . truncateToUtf8ByteLength ( astralString , astralBytes . length - 1 , true ) ) . toEqual ( expected ) ;
121+ expect ( Utf8Tools . truncateToUtf8ByteLength ( astralBytes , astralBytes . length - 1 , true ) ) . toEqual ( expected ) ;
122+ } ) ;
81123} ) ;
0 commit comments