Skip to content

Instantly share code, notes, and snippets.

@tyhonchik
Created April 12, 2019 14:17
Show Gist options
  • Select an option

  • Save tyhonchik/1647b2157670d3f116dd8159c776f0c7 to your computer and use it in GitHub Desktop.

Select an option

Save tyhonchik/1647b2157670d3f116dd8159c776f0c7 to your computer and use it in GitHub Desktop.
/*
Задача:
Что вы здесь видите и как реализовать:
let taskList = new TaskList<Task>();
taskList.add(new SimpleTask(title, content));
taskList.add(new FitureTask(title, content, fiture));
taskList.startAll();
*/
enum TaskStatus {
TO_DO,
IN_PROGRESS,
IN_TESTING,
DONE
}
interface ITask {
getTask: () => void;
}
interface ISimpleTask {
title: string;
content: string;
}
interface IFeatureTask extends ISimpleTask {
feature: string;
}
function isFeatureTask(object: any): object is IFeatureTask {
return "feature" in object;
}
class Task implements ITask {
private title: string;
private content: string;
private feature?: string;
status: TaskStatus;
constructor(args: ISimpleTask | IFeatureTask) {
this.title = args.title;
this.content = args.content;
this.status = TaskStatus.TO_DO;
if (isFeatureTask(args)) {
this.feature = args.feature;
}
this.getTask();
}
getTask = () => {
const { title, content, feature, status } = this;
console.log(
`Task is: ${title}, ${content}${
feature ? `, ${feature}` : ""
} with status: ${TaskStatus[status]}`
);
};
}
class TaskList<T extends Task = Task> {
private taskList: T[] = [];
add(newElement: T): void {
this.taskList.push(newElement);
}
startAll(): void {
this.taskList.forEach(task =>
task.status === TaskStatus.TO_DO || !task.status
? (task.status = TaskStatus.IN_PROGRESS)
: null
);
}
}
class SimpleTask extends Task {
constructor(title: string, content: string) {
super({ title, content });
}
}
class FeatureTask extends Task {
constructor(title: string, content: string, feature: string) {
super({ title, content, feature });
}
}
// ======== main ========
// Создаем экземпляр списка задач
let taskList = new TaskList<Task>();
// Добавляем задачу без фичи
taskList.add(new SimpleTask("titleSimple", "simpleContent"));
// Добавляем задачу с фичей
taskList.add(new FeatureTask("featSimple", "featContent", "featFeature"));
// Переводим все задачи в InProgress
taskList.startAll();
console.log(taskList);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment