-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
40 lines (37 loc) · 915 Bytes
/
utils.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
export function calculateScore(
maxBugs,
maxHints,
time,
solutionType,
bugs,
hints
) {
return (
0.75 * (1 - hints / maxHints) +
(1 - bugs / maxBugs) +
(1 - time / 45) +
0.75 * (solutionType / 4)
);
}
export function removeUnicodeAndLowerCase(str) {
return str
.replace(/[^\x00-\x7F]/g, "")
.toLowerCase()
.trim();
}
export function sanitizeInput(input) {
return input ? input.replace(/</g, "<").replace(/>/g, ">") : "";
}
// Source: https://stackoverflow.com/questions/3426404/create-a-hexadecimal-colour-based-on-a-string-with-javascript
export const stringToColor = (str) => {
let hash = 0;
str.split("").forEach((char) => {
hash = char.charCodeAt(0) + ((hash << 5) - hash);
});
let color = "#";
for (let i = 0; i < 3; i++) {
const value = (hash >> (i * 8)) & 0xff;
color += value.toString(16).padStart(2, "0");
}
return color;
};