Skip to content

Build your personal knowledge base with Trilium Notes

1.6 KB
Raw Blame
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);
    }

    /** The note IDs as one JSON array, the parameter of `IN (SELECT value FROM json_each(?))`. */
    toSqlParam() {
        return JSON.stringify([ ...this.noteIdSet ]);
    }

    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;