11export {
22 // public
33 encode , decode ,
4- BIT_LENGTH , ALPHABET_LENGTH ,
4+ LETTER_LENGTH , ALPHABET_LENGTH ,
55 // private
66 _0To31NumberFromCharacter ,
77 _0To31NumbersFromString ,
@@ -19,8 +19,8 @@ const EXTRAS = [
1919 `\n` ,
2020 undefined , // empty
2121] ;
22- const BIT_LENGTH = 5 ;
23- const BYTE = 8 ;
22+ const LETTER_LENGTH = 5 ;
23+ const BYTE_LENGTH = 8 ;
2424
2525const _0To31NumberFromCharacter = character => {
2626 const numberRepresentation = character . charCodeAt ( 0 ) - UTF_OFFSET_A ;
@@ -48,52 +48,52 @@ const stringFrom0To31Numbers = numbers => {
4848 } ) . join ( `` ) ;
4949} ;
5050
51- const addNumberIntoByte = ( number , existingByte = 0 , offset , BitLength = BIT_LENGTH ) => {
51+ const addNumberIntoByte = ( number , existingByte = 0 , offset , BitLength = LETTER_LENGTH ) => {
5252
5353} ;
5454
5555const _compactUInt8ArrayFrom0To31Numbers = _0To31Numbers => {
56- const byteLength = Math . ceil ( ( _0To31Numbers . length * BIT_LENGTH ) / BYTE ) ;
56+ const byteLength = Math . ceil ( ( _0To31Numbers . length * LETTER_LENGTH ) / BYTE_LENGTH ) ;
5757 const compactUInt8Array = new Uint8Array ( byteLength ) ;
5858
5959 let bitPosition = 0 ;
60- let currentNumberToBeSaved = 0 ;
60+ let numberToBeSaved = 0 ;
6161 let byteIndex = 0 ;
6262
6363 const saveByte = ( number ) => {
6464 console . log ( `save ${ number } (${ number . toString ( 2 ) . padStart ( 8 , "0" ) } )` )
6565 compactUInt8Array [ byteIndex ] = number ;
6666 byteIndex += 1 ;
67- currentNumberToBeSaved = 0 ;
67+ numberToBeSaved = 0 ;
6868 bitPosition = 0 ;
6969 } ;
7070 _0To31Numbers . forEach ( number => {
71- if ( bitPosition + BIT_LENGTH <= BYTE ) { // can fit at once
72- const toShift = BYTE - BIT_LENGTH - bitPosition ;
73- currentNumberToBeSaved += number << toShift ;
74- bitPosition += BIT_LENGTH ;
71+ if ( bitPosition + LETTER_LENGTH <= BYTE_LENGTH ) { // can fit at once
72+ const toShift = BYTE_LENGTH - LETTER_LENGTH - bitPosition ;
73+ numberToBeSaved += number << toShift ;
74+ bitPosition += LETTER_LENGTH ;
7575
76- if ( bitPosition === BYTE ) {
77- saveByte ( currentNumberToBeSaved ) ;
76+ if ( bitPosition === BYTE_LENGTH ) {
77+ saveByte ( numberToBeSaved ) ;
7878 }
7979 } else { // cannot fit at once
80- const shiftRight = - BYTE + bitPosition + BIT_LENGTH ; // 2
81- const missedBits = BIT_LENGTH - shiftRight ; // 3
80+ const shiftRight = - BYTE_LENGTH + bitPosition + LETTER_LENGTH ; // 2
81+ const missedBits = LETTER_LENGTH - shiftRight ; // 3
8282 const clamped = number >> shiftRight ;
83- currentNumberToBeSaved += clamped ;
83+ numberToBeSaved += clamped ;
8484
85- saveByte ( currentNumberToBeSaved ) ;
85+ saveByte ( numberToBeSaved ) ;
8686
8787 const substractHead = clamped << shiftRight ;
8888 const rest = number - substractHead ;
89- currentNumberToBeSaved += rest << ( BYTE + 1 ) - missedBits ;
89+ numberToBeSaved += rest << ( BYTE_LENGTH + 1 ) - missedBits ;
9090 bitPosition += missedBits - 1 ; //3
9191 }
9292 } ) ;
9393 // todo handle all cases
94- // console.log(90,currentNumberToBeSaved )
95- if ( currentNumberToBeSaved ) {
96- saveByte ( currentNumberToBeSaved ) ;
94+ // console.log(90,numberToBeSaved )
95+ if ( numberToBeSaved ) {
96+ saveByte ( numberToBeSaved ) ;
9797 } //else {
9898 // compactUInt8Array[byteIndex] = remainder;
9999 // }
@@ -104,7 +104,7 @@ const _compactUInt8ArrayFrom0To31Numbers = _0To31Numbers => {
104104const _0To31NumbersFromCompactUInt8Array = compactUInt8Array => {
105105 const byteLength = compactUInt8Array . length ;
106106 const _0To31Numbers = [ ] ;
107- const enoughSpace = BYTE - BIT_LENGTH ;
107+ const enoughSpace = BYTE_LENGTH - LETTER_LENGTH ;
108108
109109 const masks = [
110110 0b11111111 ,
@@ -121,26 +121,26 @@ const _0To31NumbersFromCompactUInt8Array = compactUInt8Array => {
121121 let bitsScanned = 0 ;
122122 compactUInt8Array . forEach ( ( uInt8 , i ) => {
123123 console . log ( i , uInt8 , 'uInt8' )
124- while ( offset !== BYTE && ! ( i === byteLength - 1 && offset > enoughSpace ) ) {
125- if ( BIT_LENGTH - bitsScanned <= BYTE - offset ) {
124+ while ( offset !== BYTE_LENGTH && ! ( i === byteLength - 1 && offset > enoughSpace ) ) {
125+ if ( LETTER_LENGTH - bitsScanned <= BYTE_LENGTH - offset ) {
126126 let number ;
127127 if ( bitsScanned === 0 ) {
128128 number = ( uInt8 & masks [ offset ] ) >> ( enoughSpace - offset ) ;
129- offset += BIT_LENGTH ;
129+ offset += LETTER_LENGTH ;
130130 // console.log(number, offset)
131131 console . log ( i , number , 'full' )
132132 } else {
133- const scanning = BIT_LENGTH - bitsScanned ;
134- number = remainder + ( uInt8 >> ( BYTE - scanning ) ) ;
133+ const scanning = LETTER_LENGTH - bitsScanned ;
134+ number = remainder + ( uInt8 >> ( BYTE_LENGTH - scanning ) ) ;
135135 offset += scanning ;
136- console . log ( i , number , 'rest' , ( uInt8 >> ( BYTE - scanning ) ) )
136+ console . log ( i , number , 'rest' , ( uInt8 >> ( BYTE_LENGTH - scanning ) ) )
137137 remainder = 0 ;
138138 bitsScanned = 0 ;
139139 }
140140 _0To31Numbers . push ( number ) ;
141141 } else {
142- bitsScanned = BYTE - offset ;
143- remainder = ( uInt8 & masks [ offset ] ) << ( BIT_LENGTH - bitsScanned ) ;
142+ bitsScanned = BYTE_LENGTH - offset ;
143+ remainder = ( uInt8 & masks [ offset ] ) << ( LETTER_LENGTH - bitsScanned ) ;
144144 offset += bitsScanned ;
145145 console . log ( i , 'scanning +' , remainder ) ;
146146 }
@@ -159,15 +159,15 @@ const _bytesRequiredToSaveNumber = number => {
159159 bits += 1 ;
160160 divided = divided >> 1 ;
161161 }
162- const bytesRequired = Math . ceil ( bits / BYTE ) ;
162+ const bytesRequired = Math . ceil ( bits / BYTE_LENGTH ) ;
163163 console . log ( "bits required" , bits ) ;
164164 console . log ( "bytes required" , bytesRequired ) ;
165165 return bytesRequired ;
166166} ;
167167
168168const _initializeUint8WithLength = characters => {
169- const textBitLength = characters * BIT_LENGTH ;
170- const textByteLength = Math . ceil ( textBitLength / BYTE ) ;
169+ const textBitLength = characters * LETTER_LENGTH ;
170+ const textByteLength = Math . ceil ( textBitLength / BYTE_LENGTH ) ;
171171 const bytesRequiredForTextByteLength = _bytesRequiredToSaveNumber ( textByteLength ) ;
172172 let totalByteLength = 1 + bytesRequiredForTextByteLength + textByteLength ;
173173 let ArrayConstructor ;
@@ -177,12 +177,12 @@ const _initializeUint8WithLength = characters => {
177177 ArrayConstructor = Uint16Array ;
178178 totalByteLength += 1 ; // see todo
179179 } else if ( bytesRequiredForTextByteLength === 3 ) {
180- console . warn ( `unhandled case byte ${ Byte } === 3` ) ;
180+ console . warn ( `unhandled case BYTE_LENGTH ${ BYTE_LENGTH } === 3` ) ;
181181 } else if ( bytesRequiredForTextByteLength === 4 ) {
182182 ArrayConstructor = Uint32Array ;
183183 totalByteLength += 3 ; // see todo
184184 } else if ( bytesRequiredForTextByteLength > 4 ) {
185- console . warn ( `unhandled case byte ${ Byte } > 4` ) ;
185+ console . warn ( `unhandled case BYTE_LENGTH ${ BYTE_LENGTH } > 4` ) ;
186186 }
187187 const uInt8WithLength = new Uint8Array ( totalByteLength ) ;
188188 uInt8WithLength [ 0 ] = bytesRequiredForTextByteLength ;
0 commit comments