-
Notifications
You must be signed in to change notification settings - Fork 0
/
submit.js
97 lines (75 loc) · 2.54 KB
/
submit.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
92
93
94
95
96
97
const form = document.getElementById('form');
form.onsubmit = onSubmit
function onSubmit(event) {
event.preventDefault();
const params = {};
for (const [key, value] of new FormData(event.target).entries()) {
params[key] = value;
}
document.getElementById('result').textContent = result(params);
}
function result(params) {
const {
length,
delimeter,
useDelimeter
} = params;
if (parseInt(useDelimeter)) {
return insertDelimeter(randomString(length, validCharCodes(params)), delimeter);
}
return randomString(length, validCharCodes(params));
}
function randomString(stringLength, validCharCodes) {
let randomCharCodes = new Uint8Array(0);
while (randomCharCodes.length < stringLength) {
const values = window.crypto.getRandomValues(new Uint8Array(stringLength));
randomCharCodes = new Uint8Array([...randomCharCodes, ...values.filter(int => validCharCodes.includes(int))]);
}
return String.fromCharCode(...randomCharCodes.slice(0, stringLength));
}
function insertDelimeter(string, delimeterCode) {
const delimeter = String.fromCharCode(delimeterCode);
let previousDelimeterIndex = 0;
let endReached = false
while (!endReached) {
const interval = intervalFromRange();
const front = string.slice(0, previousDelimeterIndex += interval);
const back = string.slice(previousDelimeterIndex += 1); //account for width of delimeter in string
if (!back.length) {
endReached = true;
continue;
}
string = front + delimeter + back;
}
return string;
}
function intervalFromRange() {
const [small, large] = [5, 9]
const interval = window.crypto.getRandomValues(new Uint8Array(255)).find(int => (int >= small) && (int <= large));
return interval ? interval : intervalFromRange();
}
const numberCodes = range(48, 57);
const uppercaseCodes = range(65, 90);
const lowercaseCodes = range(97, 122);
function validCharCodes(params) {
params.hasOwnProperty('numbers');
params.hasOwnProperty('uppercase');
params.hasOwnProperty('lowercase');
return [
...(params.hasOwnProperty('numbers') ? numberCodes : []),
...(params.hasOwnProperty('uppercase') ? uppercaseCodes : []),
...(params.hasOwnProperty('lowercase') ? lowercaseCodes : []),
...keyCodes(params, 'symbol')
];
}
function keyCodes(params, type) {
return (Object.keys(params).filter(key => key.includes(type)).map(key => parseInt(key.split('-')[1])));
}
function range(start, stop) {
const numbers = [start];
let next = start;
while (next < stop) {
numbers.push(next += 1);
}
return numbers;
}