Notes/src/services/search/note_set.js

64 lines
1.3 KiB
JavaScript
Raw Normal View History

2020-05-17 09:48:24 +02:00
"use strict";
class NoteSet {
2020-05-16 23:12:29 +02:00
constructor(notes = []) {
2020-05-23 23:44:55 +02:00
/** @type {Note[]} */
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
/** @type {boolean} */
this.sorted = false;
2020-05-16 23:12:29 +02:00
}
add(note) {
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) {
for (const note of notes) {
this.add(note);
}
}
hasNote(note) {
return this.hasNoteId(note.noteId);
2020-05-16 23:12:29 +02:00
}
hasNoteId(noteId) {
2020-08-28 23:20:22 +02:00
return this.noteIdSet.has(noteId);
2020-05-16 23:12:29 +02:00
}
mergeIn(anotherNoteSet) {
this.notes = this.notes.concat(anotherNoteSet.notes);
2020-08-28 23:20:22 +02:00
this.noteIdSet = new Set(this.notes.map(note => note.noteId));
2020-05-16 23:12:29 +02:00
}
2020-05-17 19:43:37 +02:00
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;
}
2020-05-16 23:12:29 +02:00
}
2020-05-17 09:48:24 +02:00
module.exports = NoteSet;