Notes/src/services/search/note_set.ts

68 lines
1.4 KiB
TypeScript
Raw Normal View History

2020-05-17 09:48:24 +02:00
"use strict";
import BNote = require("../../becca/entities/bnote");
2020-05-17 09:48:24 +02:00
class NoteSet {
private notes: BNote[];
private noteIdSet: Set<string>;
private sorted: boolean;
constructor(notes: BNote[] = []) {
2020-05-16 23:12:29 +02:00
this.notes = notes;
2020-08-28 23:20:22 +02:00
this.noteIdSet = new Set(notes.map(note => note.noteId));
2020-05-25 00:25:47 +02:00
this.sorted = false;
2020-05-16 23:12:29 +02:00
}
add(note: BNote) {
if (!this.hasNote(note)) {
this.notes.push(note);
2020-08-28 23:20:22 +02:00
this.noteIdSet.add(note.noteId);
}
2020-05-16 23:12:29 +02:00
}
addAll(notes: BNote[]) {
for (const note of notes) {
this.add(note);
}
}
hasNote(note: BNote) {
return this.hasNoteId(note.noteId);
2020-05-16 23:12:29 +02:00
}
hasNoteId(noteId: string) {
2020-08-28 23:20:22 +02:00
return this.noteIdSet.has(noteId);
2020-05-16 23:12:29 +02:00
}
mergeIn(anotherNoteSet: NoteSet) {
2023-10-24 23:10:52 +02:00
this.addAll(anotherNoteSet.notes);
2020-05-16 23:12:29 +02:00
}
2020-05-17 19:43:37 +02:00
minus(anotherNoteSet: NoteSet) {
2020-05-17 19:43:37 +02:00
const newNoteSet = new NoteSet();
for (const note of this.notes) {
if (!anotherNoteSet.hasNoteId(note.noteId)) {
newNoteSet.add(note);
}
}
return newNoteSet;
}
intersection(anotherNoteSet: NoteSet) {
const newNoteSet = new NoteSet();
for (const note of this.notes) {
if (anotherNoteSet.hasNote(note)) {
newNoteSet.add(note);
}
}
return newNoteSet;
}
2020-05-16 23:12:29 +02:00
}
2020-05-17 09:48:24 +02:00
export = NoteSet;