-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathchallenges.ts
202 lines (189 loc) · 5.46 KB
/
challenges.ts
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import { randomInt } from 'crypto';
import fs from 'fs';
import { homedir } from 'os';
import path from 'path';
import jsdom from 'jsdom';
interface IChallenges {
/**
* Generate a random challenge from the given set of challenges.
*
* @returns {string}
*/
getRandomChallenge(): string;
/**
* List out the challenges.
*
* @returns {string[]}
*/
listChallenges(): string[];
/**
* Function to add a challenge to the local storage.
* The input must be a valid URL, otherwise it will throw an Error.
*
* @param {string} str
* @returns {string}
*/
addChallenge(str: string): Promise<string>;
}
class Challenges implements IChallenges {
/**
* This is an initial data used for local storage initialization.
*
* @private
*/
private initialData = {
challenges: [
{
name: 'Write Your Own wc Tool',
url: 'https://codingchallenges.fyi/challenges/challenge-wc'
},
{
name: 'Write Your Own JSON Parser',
url: 'https://codingchallenges.fyi/challenges/challenge-json-parser'
},
{
name: 'Write Your Own Compression Tool',
url: 'https://codingchallenges.fyi/challenges/challenge-huffman'
},
{
name: 'Write Your Own cut Tool',
url: 'https://codingchallenges.fyi/challenges/challenge-cut'
},
{
name: 'Write You Own Load Balancer',
url: 'https://codingchallenges.fyi/challenges/challenge-load-balancer'
},
{
name: 'Write Your Own Sort Tool',
url: 'https://codingchallenges.fyi/challenges/challenge-sort'
},
{
name: 'Write Your Own Calculator',
url: 'https://codingchallenges.fyi/challenges/challenge-calculator'
},
{
name: 'Write Your Own Redis Server',
url: 'https://codingchallenges.fyi/challenges/challenge-redis'
},
{
name: 'Write Your Own grep',
url: 'https://codingchallenges.fyi/challenges/challenge-grep'
},
{
name: 'Write Your Own uniq Tool',
url: 'https://codingchallenges.fyi/challenges/challenge-uniq'
},
{
name: 'Write Your Own Web Server',
url: 'https://codingchallenges.fyi/challenges/challenge-webserver'
},
{
name: 'Write Your Own URL Shortener',
url: 'https://codingchallenges.fyi/challenges/challenge-url-shortener'
},
{
name: 'Write Your Own diff Tool',
url: 'https://codingchallenges.fyi/challenges/challenge-diff'
},
{
name: 'Write Your Own Shell',
url: 'https://codingchallenges.fyi/challenges/challenge-shell'
},
{
name: 'Write Your Own cat Tool',
url: 'https://codingchallenges.fyi/challenges/challenge-cat'
},
{
name: 'Write Your Own IRC Client',
url: 'https://codingchallenges.fyi/challenges/challenge-irc'
},
{
name: 'Write Your Own Memcached Server',
url: 'https://codingchallenges.fyi/challenges/challenge-memcached'
},
{
name: 'Write Your Own Spotify Client',
url: 'https://codingchallenges.fyi/challenges/challenge-spotify'
}
]
};
/**
* All the operations will be performed around this data.
*
* @private
*/
private data: {
challenges: {
name: string;
url: string;
}[];
};
/**
* The path where the data will be stored.
*
* @private
* @type {string}
*/
private filePath: string;
constructor() {
this.filePath = path.join(homedir(), '.cc_challenges_list');
if (fs.existsSync(this.filePath)) {
this.data = JSON.parse(fs.readFileSync(this.filePath).toString());
} else {
this.data = this.initialData;
this.saveToStorage();
}
}
private saveToStorage() {
fs.writeFileSync(this.filePath, JSON.stringify(this.data));
}
private isPresent(url: string): boolean {
for (let i = 0; i < this.data.challenges.length; i++) {
if (this.data.challenges[i].url.indexOf(url) >= 0) {
return true;
}
}
return false;
}
async addChallenge(str: string): Promise<string> {
try {
const url = new URL(str);
if (!str.startsWith('https://codingchallenges.fyi')) {
throw new Error(`Unable to add ${str}. Invalid domain`);
}
if (this.isPresent(str)) {
throw new Error(`Unable to add ${str}. URL already present`);
}
const res = await fetch(url, {
headers: { 'Content-Type': 'text/html' }
});
const html = await res.text();
const dom = new jsdom.JSDOM(html);
const titleElem = dom.window.document.getElementsByTagName('title')[0];
if (!titleElem) {
throw new Error(`Unable to add ${str}. No title element found in HTML`);
}
const title = titleElem.innerHTML.split('|')[0].trim();
this.data.challenges.push({ name: title, url: str });
this.saveToStorage();
return title + ' ' + str;
} catch (e) {
console.error(`Unable to add ${str}. ${e}`);
throw e;
}
}
getRandomChallenge(): string {
const challengesLength = this.data.challenges.length;
const challenge = this.data.challenges[randomInt(challengesLength)];
return challenge.name + ' ' + challenge.url;
}
listChallenges(): string[] {
const output: string[] = [];
this.data.challenges.forEach((value) => {
output.push(value.name + ' ' + value.url);
});
return output;
}
}
const Storage = new Challenges();
export default Storage;