Notes/src/services/search/note_set.ts
2024-02-17 13:32:42 +02:00

63 lines
1.2 KiB
TypeScript

"use strict";
class NoteSet {
constructor(notes = []) {
/** @type {BNote[]} */
this.notes = notes;
this.noteIdSet = new Set(notes.map(note => note.noteId));
/** @type {boolean} */
this.sorted = false;
}
add(note) {
if (!this.hasNote(note)) {
this.notes.push(note);
this.noteIdSet.add(note.noteId);
}
}
addAll(notes) {
for (const note of notes) {
this.add(note);
}
}
hasNote(note) {
return this.hasNoteId(note.noteId);
}
hasNoteId(noteId) {
return this.noteIdSet.has(noteId);
}
mergeIn(anotherNoteSet) {
this.addAll(anotherNoteSet.notes);
}
minus(anotherNoteSet) {
const newNoteSet = new NoteSet();
for (const note of this.notes) {
if (!anotherNoteSet.hasNoteId(note.noteId)) {
newNoteSet.add(note);
}
}
return newNoteSet;
}
intersection(anotherNoteSet) {
const newNoteSet = new NoteSet();
for (const note of this.notes) {
if (anotherNoteSet.hasNote(note)) {
newNoteSet.add(note);
}
}
return newNoteSet;
}
}
export = NoteSet;