@@ -3,65 +3,84 @@ import { group } from "@thi.ng/testament";
3
3
import * as assert from "assert" ;
4
4
import { defKSUID32 , defKSUID64 , defULID , IKSUID } from "../src/index.js" ;
5
5
6
- const check = (
7
- id : IKSUID ,
8
- eps : number ,
9
- buf : Uint8Array ,
10
- epochBuf : Uint8Array
11
- ) => {
6
+ const check = ( {
7
+ idgen,
8
+ eps,
9
+ epoch,
10
+ epochId,
11
+ id1,
12
+ id2,
13
+ } : {
14
+ idgen : IKSUID ;
15
+ eps : number ;
16
+ epoch : number ;
17
+ epochId : string ;
18
+ id1 : Uint8Array ;
19
+ id2 : Uint8Array ;
20
+ } ) => {
12
21
const t = Date . now ( ) ;
13
- const a = id . timeOnly ( t ) ;
14
- assert . strictEqual ( a . length , id . encodedSize ) ;
15
- let res = id . parse ( a ) ;
22
+ let a = idgen . timeOnly ( t ) ;
23
+ assert . strictEqual ( a . length , idgen . encodedSize ) ;
24
+ let res = idgen . parse ( a ) ;
16
25
assert . ok ( Math . abs ( res . epoch - t ) < eps ) ;
17
- assert . deepStrictEqual ( res . id , new Uint8Array ( id . size - id . epochSize ) ) ;
18
- const b = id . nextBinary ( ) ;
19
- assert . deepStrictEqual ( b . slice ( id . epochSize ) , buf ) ;
20
- res = id . parse ( id . format ( b ) ) ;
26
+ assert . deepStrictEqual (
27
+ res . id ,
28
+ new Uint8Array ( idgen . size - idgen . epochSize )
29
+ ) ;
30
+ const b = idgen . nextBinary ( ) ;
31
+ assert . deepStrictEqual ( b . slice ( idgen . epochSize ) , id1 ) ;
32
+ res = idgen . parse ( idgen . format ( b ) ) ;
21
33
assert . ok ( Math . abs ( res . epoch - t ) < eps ) ;
22
- assert . deepStrictEqual ( res . id , buf ) ;
23
- assert . deepStrictEqual ( id . fromEpochBinary ( 1673827200000 ) , epochBuf ) ;
34
+ assert . deepStrictEqual ( res . id , id1 ) ;
35
+
36
+ a = idgen . fromEpoch ( epoch ) ;
37
+ assert . strictEqual ( a , epochId ) ;
38
+ res = idgen . parse ( a ) ;
39
+ assert . ok ( Math . abs ( res . epoch - epoch ) < 1000 ) ;
40
+ assert . deepStrictEqual ( res . id , id2 ) ;
24
41
} ;
25
42
26
43
group ( "ksuid" , {
27
44
ksuid32 : ( ) => {
28
- check (
29
- defKSUID32 ( { rnd : new XsAdd ( 0xdecafbad ) } ) ,
30
- 1000 * 2 ,
31
- new Uint8Array ( [
45
+ check ( {
46
+ idgen : defKSUID32 ( { rnd : new XsAdd ( 0xdecafbad ) } ) ,
47
+ eps : 1000 * 2 ,
48
+ epoch : 1673827200987 ,
49
+ epochId : "0cvXkpEgU5CRFeBfpf2KrwummtA" ,
50
+ id1 : new Uint8Array ( [
32
51
170 , 213 , 122 , 63 , 189 , 122 , 161 , 143 , 91 , 187 , 80 , 231 , 61 , 17 ,
33
52
112 , 238 ,
34
53
] ) ,
35
- new Uint8Array ( [
36
- 4 , 102 , 131 , 128 , 226 , 90 , 28 , 179 , 222 , 71 , 112 , 20 , 59 , 2 , 22 ,
37
- 112 , 98 , 25 , 104 , 28 ,
38
- ] )
39
- ) ;
54
+ id2 : new Uint8Array ( [
55
+ 226 , 90 , 28 , 179 , 222 , 71 , 112 , 20 , 59 , 2 , 22 , 112 , 98 , 25 , 104 ,
56
+ 28 ,
57
+ ] ) ,
58
+ } ) ;
40
59
} ,
41
60
42
61
ksuid64 : ( ) => {
43
- check (
44
- defKSUID64 ( { rnd : new XsAdd ( 0xdecafbad ) } ) ,
45
- 1 * 2 ,
46
- new Uint8Array ( [
62
+ check ( {
63
+ idgen : defKSUID64 ( { rnd : new XsAdd ( 0xdecafbad ) } ) ,
64
+ eps : 1 * 2 ,
65
+ epoch : 1673827200987 ,
66
+ epochId : "000029vWC12Ap6k6ZH00XfKuZGp" ,
67
+ id1 : new Uint8Array ( [
47
68
189 , 122 , 161 , 143 , 91 , 187 , 80 , 231 , 61 , 17 , 112 , 238 ,
48
69
] ) ,
49
- new Uint8Array ( [
50
- 0 , 0 , 0 , 17 , 48 , 113 , 172 , 0 , 59 , 2 , 22 , 112 , 98 , 25 , 104 , 28 ,
51
- 170 , 213 , 122 , 63 ,
52
- ] )
53
- ) ;
70
+ id2 : new Uint8Array ( [
71
+ 59 , 2 , 22 , 112 , 98 , 25 , 104 , 28 , 170 , 213 , 122 , 63 ,
72
+ ] ) ,
73
+ } ) ;
54
74
} ,
55
75
56
76
ulid : ( ) => {
57
- check (
58
- defULID ( { rnd : new XsAdd ( 0xdecafbad ) } ) ,
59
- 1 * 2 ,
60
- new Uint8Array ( [ 161 , 143 , 91 , 187 , 80 , 231 , 61 , 17 , 112 , 238 ] ) ,
61
- new Uint8Array ( [
62
- 1 , 133 , 183 , 224 , 44 , 0 , 98 , 25 , 104 , 28 , 170 , 213 , 122 , 63 ,
63
- 189 , 122 ,
64
- ] )
65
- ) ;
77
+ check ( {
78
+ idgen : defULID ( { rnd : new XsAdd ( 0xdecafbad ) } ) ,
79
+ eps : 1 * 2 ,
80
+ epoch : 1673827200987 ,
81
+ epochId : "01GPVY0BYVC8CPG75ATNX3ZFBT" ,
82
+ id1 : new Uint8Array ( [ 161 , 143 , 91 , 187 , 80 , 231 , 61 , 17 , 112 , 238 ] ) ,
83
+ id2 : new Uint8Array ( [ 98 , 25 , 104 , 28 , 170 , 213 , 122 , 63 , 189 , 122 ] ) ,
84
+ } ) ;
66
85
} ,
67
86
} ) ;
0 commit comments