-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
67 lines (53 loc) · 1.57 KB
/
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
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
import fs from "fs";
import chalk from "chalk";
import lodash from "lodash";
const { groupBy, mapValues, sampleSize } = lodash;
const readFileSync = (path) => fs.readFileSync(path, { encoding: "utf-8" });
export const getOrCreate = (path) => {
try {
return readFileSync(path);
} catch (e) {
fs.writeFileSync(path, "[]");
return readFileSync(path);
}
};
export const writeSolvedFiles = (path, prevSolvedArray, currSolvedArray) => {
fs.writeFileSync(
path,
JSON.stringify([...prevSolvedArray, ...currSolvedArray].sort())
);
};
const DifficultyColors = {
warm: "008080",
easy: "7aad0c",
medium: "d9901a",
hard: "de3d37",
extreme: "b11b8d",
};
export const findRandomExtinctItems = (items, count) => {
const targets = new Set();
const levelMap = mapValues(
groupBy(items, (item) => item.split("-")[1]),
(items) => sampleSize(items, count)
);
const levels = Object.keys(DifficultyColors);
let levelIdx = 0;
while (
targets.size < count &&
Object.values(levelMap).some((v) => v.length)
) {
const levelQuestions = levelMap[levels[levelIdx]];
levelQuestions?.length > 0 && targets.add(levelQuestions.shift());
levelIdx = (levelIdx + 1) % levels.length;
}
return Array.from(targets);
};
export const getQuestionUrl = (id) => `https://tsch.js.org/${id}/play/ko`;
export const printQuestion = (question) => {
const [id, difficulty, name] = question.split("-");
console.log(
`${chalk.bgHex(DifficultyColors[difficulty])(
`[${difficulty}]`
)} ${name} : ${getQuestionUrl(parseInt(id, 10))}`
);
};