@@ -365,33 +365,35 @@ const toISO88591Tests: ByteUtilTest<'toISO88591'>[] = [
365365 }
366366 }
367367] ;
368- const fromUTF8Tests : ByteUtilTest < 'fromUTF8 ' > [ ] = [
368+ const fromUTF8Tests : ByteUtilTest < 'encodeUTF8Into ' > [ ] = [
369369 {
370- name : 'should create buffer from utf8 input ' ,
371- inputs : [ Buffer . from ( 'abc\u{1f913}' , 'utf8' ) . toString ( 'utf8' ) ] ,
370+ name : 'should insert utf8 bytes into buffer ' ,
371+ inputs : [ Buffer . alloc ( 7 ) , 'abc\u{1f913}' , 0 ] ,
372372 expectation ( { output, error } ) {
373373 expect ( error ) . to . be . null ;
374- expect ( output ) . to . deep . equal ( Buffer . from ( 'abc\u{1f913}' , 'utf8' ) ) ;
374+ expect ( output ) . to . equal ( 7 ) ;
375+ expect ( this . inputs [ 0 ] ) . to . deep . equal ( Buffer . from ( 'abc\u{1f913}' , 'utf8' ) ) ;
375376 }
376377 } ,
377378 {
378- name : 'should return empty buffer for empty string input' ,
379- inputs : [ '' ] ,
379+ name : 'should return 0 and not modify input buffer ' ,
380+ inputs : [ Uint8Array . from ( [ 2 , 2 ] ) , '' , 0 ] ,
380381 expectation ( { output, error } ) {
381382 expect ( error ) . to . be . null ;
382- expect ( output ) . to . have . property ( 'byteLength' , 0 ) ;
383+ expect ( output ) . to . equal ( 0 ) ;
384+ expect ( this . inputs [ 0 ] ) . to . deep . equal ( Uint8Array . from ( [ 2 , 2 ] ) ) ;
383385 }
384386 } ,
385387 {
386- name : 'should return bytes with replacement character if string is not encodable' ,
387- inputs : [ '\u{1f913}' . slice ( 0 , 1 ) ] ,
388+ name : 'should insert replacement character bytes if string is not encodable' ,
389+ inputs : [ Uint8Array . from ( { length : 10 } , ( ) => 2 ) , '\u{1f913}' . slice ( 0 , 1 ) , 2 ] ,
388390 expectation ( { output, error } ) {
389391 expect ( error ) . to . be . null ;
390- expect ( output ) . to . have . property ( 'byteLength' , 3 ) ;
391- expect ( output ) . to . have . property ( '0 ' , 0xef ) ;
392- expect ( output ) . to . have . property ( '1 ' , 0xbf ) ;
393- expect ( output ) . to . have . property ( '2 ' , 0xbd ) ;
394- const backToString = Buffer . from ( output ! ) . toString ( 'utf8' ) ;
392+ expect ( output ) . to . equal ( 3 ) ;
393+ expect ( this . inputs [ 0 ] ) . to . have . property ( '2 ' , 0xef ) ;
394+ expect ( this . inputs [ 0 ] ) . to . have . property ( '3 ' , 0xbf ) ;
395+ expect ( this . inputs [ 0 ] ) . to . have . property ( '4 ' , 0xbd ) ;
396+ const backToString = Buffer . from ( this . inputs [ 0 ] . subarray ( 2 , 5 ) ) . toString ( 'utf8' ) ;
395397 const replacementCharacter = '\u{fffd}' ;
396398 expect ( backToString ) . to . equal ( replacementCharacter ) ;
397399 }
@@ -507,7 +509,7 @@ const table = new Map<keyof ByteUtils, ByteUtilTest<keyof ByteUtils>[]>([
507509 [ 'toHex' , toHexTests ] ,
508510 [ 'fromISO88591' , fromISO88591Tests ] ,
509511 [ 'toISO88591' , toISO88591Tests ] ,
510- [ 'fromUTF8 ' , fromUTF8Tests ] ,
512+ [ 'encodeUTF8Into ' , fromUTF8Tests ] ,
511513 [ 'toUTF8' , toUTF8Tests ] ,
512514 [ 'utf8ByteLength' , utf8ByteLengthTests ] ,
513515 [ 'randomBytes' , randomBytesTests ]
0 commit comments