M nanoid.janet => nanoid.janet +13 -15
@@ 7,18 7,16 @@
(default size 21)
(default alphabet DEFAULT_ALPHABET)
- (def mask (find |(>= $ (dec (length alphabet))) masks))
- (def step (->> alphabet
- (length)
- (/ (* 1.6 mask size))
- (math/ceil)))
- (def result (array/new size))
- (while (> size (length result))
- (loop [byte :in (os/cryptorand step)
- :when (> size (length result))]
- (let [mask-byte (band mask byte)
- char (get alphabet mask-byte)]
- (if char
- (array/push result char))
- )))
- (string/from-bytes ;result))
+ (let [mask (find |(>= $ (dec (length alphabet))) masks)
+ step (->> alphabet
+ (length)
+ (/ (* 1.6 mask size))
+ (math/ceil))
+ result (array/new size)]
+ (while (> size (length result))
+ (loop [byte :in (os/cryptorand step)
+ :when (> size (length result))]
+ (let [mask-byte (band mask byte)
+ char (get alphabet mask-byte)]
+ (if char (array/push result char)))))
+ (string/from-bytes ;result)))
M test/nanoid.janet => test/nanoid.janet +2 -1
@@ 9,9 9,10 @@
(assert
(= 10 (length (n/gen :size 10)))
"generates id of length 10"))
+(pp (n/gen))
(let [results (table)]
- (loop [x :range [0 1000]]
+ (loop [x :range [0 10_000]]
(let [id (n/gen)]
(assert (nil? (results id))
(string/format "%q not unique" id))