@@ -901,7 +901,13 @@ export function utf8ToBytes(str: string): Uint8Array {
901901 return new Uint8Array ( new TextEncoder ( ) . encode ( str ) ) ; // https://bugzil.la/1681809
902902}
903903
904- function encode ( ver : Version , ecc : ErrorCorrection , data : string , type : EncodingType ) : Uint8Array {
904+ function encode (
905+ ver : Version ,
906+ ecc : ErrorCorrection ,
907+ data : string ,
908+ type : EncodingType ,
909+ encoder : ( value : string ) => Uint8Array = utf8ToBytes
910+ ) : Uint8Array {
905911 let encoded = '' ;
906912 let dataLen = data . length ;
907913 if ( type === 'numeric' ) {
@@ -919,7 +925,7 @@ function encode(ver: Version, ecc: ErrorCorrection, data: string, type: Encoding
919925 for ( let i = 0 ; i < n - 1 ; i += 2 ) encoded += bin ( t [ i ] * 45 + t [ i + 1 ] , 11 ) ;
920926 if ( n % 2 == 1 ) encoded += bin ( t [ n - 1 ] , 6 ) ; // pad if odd number of chars
921927 } else if ( type === 'byte' ) {
922- const utf8 = utf8ToBytes ( data ) ;
928+ const utf8 = encoder ( data ) ;
923929 dataLen = utf8 . length ;
924930 encoded = Array . from ( utf8 )
925931 . map ( ( i ) => bin ( i , 8 ) )
@@ -1041,6 +1047,7 @@ function drawQRBest(ver: Version, ecc: ErrorCorrection, data: Uint8Array, maskId
10411047export type QrOpts = {
10421048 ecc ?: ErrorCorrection | undefined ;
10431049 encoding ?: EncodingType | undefined ;
1050+ textEncoder ?: ( text : string ) => Uint8Array ;
10441051 version ?: Version | undefined ;
10451052 mask ?: number | undefined ;
10461053 border ?: number | undefined ;
@@ -1112,13 +1119,13 @@ export function encodeQR(text: string, output: Output = 'raw', opts: QrOpts & Sv
11121119 err = new Error ( 'Unknown error' ) ;
11131120 if ( ver !== undefined ) {
11141121 validateVersion ( ver ) ;
1115- data = encode ( ver , ecc , text , encoding ) ;
1122+ data = encode ( ver , ecc , text , encoding , opts . textEncoder ) ;
11161123 } else {
11171124 // If no version is provided, try to find smallest one which fits
11181125 // Currently just scans all version, can be significantly speedup if needed
11191126 for ( let i = 1 ; i <= 40 ; i ++ ) {
11201127 try {
1121- data = encode ( i , ecc , text , encoding ) ;
1128+ data = encode ( i , ecc , text , encoding , opts . textEncoder ) ;
11221129 ver = i ;
11231130 break ;
11241131 } catch ( e ) {
0 commit comments