You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
//You can also input a seed time which will consistently
49
-
// give you the same string for the time component. This is
50
-
// useful for migrating to ulid.
75
+
You can also input a seed time which will consistently give you the same string for the time component. This is useful for migrating to ulid.
76
+
77
+
```javascript
51
78
ulid(1469918176385) // 01ARYZ6S41TSV4RRFFQ69G5FAV
52
79
```
53
80
54
-
####Monotonic ULIDs
81
+
### Monotonic ULIDs
55
82
56
83
To generate monotonically increasing ULIDs, create a monotonic counter.
57
84
85
+
*Note that the same seed time is being passed in for this example to demonstrate its behaviour when generating multiple ULIDs within the same millisecond*
By default, `ulid` will not use `Math.random`, because that is insecure. To allow the use of `Math.random`, you'll have to use `factory` and `detectPrng`.
110
+
111
+
```javascript
112
+
import { factory, detectPrng } from'ulid'
113
+
114
+
constprng=detectPrng(true) // pass `true` to allow insecure
115
+
constulid=factory(prng)
116
+
117
+
ulid() // 01BXAVRG61YJ5YSBRM51702F6M
118
+
```
119
+
120
+
#### Use your own PRNG
121
+
122
+
To use your own pseudo-random number generator, import the factory, and pass it your generator function.
123
+
124
+
```javascript
125
+
import { factory } from'ulid'
126
+
importprngfrom'somewhere'
127
+
128
+
constulid=factory(prng)
129
+
130
+
ulid() // 01BXAVRG61YJ5YSBRM51702F6M
131
+
```
132
+
133
+
You can also pass in a `prng` to the `monotonicFactory` function.
134
+
135
+
```javascript
136
+
import { monotonicFactory } from'ulid'
137
+
importprngfrom'somewhere'
138
+
139
+
constulid=factory(prng)
140
+
141
+
ulid() // 01BXAVRG61YJ5YSBRM51702F6M
142
+
```
143
+
74
144
## Implementations in other languages
75
145
76
146
From the community!
@@ -195,7 +265,7 @@ ulid() // throw new Error()!
195
265
196
266
#### Overflow Errors when Parsing Base32 Strings
197
267
198
-
Technically, a 26 character Base32 encoded string can contain 130 bits of information, whereas a ULID must only contain 128 bits. Therefore, the largest valid ULID encoded in Base32 is `7ZZZZZZZZZZZZZZZZZZZZZZZZZ`, which corresponds to an epoch time of `281474976710655`.
268
+
Technically, a 26 character Base32 encoded string can contain 130 bits of information, whereas a ULID must only contain 128 bits. Therefore, the largest valid ULID encoded in Base32 is `7ZZZZZZZZZZZZZZZZZZZZZZZZZ`, which corresponds to an epoch time of `281474976710655` or `2 ^ 48 - 1`.
199
269
200
270
Any attempt to decode or encode a ULID larger than this should be rejected by all implementations, to prevent overflow bugs.
0 commit comments