-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadme.test.ts
More file actions
66 lines (60 loc) · 2.26 KB
/
readme.test.ts
File metadata and controls
66 lines (60 loc) · 2.26 KB
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
import { expect } from "bun:test";
import { muddle } from "."
/**
* `muddle` requires you to provide a `getRandomValues` function
* which fills the given `Uint8Array` with random bytes.
*
* You may want to use `Crypto.getRandomValues`:
* https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
*
* For the sake of deterministic tests, here we use a silly not-
* actually-random implementation.
*/
function getRandomValues(a: Uint8Array): void {
a.set(new TextEncoder().encode(
'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'
).slice(0, a.length), 0)
}
/**
* Muddling accepts a string and produces a string resembling
* the original but with random character substitutions.
*
* For example, if the input is a URI, the output will also be
* a URI.
*/
expect(muddle('https://hackage.haskell.org/package/containers',
{ getRandomValues }))
.toBe("yhkxf://gdeldfc.wdehagl.bmg/tbfmcgd/dglxdmxcnk")
/**
* Basic Latin letters are only replaced with letters, and
* numbers 0-9 are only replaced with numbers. All other
* characters are left unchanged. These rules preserves the
* essential characteristics of many simple string formats.
*/
expect(muddle('{"userId": 452, "name": "Chris"}',
{ getRandomValues }))
.toBe('{"yhaxFc": 525, "nbge": "Dehkg"}')
/**
* By default, letter case is preserved. In the previous example,
* the input is all lower case, so the output is as well. This
* behavior can be changed via the `letterCase` option.
*/
expect(muddle('https://hackage.haskell.org/package/containers',
{ getRandomValues, letterCase: 'insensitive' }))
.toBe("YHKxF://gdELdFc.wDEHagL.BMg/tBFMcgD/DGLxDMxcNK")
/**
* By default, characters in the hexadecimal range (A to F) will
* not be converted to characters outside this range. This means
* that hex strings remain hex strings, so for example if the
* input is a UUID, then the output will also be a UUID.
*/
expect(muddle('c97db9ed-354e-4fb0-8b16-29ea7a824bd1',
{ getRandomValues }))
.toBe("e14fb2de-579c-0da1-4c55-62bb1c429dc5")
/**
* The hexadecimal letter preservation behavior can be disabled
* via the `hexLetters` option.
*/
expect(muddle('c97db9ed-354e-4fb0-8b16-29ea7a824bd1',
{ getRandomValues, hexLetters: 'insensitive' }))
.toBe("y14xf2bi-579g-0he1-4g55-62tf1m429hg5")