-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.js
More file actions
73 lines (61 loc) · 1.83 KB
/
Copy pathfunctions.js
File metadata and controls
73 lines (61 loc) · 1.83 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
67
68
69
70
71
72
73
const ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
export const fetchPoem = async lines => {
const response = await fetch(
'https://poetrydb.org/random,linecount/1;' + lines
)
const poem = (await response.json())[0]
poem.lines = poem.lines.join('\n')
poem.plain = normalize(poem.lines)
poem.alphabet = randAlphabet()
poem.cipher = encrypt(poem.plain, poem.alphabet)
poem.rawSolution = solve(poem.alphabet, poem.cipher)
poem.solution = poem.rawSolution.replace(/_/g, '')
return poem
}
export const fetchTextArea = async () => {
const response = await fetch('/textarea.txt')
return await response.text()
}
export const randInt = (min, max) => {
min = Math.ceil(min)
max = Math.floor(max)
return Math.floor(Math.random() * (max - min + 1)) + min
}
export const randAlphabet = () => {
let a = ALPHABET.split('')
for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1))
;[a[i], a[j]] = [a[j], a[i]]
}
return a.join('')
}
const normalize = text =>
text.toUpperCase().replace(/’/g, "'").replace(/“|”/g, '"').trim()
const encrypt = (plain, alphabet) => {
let cipher = ''
for (let char of plain) {
if ('A' <= char && char <= 'Z')
cipher += alphabet[ALPHABET.indexOf(char)]
else cipher += char
}
return cipher
}
const solve = (alphabet, cipher) => {
let solution = Array(26)
for (let i = 0; i < alphabet.length; i++) {
let char = alphabet[i]
if (cipher.includes(char)) solution[toCode(char)] = toChar(i)
else solution[toCode(char)] = '_'
}
return solution.join('')
}
export const shuffle = array => {
let ret = [...array]
for (let i = ret.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1))
;[ret[i], ret[j]] = [ret[j], ret[i]]
}
return ret
}
export const toCode = char => char.charCodeAt(0) - 65
export const toChar = charCode => String.fromCharCode(charCode + 65)