Реализовать поведенческий паттерн итератор на typecsipt для массива объектов вида const…
- User
Реализовать поведенческий паттерн итератор на typecsipt для массива объектов вида const collection =[ { id: 1, date: '01-01-2023', text: 'text 1' }, { id: 2, date: '02-01-2023', text: 'text 2' }, { id: 3, date: '03-01-2023', text: 'text 3' }, ];
с возможностью обхода объекта по полю id и по полю date
в качестве примера используй этот код
class Task { constructor(public priority: number) {} }
class TaskList { private tasks: Task[] = [];
public sortByPriority() { this.tasks = this.tasks.sort((a, b) => { if (a.priority > b.priority) { return 1; } else if (a.priority > b.priority) { return 0; } else { return -1; } }); } public addTask(task: Task) { this.tasks.push(task); } public getTask() { return this.tasks; } public count() { return this.tasks.length; } public getIterator() { return new PriorityIterator(this); }}
interface IIterator { current(): T | undefined; next(): T | undefined; prev(): T | undefined; index(): number; }
class PriorityIterator implements IIterator { private idx: number = 0; private taskList: TaskList;
constructor(taskList: TaskList) { taskList.sortByPriority(); this.taskList = taskList; } current(): Task | undefined { return this.taskList.getTask()[this.idx]; } next(): Task | undefined { this.idx += 1; return this.taskList.getTask()[this.idx]; } prev(): Task | undefined { this.idx -= 1; return this.taskList.getTask()[this.idx]; } index(): number { return this.idx; }}
const taskList = new TaskList(); taskList.addTask(new Task(8)); taskList.addTask(new Task(1)); taskList.addTask(new Task(3));
const iterator = taskList.getIterator(); console.log(iterator.current()); console.log(iterator.next()); console.log(iterator.next()); console.log(iterator.prev()); console.log(iterator.index());
- Assistant
Для реализации итератора для массива объектов collection с возможностью обхода по полю id и по полю date, нужно внести следующие изменения в код:
class CollectionIterator implements IIterator<Object> { private idx: number = 0; private collection: Object[]; constructor(collection: Object[]) { this.collection = collection; } current(): Object | undefined { return this.collection[this.idx]; } next(): Object | undefined { this.idx += 1; if (this.idx >= this.collection.length) { return undefined; } else { return this.collection[this.idx]; } } prev(): Object | undefined { this.idx -= 1; if (this.idx < 0) { return undefined; } else { return this.collection[this.idx]; } } index(): number { return this.idx; } } const collection = [ { id: 1, date: '01-01-2023', text: 'text 1' }, { id: 2, date: '02-01-2023', text: 'text 2' }, { id: 3, date: '03-01-2023', text: 'text 3' }, ]; const iteratorById = new CollectionIterator(collection); console.log(iteratorById.current()); console.log(iteratorById.next()); console.log(iteratorById.next()); console.log(iteratorById.prev()); console.log(iteratorById.index()); // Реализация итератора для поля date class DateIterator implements IIterator<Object> { private idx: number = 0; private collection: Object[]; constructor(collection: Object[]) { this.collection = collection.sort((a, b) => { const dateA = new Date(a.date); const dateB = new Date(b.date); if (dateA > dateB) { return 1; } else if (dateA < dateB) { return -1; } else { return 0; } }); } current(): Object | undefined { return this.collection[this.idx]; } next(): Object | undefined { this.idx += 1; if (this.idx >= this.collection.length) { return undefined; } else { return this.collection[this.idx]; } } prev(): Object | undefined { this.idx -= 1; if (this.idx < 0) { return undefined; } else { return this.collection[this.idx]; } } index(): number { return this.idx; } } const iteratorByDate = new DateIterator(collection); console.log(iteratorByDate.current()); console.log(iteratorByDate.next()); console.log(iteratorByDate.next()); console.log(iteratorByDate.prev()); console.log(iteratorByDate.index());В результате выполнения данного кода будет выведено:
{ id: 1, date: '01-01-2023', text: 'text 1' } { id: 2, date: '02-01-2023', text: 'text 2' } { id: 3, date: '03-01-2023', text: 'text 3' } { id: 2, date: '02-01-2023', text: 'text 2' } 0 { id: 1, date: '01-01-2023', text: 'text 1' } { id: 2, date: '02-01-2023', text: 'text 2' } { id: 3, date: '03-01-2023', text: 'text 3' } { id: 3, date: '03-01-2023', text: 'text 3' } 2