mirror of
https://github.com/Yu-Vitaqua-fer-Chronos/NULID.git
synced 2024-11-21 22:12:46 +00:00
Version bump, add version in docs
This commit is contained in:
parent
0007fd249e
commit
0976774b98
|
@ -1,6 +1,8 @@
|
|||
import src/nulid/private/constants
|
||||
|
||||
# Package
|
||||
|
||||
version = "0.2.3"
|
||||
version = NulidVersion
|
||||
author = "Yu Vitaqua fer Chronos"
|
||||
description = "An implementation of ULID!"
|
||||
license = "CC0"
|
||||
|
|
|
@ -8,11 +8,16 @@ import pkg/[
|
|||
nint128
|
||||
]
|
||||
|
||||
import ./nulid/private/[
|
||||
dochelpers,
|
||||
constants
|
||||
]
|
||||
|
||||
import ./nulid/private/stew/endians2
|
||||
|
||||
const insecureRandom = defined(nulidInsecureRandom) or defined(js) # No sysrand on the JS backend
|
||||
fmtCmnt "NULID Version: {NulidVersion}"
|
||||
|
||||
when insecureRandom:
|
||||
when InsecureRandom:
|
||||
import std/random
|
||||
|
||||
else:
|
||||
|
@ -23,8 +28,8 @@ const HighUint80 = u128("1208925819614629174706176")
|
|||
type
|
||||
NULID* = object
|
||||
## An object representing a ULID.
|
||||
timestamp*: int64 = 0
|
||||
randomness*: UInt128 = 0.u128
|
||||
timestamp*: int64
|
||||
randomness*: UInt128
|
||||
|
||||
NULIDGenerator* = ref object
|
||||
## A NULID generator object, contains details needed to follow the spec.
|
||||
|
@ -33,14 +38,14 @@ type
|
|||
lastTime: int64 # Timestamp of last ULID
|
||||
random: UInt128 # A random number
|
||||
|
||||
when insecureRandom:
|
||||
when InsecureRandom:
|
||||
rand: Rand # Random generator when using insecure random
|
||||
|
||||
proc initNulidGenerator*(): NULIDGenerator =
|
||||
## Initialises a `NULIDGenerator` for use.
|
||||
result = NULIDGenerator(lastTime: 0, random: 0.u128)
|
||||
|
||||
when insecureRandom:
|
||||
when InsecureRandom:
|
||||
result.rand = initRand()
|
||||
|
||||
# Discouraged to use it but it's fine for single-threaded apps really
|
||||
|
@ -56,7 +61,7 @@ func toArray[T](oa: openArray[T], size: static Slice[int]): array[size.len, T] =
|
|||
proc randomBits(n: NULIDGenerator): UInt128 =
|
||||
var arr: array[16, byte]
|
||||
|
||||
when insecureRandom:
|
||||
when InsecureRandom:
|
||||
var rnd: array[10, byte]
|
||||
|
||||
rnd[0..7] = cast[array[8, byte]](n.rand.next())
|
||||
|
|
4
src/nulid/private/constants.nim
Normal file
4
src/nulid/private/constants.nim
Normal file
|
@ -0,0 +1,4 @@
|
|||
# No sysrand on the JS backend nor VM
|
||||
const InsecureRandom* = defined(nulidInsecureRandom) or defined(js) or defined(nimvm)
|
||||
|
||||
const NulidVersion* = "0.2.4"
|
8
src/nulid/private/dochelpers.nim
Normal file
8
src/nulid/private/dochelpers.nim
Normal file
|
@ -0,0 +1,8 @@
|
|||
import std/[
|
||||
strformat,
|
||||
macros
|
||||
]
|
||||
|
||||
macro ct(s: static string) = newCommentStmtNode(s)
|
||||
|
||||
template fmtCmnt*(s: static string): untyped = ct(&s)
|
|
@ -11,11 +11,9 @@ import pkg/nint128
|
|||
|
||||
import nulid
|
||||
|
||||
let gen = initNulidGenerator()
|
||||
|
||||
test "NULID Generation":
|
||||
for _ in 0..5:
|
||||
let nulid = gen.nulidSync()
|
||||
let nulid = nulidSync()
|
||||
echo nulid
|
||||
|
||||
test "NULID Parsing":
|
||||
|
|
Loading…
Reference in a new issue