Skip to content

Commit bf4b28a

Browse files
committed
Add JSDOM to bigRepl
1 parent 2a6598d commit bf4b28a

2 files changed

Lines changed: 30 additions & 5 deletions

File tree

bigRepl.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
// repl.js
2+
import { style } from './styles.js';
23
import repl from 'node:repl';
34
import util from 'node:util';
45
import fs from 'node:fs/promises';
56
import path from 'node:path';
67
import { fileURLToPath } from 'node:url';
8+
import { JSDOM } from 'jsdom'; // Import JSDOM
79

8-
// Helper to get __dirname equivalent in ES Modules
10+
// ... (rest of the __filename, __dirname, and DATA_STRUCTURES_DIR code remains the same)
911
const __filename = fileURLToPath(import.meta.url);
1012
const __dirname = path.dirname(__filename);
1113
const DATA_STRUCTURES_DIR = path.join(__dirname, 'data-structures');
@@ -23,7 +25,7 @@ async function loadDataStructures(context) {
2325
'/data-structures/queue/Queue.js',
2426
'/data-structures/stack/Stack.js',
2527
'/data-structures/tree/TreeNode.js',
26-
'/data-structures/tree/binaryTree.js',
28+
'/data-structures/tree/BinaryTree.js',
2729
'/data-structures/sorting-algos/bubbleSort.js',
2830
'/data-structures/sorting-algos/getX.js',
2931
'/data-structures/sorting-algos/mergeSort.js',
@@ -67,6 +69,11 @@ async function loadDataStructures(context) {
6769
console.log(`\nSuccessfully loaded modules: ${loadedModules.join(', ')}\n`);
6870
}
6971

72+
// Function to load JSDOM into the REPL context
73+
function loadJSDOM(context) {
74+
context.JSDOM = JSDOM;
75+
console.log('JSDOM class loaded into context.');
76+
}
7077

7178
// --- Start REPL and Expose Context ---
7279

@@ -80,7 +87,25 @@ const replServer = repl.start({
8087
// Load data structures into the REPL context asynchronously
8188
await loadDataStructures(replServer.context);
8289

83-
console.log("Welcome to @tjupps REPL! Data structures loaded.");
90+
// Load JSDOM into the REPL context
91+
loadJSDOM(replServer.context);
92+
93+
console.log(`Welcome to ${style.cyan,style.bold}@tjupps${style.reset} REPL! Data structures and JSDOM loaded.`);
94+
95+
// --- Initialize JSDOM variables for immediate use ---
96+
97+
// Create an initial virtual DOM structure
98+
const dom = new JSDOM(`<!DOCTYPE html><html><body><h1>REPL DOM!</h1></body></html>`);
99+
100+
// Expose 'window', 'document', and 'body' to the REPL context
101+
replServer.context.window = dom.window;
102+
replServer.context.document = dom.window.document;
103+
replServer.context.body = dom.window.document.body;
104+
replServer.context.JSDOM = JSDOM; // Ensure JSDOM class is also available if needed for new DOMs
105+
106+
console.log(`A 'document' and 'window' are ready to use.
107+
Try: ${style.purple}const div = document.createElement('div'); div.textContent = 'Check me out Son!'; document.body.appendChild(div); document.body.innerHTML${style.reset}`
108+
);
84109

85110
// Example usage might look like:
86111
// const list = new LinkedList();

data-structures/tree/binaryTree.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ util.inspect.defaultOptions.compact = false; // break objects to new lines
1111
// suppress jests tracing console logs
1212
import console from 'console';
1313
const jestConsole = console;
14-
import binaryTree from './binaryTree.js';
14+
import BinaryTree from './BinaryTree.js';
1515

1616
const createBinaryTree = (size) => {
17-
const bt = new binaryTree(0);
17+
const bt = new BinaryTree(0);
1818
for (let node = 1; node <= size; node++) {
1919
bt.insert(node);
2020
}

0 commit comments

Comments
 (0)