-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathmain.js
104 lines (84 loc) · 2.41 KB
/
main.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
98
99
100
101
102
103
104
const canvasSel = '#myCanvas'
const {canvas, ctx, bb} = getCanvas(canvasSel)
function main() {
const experiment = new Experiment()
// Update Candidate Details
updateCandidateDetails(Experiment)
console.log({canvas, ctx, bb})
experiment.run()
}
// FIXME: Relax the validators for multiple names
function updateCandidateDetails({rollNos,names}) {
let isValidRollNo, isValidName, act, byHtml, n
isValidRollNo = (isValidName = false)
// Validate RollNo
rollNos = rollNos.split(',')
.map(
s => Number(s.trim())
)
.filter(
n => {
isValidRollNo = !isNaN(n) && 9999999 < n
if (!isValidRollNo) {
console.warn({invalidRollNo: n,
message: "Roll No should be Integer."})
}
return isValidRollNo
}
)
// Validate Name
const titleCasePat = /^[A-Z][a-z]+( [A-Z][a-z]+)*$/
names = String(names).trim()
act = names.split('(').shift().trim()
isValidName = titleCasePat.test(act)
if (!isValidName) {
console.warn({invalidAct: act,
message: "Act should be in Title Case."})
act = ''
}
names = names.split('(').pop().split(')').shift()
.split(',').map(s=>s.trim())
.filter(name => {
isValidName = titleCasePat.test(name)
if (!isValidName) {
console.warn({invalidName: name,
message: "Name should be a Title Case."})
}
return isValidName
})
byHtml = ''
n = Math.min(names.length, rollNos.length)
for (const i of Array(n).keys()) {
if (0 < i)
byHtml = `${byHtml}<span class="p-2">|</span>`
byHtml = `${byHtml}<code>${rollNos[i]}</code> : ${names[i]}`
}
if (0 < act.length) {
byHtml = `<strong>${act}</strong><br class="hidden md:inline"/><span class="p-4 md:hidden">—</span>${byHtml}`
}
if (0 < byHtml.length) {
byHtml = `Created by ${byHtml}`
} else {
byHtml
= 'Error parsing candidate details. Check console.'
}
document.querySelector('#by')
.innerHTML = byHtml
}
function saveCanvas() {
const link = document.createElement('a');
const {canvas, ctx, bb} = getCanvas(canvasSel)
link.download = `${slug(Experiment.rollNos)}-${slug(document.title)}-canvas.png`;
link.href = canvas.toDataURL()
link.click();
}
// ----------------------------------------------------
// String Manipulations
// ----------------------------------------------------
function slug(s) {
return s
.toLowerCase()
.split(/[^a-z0-9]+/)
.filter(s=>0<s.length)
.join('-')
}