Skip to content

Commit c87c456

Browse files
Rework readme
1 parent b2acf0b commit c87c456

File tree

1 file changed

+56
-113
lines changed

1 file changed

+56
-113
lines changed

README.md

Lines changed: 56 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,25 @@
77
<br>
88
</h1>
99

10+
# Universally Unique Lexicographically Sortable Identifier
11+
1012
[![Tests](https://github.com/ulid/javascript/actions/workflows/test.yml/badge.svg)](https://github.com/ulid/javascript/actions/workflows/test.yml)
1113
[![codecov](https://codecov.io/gh/ulid/javascript/branch/master/graph/badge.svg)](https://codecov.io/gh/ulid/javascript)
12-
[![npm](https://img.shields.io/npm/dm/ulid.svg)](https://www.npmjs.com/package/ulid)
14+
[![npm](https://img.shields.io/npm/dm/ulid.svg)](https://www.npmjs.com/package/ulid) [![npm](https://img.shields.io/npm/dy/ulid)](https://www.npmjs.com/package/ulid)
1315

14-
# Universally Unique Lexicographically Sortable Identifier
16+
ULIDs are unique, sortable identifiers that work much in the same way as UUIDs, though with some improvements:
17+
18+
* Lexicographically sortable
19+
* Canonically encoded as a 26 character string, as opposed to the 36 character UUID
20+
* Uses Crockford's base32 for better efficiency and readability (5 bits per character)
21+
* Monotonic sort order (correctly detects and handles the same millisecond)
22+
23+
ULIDs also provide:
24+
25+
* 128-bit compatibility with UUID
26+
* 1.21e+24 unique IDs per millisecond
27+
* Case insensitivity
28+
* No special characters (URL safe)
1529

1630
UUID can be suboptimal for many uses-cases because:
1731

@@ -20,164 +34,93 @@ UUID can be suboptimal for many uses-cases because:
2034
- UUID v3/v5 requires a unique seed and produces randomly distributed IDs, which can cause fragmentation in many data structures
2135
- UUID v4 provides no other information than randomness which can cause fragmentation in many data structures
2236

23-
Instead, herein is proposed ULID:
24-
25-
- 128-bit compatibility with UUID
26-
- 1.21e+24 unique ULIDs per millisecond
27-
- Lexicographically sortable!
28-
- Canonically encoded as a 26 character string, as opposed to the 36 character UUID
29-
- Uses Crockford's base32 for better efficiency and readability (5 bits per character)
30-
- Case insensitive
31-
- No special characters (URL safe)
32-
- Monotonic sort order (correctly detects and handles the same millisecond)
33-
34-
## Install with a script tag
35-
36-
```html
37-
<script src="https://unpkg.com/ulid@{{VERSION_NUMBER}}/dist/index.umd.js"></script>
38-
<script>
39-
ULID.ulid()
40-
</script>
41-
```
37+
## Installation
4238

43-
## Install with NPM
39+
Install using NPM:
4440

45-
```
46-
npm install --save ulid
41+
```shell
42+
npm install ulid --save
4743
```
4844

49-
### Import
45+
### Compatibility
5046

51-
**TypeScript, ES6+, Babel, Webpack, Rollup, etc.. environments**
52-
```javascript
53-
import { ulid } from 'ulid'
47+
ULID supports the following environments:
5448

55-
ulid() // 01ARZ3NDEKTSV4RRFFQ69G5FAV
56-
```
49+
| Version | NodeJS | Browsers | React-Native | Web Workers | Edge Functions |
50+
|-----------|-----------|---------------|---------------|---------------|-------------------|
51+
| v3 | v18+ | Yes | Yes | Yes | ? |
52+
| v2 | v16+ | Yes | No | No | No |
5753

58-
**CommonJS environments**
59-
```javascript
60-
const ULID = require('ulid')
61-
62-
ULID.ulid()
63-
```
64-
65-
**AMD (RequireJS) environments**
66-
```javascript
67-
define(['ULID'] , function (ULID) {
68-
ULID.ulid()
69-
});
70-
```
54+
Additionally, both ESM and CommonJS entrypoints are provided.
7155

7256
## Usage
7357

74-
To generate a ULID, simply run the function!
58+
To quickly generate a ULID, you can simply import the `ulid` function:
7559

76-
```javascript
77-
import { ulid } from 'ulid'
60+
```typescript
61+
import { ulid } from "ulid";
7862

79-
ulid() // 01ARZ3NDEKTSV4RRFFQ69G5FAV
63+
ulid(); // 01ARZ3NDEKTSV4RRFFQ69G5FAV
8064
```
8165

8266
### Seed Time
8367

8468
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.
8569

86-
```javascript
70+
```typescript
8771
ulid(1469918176385) // 01ARYZ6S41TSV4RRFFQ69G5FAV
8872
```
8973

9074
### Monotonic ULIDs
9175

92-
To generate monotonically increasing ULIDs, create a monotonic counter.
76+
To generate monotonically increasing ULIDs, create a monotonic counter with `monotonicFactory`.
9377

94-
*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*
78+
> 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
9579
96-
```javascript
97-
import { monotonicFactory } from 'ulid'
80+
```typescript
81+
import { monotonicFactory } from "ulid";
9882

99-
const ulid = monotonicFactory()
83+
const ulid = monotonicFactory();
10084

10185
// Strict ordering for the same timestamp, by incrementing the least-significant random bit by 1
102-
ulid(150000) // 000XAL6S41ACTAV9WEVGEMMVR8
103-
ulid(150000) // 000XAL6S41ACTAV9WEVGEMMVR9
104-
ulid(150000) // 000XAL6S41ACTAV9WEVGEMMVRA
105-
ulid(150000) // 000XAL6S41ACTAV9WEVGEMMVRB
106-
ulid(150000) // 000XAL6S41ACTAV9WEVGEMMVRC
86+
ulid(150000); // 000XAL6S41ACTAV9WEVGEMMVR8
87+
ulid(150000); // 000XAL6S41ACTAV9WEVGEMMVR9
88+
ulid(150000); // 000XAL6S41ACTAV9WEVGEMMVRA
89+
ulid(150000); // 000XAL6S41ACTAV9WEVGEMMVRB
90+
ulid(150000); // 000XAL6S41ACTAV9WEVGEMMVRC
10791

10892
// Even if a lower timestamp is passed (or generated), it will preserve sort order
109-
ulid(100000) // 000XAL6S41ACTAV9WEVGEMMVRD
93+
ulid(100000); // 000XAL6S41ACTAV9WEVGEMMVRD
11094
```
11195

11296
### Pseudo-Random Number Generators
11397

114-
`ulid` automatically detects a suitable (cryptographically-secure) PRNG. In the browser it will use `crypto.getRandomValues` and on node it will use `crypto.randomBytes`.
115-
116-
#### Allowing the insecure `Math.random`
117-
118-
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`.
98+
`ulid` automatically detects a suitable (cryptographically-secure) PRNG. In the browser it will use `crypto.getRandomValues` and on NodeJS it will use `crypto.randomBytes`.
11999

120-
```javascript
121-
import { factory, detectPrng } from 'ulid'
100+
#### Using `Math.random` (insecure)
122101

123-
const prng = detectPrng(true) // pass `true` to allow insecure
124-
const ulid = factory(prng)
102+
By default, `ulid` will not use `Math.random` to generate random values. You can bypass this limitation by overriding the PRNG:
125103

126-
ulid() // 01BXAVRG61YJ5YSBRM51702F6M
127-
```
128-
129-
#### Use your own PRNG
130-
131-
To use your own pseudo-random number generator, import the factory, and pass it your generator function.
132-
133-
```javascript
134-
import { factory } from 'ulid'
135-
import prng from 'somewhere'
136-
137-
const ulid = factory(prng)
138-
139-
ulid() // 01BXAVRG61YJ5YSBRM51702F6M
140-
```
141-
142-
You can also pass in a `prng` to the `monotonicFactory` function.
143-
144-
```javascript
145-
import { monotonicFactory } from 'ulid'
146-
import prng from 'somewhere'
104+
```typescript
105+
const ulid = monotonicFactory(() => Math.random());
147106

148-
const ulid = monotonicFactory(prng)
149-
150-
ulid() // 01BXAVRG61YJ5YSBRM51702F6M
107+
ulid(); // 01BXAVRG61YJ5YSBRM51702F6M
151108
```
152109

153-
## Implementations in other languages
110+
### Tests
154111

155-
Refer to [ulid/spec](https://github.com/ulid/spec)
112+
Install dependencies using `npm install` first, and then simply run `npm test` to run the test suite.
156113

157114
## Specification
158115

159-
Refer to [ulid/spec](https://github.com/ulid/spec)
160-
161-
## Test Suite
162-
163-
```
164-
npm test
165-
```
116+
You can find the full specification, as well as information regarding implementations in other languages, over at [ulid/spec](https://github.com/ulid/spec).
166117

167118
## Performance
168119

169-
```
170-
npm run perf
171-
```
120+
You can test `ulid`'s performance by running `npm run bench`:
172121

173122
```
174-
ulid
175-
336,331,131 op/s » encodeTime
176-
102,041,736 op/s » encodeRandom
177-
17,408 op/s » generate
178-
179-
180-
Suites: 1
181-
Benches: 3
182-
Elapsed: 7,285.75 ms
123+
Simple ulid x 56,782 ops/sec ±2.50% (86 runs sampled)
124+
ulid with timestamp x 58,574 ops/sec ±1.80% (87 runs sampled)
125+
Done!
183126
```

0 commit comments

Comments
 (0)