mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-07-27 10:02:59 +08:00
68 lines
1.4 KiB
TypeScript
68 lines
1.4 KiB
TypeScript
"use strict";
|
|
|
|
import type BNote from "../../becca/entities/bnote.js";
|
|
|
|
class NoteSet {
|
|
private noteIdSet: Set<string>;
|
|
|
|
notes: BNote[];
|
|
sorted: boolean;
|
|
|
|
constructor(notes: BNote[] = []) {
|
|
this.notes = notes;
|
|
this.noteIdSet = new Set(notes.map((note) => note.noteId));
|
|
this.sorted = false;
|
|
}
|
|
|
|
add(note: BNote) {
|
|
if (!this.hasNote(note)) {
|
|
this.notes.push(note);
|
|
this.noteIdSet.add(note.noteId);
|
|
}
|
|
}
|
|
|
|
addAll(notes: BNote[]) {
|
|
for (const note of notes) {
|
|
this.add(note);
|
|
}
|
|
}
|
|
|
|
hasNote(note: BNote) {
|
|
return this.hasNoteId(note.noteId);
|
|
}
|
|
|
|
hasNoteId(noteId: string) {
|
|
return this.noteIdSet.has(noteId);
|
|
}
|
|
|
|
mergeIn(anotherNoteSet: NoteSet) {
|
|
this.addAll(anotherNoteSet.notes);
|
|
}
|
|
|
|
minus(anotherNoteSet: NoteSet) {
|
|
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;
|
|
}
|
|
}
|
|
|
|
export default NoteSet;
|