Skip to content

Commit 0e260bc

Browse files
committed
Improve documentation
1 parent 15599ee commit 0e260bc

File tree

1 file changed

+79
-9
lines changed

1 file changed

+79
-9
lines changed

README.md

Lines changed: 79 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,60 @@ Instead, herein is proposed ULID:
3030
- No special characters (URL safe)
3131
- Monotonic sort order (correctly detects and handles the same millisecond)
3232

33-
## JavaScript
34-
35-
### Installation
33+
## Installation
3634

3735
```
3836
npm install --save ulid
3937
```
4038

41-
### Usage
39+
## Import
40+
41+
**TypeScript, ES6+, Babel, Webpack, Rollup, etc.. environments**
42+
```javascript
43+
import { ulid } from 'ulid'
44+
45+
ulid() // 01ARZ3NDEKTSV4RRFFQ69G5FAV
46+
```
47+
48+
**CommonJS environments**
49+
```javascript
50+
const ULID = require('ulid')
51+
52+
ULID.ulid()
53+
```
54+
55+
**Browser**
56+
```html
57+
<script src="/path/to/ulid.js"></script>
58+
<script>
59+
ULID.ulid()
60+
</script>
61+
```
62+
63+
## Usage
64+
65+
To generate a ULID, simply run the function!
4266

4367
```javascript
4468
import { ulid } from 'ulid'
4569

4670
ulid() // 01ARZ3NDEKTSV4RRFFQ69G5FAV
71+
```
72+
73+
### Seed Time
4774

48-
// 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
5178
ulid(1469918176385) // 01ARYZ6S41TSV4RRFFQ69G5FAV
5279
```
5380

54-
#### Monotonic ULIDs
81+
### Monotonic ULIDs
5582

5683
To generate monotonically increasing ULIDs, create a monotonic counter.
5784

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*
86+
5887
```javascript
5988
import { monotonicFactory } from 'ulid'
6089

@@ -71,6 +100,47 @@ ulid(150000) // 000XAL6S41ACTAV9WEVGEMMVRC
71100
ulid(100000) // 000XAL6S41ACTAV9WEVGEMMVRD
72101
```
73102

103+
### Pseudo-Random Number Generators
104+
105+
`ulid` automatically detects a suitable PRNG.
106+
107+
#### Allowing the insecure `Math.random`
108+
109+
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+
const prng = detectPrng(true) // pass `true` to allow insecure
115+
const ulid = 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+
import prng from 'somewhere'
127+
128+
const ulid = 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+
import prng from 'somewhere'
138+
139+
const ulid = factory(prng)
140+
141+
ulid() // 01BXAVRG61YJ5YSBRM51702F6M
142+
```
143+
74144
## Implementations in other languages
75145

76146
From the community!
@@ -195,7 +265,7 @@ ulid() // throw new Error()!
195265

196266
#### Overflow Errors when Parsing Base32 Strings
197267

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`.
199269

200270
Any attempt to decode or encode a ULID larger than this should be rejected by all implementations, to prevent overflow bugs.
201271

0 commit comments

Comments
 (0)