-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcuid2.javascript.txt
42 lines (32 loc) · 2.02 KB
/
cuid2.javascript.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
┏━━━━━━━━━━━┓
┃ CUID2 ┃
┗━━━━━━━━━━━┛
ALTERNATIVES ==> #See UUID
VERSION ==> #2.2.2 (2024-10-03)
FORMAT ==> #~124 bits
#24 chars, base36
#SHA3 hash, base36 (first 23 chars) of:
# - time: 1ms, Epoch
# - random: 20 bits, 50% collision after 1.5e3 items
# - serial: max 9e15
# - initial value can be random
# - def uses 29 bits of randomness (50% collision after 2.6e4 items)
# - fingerprint: 1152 bits, computed once per process
# - default uses only 20 bits of randomness + STR_ARR keys of global OBJ
#Prepended with 1 random base26 char
createId()->STR
init([OPTS])->FUNC()->STR #
OPTS.length #UINT (def: 24)
OPTS.fingerprint #STR, 32 chars, base36
createFingerprint([FOPTS])->STR #Default value of OPTS.fingerprint
#SHA3 of randomness
#Randomness: 4 random base36 chars
#SHA3 is 512 bits, then truncated
FOPTS.globalObj #Def: global, window or empty OBJ
#Its keys are added to randomness
OPTS.counter #FUNC()->UINT incrementing and returning UINT
createCounter(UINT)->FUNC #Default value of OPTS.counter, with UINT being random from 0 to 5e8
[F]OPTS.random #FUNC()->0~1 (def: Math.random)
isCuid(STR[, TEST_OPTS])->BOOL #
TEST_OPTS.minLength #NUM (def: 2)
TEST_OPTS.maxLength #NUM (def: 32)