dailyprogrammer
Version:
The /r/dailyprogrammer Tool provides you with a cli to quickly initialize programming challenges and publish them.
48 lines (42 loc) • 1.66 kB
text/typescript
import {IChallengeProvider} from "./IChallengeProvider";
import {Difficulty} from "./Difficulty";
import {IChallenge} from "./IChallenge";
export class JSONChallengeProvider implements IChallengeProvider {
private static _challenges: IChallenge[];
get challenges(): IChallenge[] {
if (!JSONChallengeProvider._challenges) {
JSONChallengeProvider._challenges = this.loadChallenges();
}
return JSONChallengeProvider._challenges;
}
getAll(difficulty?: Difficulty): Promise<IChallenge[]> {
return Promise.resolve(this.challenges)
.then(challenges => {
if (difficulty) {
return challenges.filter(c => c.difficulty === difficulty);
}
return challenges;
});
}
get(id: number, difficulty?: Difficulty): Promise<IChallenge[]> {
return Promise.resolve(this.challenges)
.then(challenges => challenges.filter(c => {
if( difficulty != null && c.difficulty !== difficulty ) {
return false;
}
return c.id === id;
}));
}
private loadChallenges() {
return require('../../data/challenges.json')
.map((item) => <IChallenge>{
date: new Date(item.date),
description: item.description,
difficulty: Difficulty[Difficulty[item.difficulty]] || Difficulty.Unknown,
id: item.id,
uniqid: `${item.id}_${item.difficulty}`,
link: item.link,
title: item.title
});
}
}