Skip to content

Commit 4303232

Browse files
committed
Restructure create bits muncher
1 parent 410d3f4 commit 4303232

File tree

3 files changed

+12
-16
lines changed

3 files changed

+12
-16
lines changed

src/lib/bits.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type BitShifts = readonly BitShift[]
4242
//
4343
// Each value slice uses 6 bits of entropy. In bits, 36 is 100100.
4444
// Now suppose we slice the value 50. In bits, 50 is 110010.
45-
//
45+
//
4646
// Only two bits are necessary to determine 100100 < 110010
4747
//
4848
const bitShifts = (chars: string): BitShifts => {
@@ -90,6 +90,7 @@ const entropyByValues = (skipBytes: number, entropyBuffer: EntropyBuffer, source
9090
}
9191
}
9292

93+
// Fill passed entropy buffer using entropy function
9394
const fillEntropy = (entropyOffset: number, entropyBuffer: ArrayBuffer, entropyFunction: EntropyFunction): number => {
9495
const entropyBytes: PuidBytes = new Uint8Array(entropyBuffer)
9596

@@ -145,7 +146,7 @@ const valueAt = (lOffset: number, nBits: number, puidBytes: PuidBytes): number =
145146
return lValue + rValue
146147
}
147148

148-
export default (puidLen: number, puidChars: string, entropyFunction: EntropyFunction): PuidBitsMuncherResult => {
149+
export default (puidLen: number, puidChars: string, entropyFunction: EntropyFunction): PuidBitsMuncher => {
149150
const nBitsPerChar = bitsPerChar(puidChars)
150151
const nBitsPerPuid = nBitsPerChar * puidLen
151152
const nBytesPerPuid = ceil(nBitsPerPuid / 8)
@@ -160,20 +161,20 @@ export default (puidLen: number, puidChars: string, entropyFunction: EntropyFunc
160161
const nChars = puidChars.length
161162
const mapper = new Array(puidLen).fill(0).map((zero, ndx) => zero + ndx)
162163

164+
// When chars count is a power of 2, sliced bits always yield a valid value
163165
if (isPow2(nChars)) {
164-
// When chars count is a power of 2, sliced bits always yield a valid value
165-
const bitsMuncher = () => {
166+
return () => {
166167
entropyOffset = fillEntropy(entropyOffset, entropyBuffer, entropyFunction)
167168
const codes = mapper.map((ndx: number) =>
168169
charsEncoder(valueAt(entropyOffset + ndx * nBitsPerChar, nBitsPerChar, entropyBytes))
169170
)
170171
entropyOffset += nBitsPerPuid
171172
return String.fromCharCode(...codes)
172173
}
173-
174-
return { success: bitsMuncher }
175174
}
176175

176+
// When chars count not a power of 2, sliced bits may yield an invalid value
177+
177178
const nEntropyBits = 8 * entropyBytes.length
178179
const puidShifts = bitShifts(puidChars)
179180

@@ -189,14 +190,15 @@ export default (puidLen: number, puidChars: string, entropyFunction: EntropyFunc
189190
return [false, shift || nBitsPerChar]
190191
}
191192

192-
// When chars count not a power of 2, sliced bits may yield an invalid value
193+
// Slice value from entropy bytes
193194
const sliceValue = () => {
195+
// Add more entropy bytes if necessary
194196
if (nEntropyBits < entropyOffset + nBitsPerChar) {
195197
entropyOffset = fillEntropy(entropyOffset, entropyBuffer, entropyFunction)
196198
}
197199

198200
const slicedValue = valueAt(entropyOffset, nBitsPerChar, entropyBytes)
199-
201+
200202
const [accept, shift] = acceptValue(slicedValue)
201203
// Returned shift is the minimal bits necessary to determine if slice value is valid
202204
entropyOffset += shift
@@ -208,9 +210,7 @@ export default (puidLen: number, puidChars: string, entropyFunction: EntropyFunc
208210
return sliceValue()
209211
}
210212

211-
const bitsMuncher = () => String.fromCharCode(...mapper.map(() => sliceValue()))
212-
213-
return { success: bitsMuncher }
213+
return () => String.fromCharCode(...mapper.map(() => sliceValue()))
214214
}
215215

216216
export { bitShifts }

src/lib/puid.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,7 @@ export default (puidConfig: PuidConfig = {}): PuidResult => {
113113
const puidLen = round(ceil(puidEntropyBits / puidBitsPerChar))
114114
const ere = (puidBitsPerChar * puidChars.length) / (8 * Buffer.byteLength(puidChars))
115115

116-
const { error: bitsMuncherError, success: bitsMuncher } = muncher(puidLen, puidChars, selectEntropyFunction(puidConfig))
117-
if (bitsMuncherError) return { error: bitsMuncherError }
116+
const bitsMuncher = muncher(puidLen, puidChars, selectEntropyFunction(puidConfig))
118117

119118
const puid: Puid = (): string => bitsMuncher()
120119

src/types/puid.d.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ type SuccessResult<T> = {
1111
success: T
1212
}
1313

14-
type Either<T> = ErrorResult | SuccessResult<T>
15-
1614
type PuidError = {
1715
error: Error
1816
generator?: never
@@ -34,7 +32,6 @@ type PuidBuffer = ArrayBuffer
3432
type PuidBytes = Uint8Array
3533
type PuidBits = [offset: number, bits: PuidBytes]
3634
type PuidBitsMuncher = () => string
37-
type PuidBitsMuncherResult = Either<PuidBitsMuncher>
3835
type PuidBitsSlicer = (puidBits: PuidBits) => number[]
3936
type PuidEncoder = (n: number) => number
4037

0 commit comments

Comments
 (0)