Skip to content

Commit 5f4cdb8

Browse files
committed
bit shifts comment/example
1 parent d7e8375 commit 5f4cdb8

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# puid-js
22

3-
Simple, flexible and efficient generation of probably unique identifiers (`puid`, aka random strings) of intuitively specified entropy using pre-defined or custom characters.
3+
Simple, flexible and efficient generation of probably unique identifiers (`puid`, aka random strings) of intuitively specified entropy using pre-defined or custom characters (including Unicode).
44

55
```js
66
const { Chars, puid } = require('puid-js')

src/lib/bits.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ type AcceptValue = readonly [accept: boolean, shift: number]
3434
type BitShift = readonly [number, number]
3535
type BitShifts = readonly BitShift[]
3636

37+
// Create array of minimum bits required to determine if a value is less than nChars
38+
// Array elements are of the form [n, bits]: For values less than n, bits bits are required
39+
//
40+
// As example, the bits shifts array for the 36 AlphaNumLower characters is:
41+
// [36, 6], [39, 5], [47, 3], [63, 2]
42+
//
43+
// Each value slice uses 6 bits of entropy. In bits, 36 is 100100.
44+
// Now suppose we slice the value 50. In bits, 50 is 110010.
45+
//
46+
// Only two bits are necessary to determine 100100 < 110010
47+
//
3748
const bitShifts = (chars: string): BitShifts => {
3849
const nBitsPerChar = bitsPerChar(chars)
3950
const baseBitShift: BitShift = [chars.length, ceil(nBitsPerChar)]

src/lib/puid.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,12 @@ test('dîngøsky chars (count power of 2 with carry)', (t) => {
169169

170170
test('Safe32 (count non-power of 2 with carry)', (t) => {
171171
// D 2 E 3 E 9 D A 1 9 0 3 B 7 3 C
172-
/// 1101 0010 1110 0011 1110 1001 1101 1010 0001 1001 0000 0011 1011 0111 0011 1100
172+
// 1101 0010 1110 0011 1110 1001 1101 1010 0001 1001 0000 0011 1011 0111 0011 1100
173173
//
174174
// 11010 01011 10001 11110 10011 10110 10000 11001 00000 01110 11011 10011 1100
175175
// |---| |---| |---| |---| |---| |---| |---| |---| |---| |---| |---| |---|
176-
// 26 11 17 30 19 22 16 25 0 14 27 19
177-
// M h r R B G q L 2 n N B
176+
// 26 11 17 30 19 22 16 25 0 14 27 19
177+
// M h r R B G q L 2 n N B
178178
//
179179
const safe32Bytes = fixedBytes([0xd2, 0xe3, 0xe9, 0xda, 0x19, 0x03, 0xb7, 0x3c])
180180
const { generator: safe32Id } = puid({ bits: 20, chars: Chars.Safe32, entropyBytes: safe32Bytes })
@@ -193,7 +193,7 @@ test('puid safe32 entropyValues', (t) => {
193193
t.is(valuesId(), '2nNB')
194194
})
195195

196-
test('AlphaLower chars (26 chars, 5 bits', (t) => {
196+
test('AlphaLower chars (26 chars, 5+ bits', (t) => {
197197
// shifts: [ [ 26, 5 ], [ 31, 3 ] ]
198198
//
199199
// 5 3 c 8 8 d e 6 3 e 2 6 a 0
@@ -237,22 +237,22 @@ test('Base32 chars (32 chars, 5 bits', (t) => {
237237
})
238238

239239
test('Base32Hex chars (32 chars, 5 bits)', (t) => {
240-
const base32HexBytes = fixedBytes([0xd2, 0xe3, 0xe9, 0xda, 0x19, 0x12, 0xce])
240+
const base32HexBytes = fixedBytes([0xd2, 0xe3, 0xe9, 0xda, 0x19, 0x12, 0xce, 0x28])
241241
const { generator: base32HexId } = puid({ bits: 30, chars: Chars.Base32Hex, entropyBytes: base32HexBytes })
242242
t.is(base32HexId(), 'qbhujm')
243-
t.is(base32HexId(), 'gp2b71')
243+
t.is(base32HexId(), 'gp2b72')
244244
})
245245

246246
test('Base32HexUpper chars (32 chars, 5 bits)', (t) => {
247-
const base32HexUpperBytes = fixedBytes([0xd2, 0xe3, 0xe9, 0xda, 0x19, 0x12, 0xce])
247+
const base32HexUpperBytes = fixedBytes([0xd2, 0xe3, 0xe9, 0xda, 0x19, 0x12, 0xce, 0x28])
248248
const { generator: base32HexUpperId } = puid({
249249
bits: 20,
250250
chars: Chars.Base32HexUpper,
251251
entropyBytes: base32HexUpperBytes
252252
})
253253
t.is(base32HexUpperId(), 'QBHU')
254254
t.is(base32HexUpperId(), 'JMGP')
255-
t.is(base32HexUpperId(), '2B71')
255+
t.is(base32HexUpperId(), '2B72')
256256
})
257257

258258
test('puid from Chars.AlphaUpper and Chars.SafeAscii', (t) => {

0 commit comments

Comments
 (0)