forked from ai/nanoid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnon-secure.test.js
91 lines (76 loc) · 2.1 KB
/
non-secure.test.js
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { is, match, ok } from 'uvu/assert'
import { test } from 'uvu'
import { nanoid, customAlphabet } from '../non-secure/index.js'
import { urlAlphabet } from '../index.js'
test('nanoid / generates URL-friendly IDs', () => {
for (let i = 0; i < 10; i++) {
let id = nanoid()
is(id.length, 21)
for (let char of id) {
match(urlAlphabet, new RegExp(char, 'g'))
}
}
})
test('nanoid / changes ID length', () => {
is(nanoid(10).length, 10)
})
test('nanoid / accepts string', () => {
is(nanoid('10').length, 10)
})
test('nanoid / has no collisions', () => {
let used = {}
for (let i = 0; i < 100 * 1000; i++) {
let id = nanoid()
is(used[id], undefined)
used[id] = true
}
})
test('nanoid / has flat distribution', () => {
let COUNT = 100 * 1000
let LENGTH = nanoid().length
let chars = {}
for (let i = 0; i < COUNT; i++) {
let id = nanoid()
for (let char of id) {
if (!chars[char]) chars[char] = 0
chars[char] += 1
}
}
is(Object.keys(chars).length, urlAlphabet.length)
let max = 0
let min = Number.MAX_SAFE_INTEGER
for (let k in chars) {
let distribution = (chars[k] * urlAlphabet.length) / (COUNT * LENGTH)
if (distribution > max) max = distribution
if (distribution < min) min = distribution
}
ok(max - min <= 0.05)
})
test('customAlphabet / has options', () => {
let nanoidA = customAlphabet('a', 5)
is(nanoidA(), 'aaaaa')
})
test('customAlphabet / has flat distribution', () => {
let COUNT = 100 * 1000
let LENGTH = 5
let ALPHABET = 'abcdefghijklmnopqrstuvwxyz'
let nanoid2 = customAlphabet(ALPHABET, LENGTH)
let chars = {}
for (let i = 0; i < COUNT; i++) {
let id = nanoid2()
for (let char of id) {
if (!chars[char]) chars[char] = 0
chars[char] += 1
}
}
is(Object.keys(chars).length, ALPHABET.length)
let max = 0
let min = Number.MAX_SAFE_INTEGER
for (let k in chars) {
let distribution = (chars[k] * ALPHABET.length) / (COUNT * LENGTH)
if (distribution > max) max = distribution
if (distribution < min) min = distribution
}
ok(max - min <= 0.05)
})
test.run()