Skip to content
Snippets Groups Projects
Commit ec009a78 authored by cms's avatar cms
Browse files

ulid can take a :time and a :random parameter, optionally

time allows you to fix the time. random allows you to supply a random
function. Primarily useful for testing.
parent 1952a4ca
Branches
Tags
1 merge request!4Broader tests
......@@ -18,6 +18,11 @@ one public function, ulid. It returns 3 values
- a bytes vector of the binary form
- the binary struct
`ulid` has two optional arguments
`:time` - specify your own timestamp, milliseconds since epoch
`:random` - specify your own RNG , e.g. `(lambda (x) (random x))`
```
CL-USER> (cms-ulid:ulid)
"01GHGA3GJ65CHWRVQTC8C7AD4P"
......
......@@ -39,19 +39,25 @@
(serapeum:batches bits 5)))))
(let ((last (make-ulid :time 0 :random 0)))
(defun ulid ()
(defun ulid (&key time (random #'crypto:strong-random))
"ulid makes a new ulid
returned as a 3 values tuple
- string
- bytes sequence
- the binary struct"
(let ((now (unix-ts-ms))
- the binary struct
args:
:time - specify a timestamp to use
:random - a random function that takes an int n and returns int > 0 and < n
(defaults to #'crypto:strong-random"
(let ((now (or time (unix-ts-ms)))
(u last))
(if (equal now (slot-value last 'time))
(incf (slot-value u 'random))
(progn
(setf u (make-ulid :time now :random (crypto:strong-random +80-bit-maxint+)))
(format t "~a~%" random)
(setf u (make-ulid :time now :random (funcall random +80-bit-maxint+)))
(setf last u)))
(let ((bytes (ulid->bytes-seq u)))
(values (bytes->base32 bytes) bytes u)))))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment