deno.land / x / uuid7@v0.0.1 / mod.test.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import test from 'ava';
import uuid, { ParseError, pattern, timestamp } from './mod';
test('generates the RFC example UUID', (t) => { const time = 0x17f22e279b0; const entropy = () => new Uint8Array([0x0c, 0xc3, 0x18, 0xc4, 0xdc, 0x0c, 0x0c, 0x07, 0x39, 0x8f]);
t.is(uuid({ time, entropy }), '017f22e2-79b0-7cc3-98c4-dc0c0c07398f'); t.is(uuid({ time, entropy, dashes: false }), '017f22e279b07cc398c4dc0c0c07398f'); t.is(uuid({ time, entropy, upper: true }), '017F22E2-79B0-7CC3-98C4-DC0C0C07398F'); t.is(uuid({ time, entropy, version: 8 }), '017f22e2-79b0-8cc3-98c4-dc0c0c07398f');});
test('generates distinct UUIDs', (t) => { const a = uuid(); const b = uuid();
t.is(pattern.test(a), true); t.is(pattern.test(b), true); t.not(a, b);});
test('generates UUIDs with the current timestamp', (t) => { const [now, id] = [Date.now(), uuid()];
t.is(timestamp(id), now);});
test('parses the timestamp field', (t) => { t.is(timestamp('017f22e2-79b0-7cc3-98c4-dc0c0c07398f'), 0x17f22e279b0); t.is(timestamp('017F22E2-79B0-7CC3-98C4-DC0C0C07398F'), 0x17f22e279b0); t.is(timestamp('017f22e279b07cc398c4dc0c0c07398f'), 0x17f22e279b0); t.is(timestamp('017F22E279B07CC398C4DC0C0C07398F'), 0x17f22e279b0);
t.throws(() => timestamp('ea761762-3b05-11ed-a261-0242ac120002'), { instanceOf: ParseError, }); t.throws(() => timestamp('0cddf0a0-d1b3-4688-8a11-c845855caa95'), { instanceOf: ParseError, }); t.throws(() => timestamp('017f22e2-79b0-7'), { instanceOf: ParseError, });});
test('matches the RFC example UUID', (t) => { t.is(pattern.test('017f22e2-79b0-7cc3-98c4-dc0c0c07398f'), true); t.is(pattern.test('017F22E2-79B0-7CC3-98C4-DC0C0C07398F'), true); t.is(pattern.test('017f22e279b07cc398c4dc0c0c07398f'), true); t.is(pattern.test('017F22E279B07CC398C4DC0C0C07398F'), true);
// containing an invalid character t.is(pattern.test('017f22e2-79b0-7cc3-98c4-dc0c0c07398z'), false); t.is(pattern.test('017F22E2-79B0-7CC3-98C4-DC0C0C07398Z'), false); t.is(pattern.test('017f22e279b07cc398c4dc0c0c07398z'), false); t.is(pattern.test('017F22E279B07CC398C4DC0C0C07398Z'), false);
// too long t.is(pattern.test('017f22e2-79b0-7cc3-98c4-dc0c0c07398fa'), false); t.is(pattern.test('017F22E2-79B0-7CC3-98C4-DC0C0C07398Fa'), false); t.is(pattern.test('017f22e279b07cc398c4dc0c0c07398fa'), false); t.is(pattern.test('017F22E279B07CC398C4DC0C0C07398Fa'), false);
// too short t.is(pattern.test('017f22e2-79b0-7cc3-98c4-dc0c0c07398'), false); t.is(pattern.test('017F22E2-79B0-7CC3-98C4-DC0C0C07398'), false); t.is(pattern.test('017f22e279b07cc398c4dc0c0c07398'), false); t.is(pattern.test('017F22E279B07CC398C4DC0C0C07398'), false);});
uuid7

Version Info

Tagged at
a year ago