Here is what my javascript file with class looks like:
console.log("calling linkedlist");
class NodeAcc {
constructor(NodeName) {
this._NodeName = NodeName;
this.next = null;
}
// Getter & Settter for AccName
get NodeName() {
return this._NodeName;
}
set NodeName(_NodeName) {
if (typeof _NodeName !== "string") throw new Error("AccName must be a string.");
this._NodeName = _NodeName;
}
};
class LinkedList {
constructor() {
this.head = null;
}
insertAtEnd(AccName) { // For adding new nodes
const newNode = new NodeAcc(AccName);
if (!this.head) {
this.head = newNode;
return;
}
let current = this.head;
while (current.next !== null) {
current = current.next;
}
current.next = newNode;
}
find(AccName) {
let current = this.head;
while (current !== null) {
if (current.AccName === AccName) {
return current;
}
current = current.next;
}
return null;
}
doesItExists(AccName) { // Same as "find" but returns booleans
let current = this.head;
while (current !== null) {
if (current.AccName === AccName) {
return true;
}
current = current.next;
}
return false;
}
printList() {
let current = this.head;
let i = 0;
while (current !== null) {
console.log(`[${i}] Account: ${current.AccName}, Value: ${current.Value}`);
current = current.next;
i++;
}
}
forEach(callback) { // Going to make my LinkedList iterable in a forEach command.
let current = this.head;
let index = 0;
while (current !== null) {
callback(current, index);
current = current.next;
index++;
}
}
}
exports.NodeAcc = NodeAcc;
exports.LinkedList = LinkedList;
This is how I use in a codeblock in a markdown note:
const { NodeAcc, LinkedList } = require(app.vault.adapter.basePath + "/03 RESOURCES/Javascripts/linkedList.js");
var myLinkedList = new LinkedList();
myLinkedList.insertAtEnd("Apple Pie");
myLinkedList.forEach(p => {
dv.el("p", p.NodeName);
})
Thanks to this guy from another post: DataviewJs - Code reuse: common place for scripts - #6 by Craig