@@ -119,17 +119,14 @@ const fillEntropy = (entropyOffset: number, entropyBuffer: ArrayBuffer, entropyF
119119const valueAt = ( lOffset : number , nBits : number , puidBytes : PuidBytes ) : number => {
120120 const lByteNdx = floor ( lOffset / 8 )
121121 const lByte = puidBytes [ lByteNdx ]
122-
123122 const lBitNum = lOffset % 8
124- // eslint-disable-next-line functional/no-let
125- let rBitNum = lBitNum + nBits
126123
127- if ( rBitNum <= 8 ) {
128- return ( ( lByte << lBitNum ) & 0xff ) >> ( lBitNum + ( 8 - rBitNum ) )
124+ if ( lBitNum + nBits <= 8 ) {
125+ return ( ( lByte << lBitNum ) & 0xff ) >> ( 8 - nBits )
129126 }
130- rBitNum -= 8
131127
132128 const rByte = puidBytes [ lByteNdx + 1 ]
129+ const rBitNum = lBitNum + nBits - 8
133130
134131 const lValue = ( ( lByte << lBitNum ) & 0xff ) >> ( lBitNum - rBitNum )
135132 const rValue = rByte >> ( 8 - rBitNum )
@@ -153,7 +150,7 @@ export default (puidLen: number, puidChars: string, entropyFunction: EntropyFunc
153150 const mapper = new Array ( puidLen ) . fill ( 0 ) . map ( ( zero , ndx ) => zero + ndx )
154151
155152 if ( isPow2 ( nChars ) ) {
156- // When chars count is a power of 2, sliced bits always yield a valid char
153+ // When chars count is a power of 2, sliced bits always yield a valid value
157154 const bitsMuncher = ( ) => {
158155 entropyOffset = fillEntropy ( entropyOffset , entropyBuffer , entropyFunction )
159156 const codes = mapper . map ( ( ndx : number ) =>
@@ -170,33 +167,37 @@ export default (puidLen: number, puidChars: string, entropyFunction: EntropyFunc
170167 const puidShifts = bitShifts ( puidChars )
171168
172169 const acceptValue = ( value : number ) : AcceptValue => {
170+ // Value is valid if it is less than the number of characters
173171 if ( value < nChars ) {
174172 return [ true , nBitsPerChar ]
175173 }
174+
175+ // For invalid value, shift the minimal bits necessary to determine validity
176176 const bitShift = puidShifts . find ( ( bs ) => value < bs [ 0 ] )
177177 const shift = bitShift && bitShift [ 1 ]
178178 return [ false , shift || nBitsPerChar ]
179179 }
180180
181+ // When chars count not a power of 2, sliced bits may yield an invalid value
181182 const sliceValue = ( ) => {
182183 if ( nEntropyBits < entropyOffset + nBitsPerChar ) {
183184 entropyOffset = fillEntropy ( entropyOffset , entropyBuffer , entropyFunction )
184185 }
185186
186187 const slicedValue = valueAt ( entropyOffset , nBitsPerChar , entropyBytes )
188+
187189 const [ accept , shift ] = acceptValue ( slicedValue )
190+ // Returned shift is the minimal bits necessary to determine if slice value is valid
188191 entropyOffset += shift
189192
190193 if ( accept ) {
191194 return charsEncoder ( slicedValue )
192195 }
196+ // If value not acceptable, slice another
193197 return sliceValue ( )
194198 }
195199
196- const bitsMuncher = ( ) => {
197- const codes = mapper . map ( ( ) => sliceValue ( ) )
198- return String . fromCharCode ( ...codes )
199- }
200+ const bitsMuncher = ( ) => String . fromCharCode ( ...mapper . map ( ( ) => sliceValue ( ) ) )
200201
201202 return { success : bitsMuncher }
202203}
0 commit comments