Skip to content

Commit c51c263

Browse files
committed
chore: update README
1 parent cb96c6b commit c51c263

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Cuid2
2+
3+
Secure, collision-resistant ids optimized for horizontal scaling and performance. Next generation UUIDs.
4+
5+
Need unique ids in your app? Forget UUIDs and GUIDs which often collide in large apps. Use Cuid2, instead.
6+
7+
This is a port of the JavaScript library [@paralleldrive/cuid2](https://github.com/paralleldrive/cuid2), rewritten in Zig. For more detailed information about Cuid2, please refer to the [original documentation](https://github.com/paralleldrive/cuid2/blob/main/README.md).
8+
9+
## Usage
10+
11+
`cuid2-zig` requires no memory allocations and is very simple to use. Simply specify the identifier length (a `comptime` value in range `[2,32]`),
12+
initialize it and generate ids:
13+
14+
```zig
15+
const length = 24; // comptime
16+
17+
var generator = Cuid2(length).init(.{});
18+
19+
// `next` returns a fixed-size array of 24 base36 bytes
20+
const id = generator.next();
21+
```
22+
23+
You can also specify a custom random function in the `init` options:
24+
25+
````zig
26+
const std = @import("std");
27+
28+
pub const cuid2 = @import("cuid2");
29+
30+
pub fn main() !void {
31+
var generator = cuid2.Cuid2(24).init(.{ .random = customRandom });
32+
33+
std.debug.print("generated id '{s}'\n", .{generator.next()});
34+
}
35+
36+
fn customRandom() f64 {
37+
// or any other implementation, `std.rand.DefaultCsprng`, ...
38+
return std.crypto.random.float(f64);
39+
}
40+
````
41+
42+
The random function must return a `f64` value in range `[0,1)`. The default implementation uses `std.rand.DefaultPrng` with a `std.time.microTimestamp()` seed.
43+
44+
## Importing the library
45+
46+
TODO
47+
48+

src/cuid2.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ const Options = struct {
1818
};
1919

2020
/// Create a cuid2 generator.
21+
///
22+
/// Parameters:
23+
/// - `length`: a comptime value in range [2, 32] to specify the length of the generated identifiers.s
2124
pub fn Cuid2(comptime length: u8) type {
2225
if (length < MinIdLength or length > MaxIdLength) {
2326
@compileError("Invalid idLength. Cuid2 id length must be in the range [2,32].");
@@ -41,6 +44,7 @@ pub fn Cuid2(comptime length: u8) type {
4144
};
4245
}
4346

47+
/// Generates a cuid2 identifier.
4448
pub fn next(self: *Self) [length]u8 {
4549

4650
// u64 values result in max 13 base36 chars

0 commit comments

Comments
 (0)