Skip to content

Commit 2a6598d

Browse files
committed
Create and configure bigRepl
- run bigRepl `node bigRepl.js` to interact with data-structures
1 parent f464c81 commit 2a6598d

File tree

5 files changed

+149
-1
lines changed

5 files changed

+149
-1
lines changed

bigRepl.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// repl.js
2+
import repl from 'node:repl';
3+
import util from 'node:util';
4+
import fs from 'node:fs/promises';
5+
import path from 'node:path';
6+
import { fileURLToPath } from 'node:url';
7+
8+
// Helper to get __dirname equivalent in ES Modules
9+
const __filename = fileURLToPath(import.meta.url);
10+
const __dirname = path.dirname(__filename);
11+
const DATA_STRUCTURES_DIR = path.join(__dirname, 'data-structures');
12+
13+
// Function to dynamically import all specified modules and add to context
14+
async function loadDataStructures(context) {
15+
const filesToImport = [
16+
'/data-structures/dijkstra/Dijkstras.js',
17+
'/data-structures/graph/Graph.js',
18+
'/data-structures/hashmap/HashMap.js',
19+
'/data-structures/heap/MinHeap.js',
20+
'/data-structures/heap/MaxHeap.js',
21+
'/data-structures/linked-lists/singly-linked-list/LinkedList.js',
22+
'/data-structures/priority-queue/PriorityQueue.js',
23+
'/data-structures/queue/Queue.js',
24+
'/data-structures/stack/Stack.js',
25+
'/data-structures/tree/TreeNode.js',
26+
'/data-structures/tree/binaryTree.js',
27+
'/data-structures/sorting-algos/bubbleSort.js',
28+
'/data-structures/sorting-algos/getX.js',
29+
'/data-structures/sorting-algos/mergeSort.js',
30+
'/data-structures/sorting-algos/quickSort.js'
31+
];
32+
33+
// Named exports handled separately
34+
const namedExportsFile = '/data-structures/sorting-algos/binarySearch.js';
35+
36+
// Track loaded module names
37+
const loadedModules = [];
38+
39+
for (const relativePath of filesToImport) {
40+
const fullPath = path.join(__dirname, relativePath);
41+
// Ensure path uses file:// protocol for dynamic import
42+
const modulePath = `file://${fullPath}`;
43+
44+
try {
45+
const module = await import(modulePath);
46+
// Extract the default export
47+
// Use the filename (without extension) as the context key (e.g., 'Dijkstras')
48+
const fileName = path.basename(relativePath, path.extname(relativePath));
49+
context[fileName] = module.default;
50+
loadedModules.push(fileName);
51+
} catch (error) {
52+
console.error(`Failed to import ${relativePath}: ${error.message}`);
53+
}
54+
}
55+
56+
// Handle named exports for binarySearch.js
57+
const binarySearchPath = `file://${path.join(__dirname, namedExportsFile)}`;
58+
try {
59+
const binarySearchModule = await import(binarySearchPath);
60+
context.binarySearchIterative = binarySearchModule.binarySearchIterative;
61+
context.binarySearchRecursive = binarySearchModule.binarySearchRecursive;
62+
loadedModules.push('binarySearchIterative', 'binarySearchRecursive');
63+
} catch (error) {
64+
console.error(`Failed to import named exports from ${namedExportsFile}: ${error.message}`);
65+
}
66+
67+
console.log(`\nSuccessfully loaded modules: ${loadedModules.join(', ')}\n`);
68+
}
69+
70+
71+
// --- Start REPL and Expose Context ---
72+
73+
const replServer = repl.start({
74+
prompt: '\x1b[38;2;255;0;255m▶ \x1b[0m',
75+
writer: (output) => {
76+
return util.inspect(output, { compact: true, breakLength: Infinity, colors: true });
77+
}
78+
});
79+
80+
// Load data structures into the REPL context asynchronously
81+
await loadDataStructures(replServer.context);
82+
83+
console.log("Welcome to @tjupps REPL! Data structures loaded.");
84+
85+
// Example usage might look like:
86+
// const list = new LinkedList();
87+
// list.add(5);
88+
// list; // to see the object structure
89+

countDown.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export function countDownIterative(count) {
2+
while (count > 0) {
3+
console.log(count);
4+
count--;
5+
}
6+
console.log('Blast Off~!');
7+
}
8+
9+
console.log('Iterative Countdown:');
10+
countDownIterative(3);
11+
12+
export function countDownRecursive(count) {
13+
if (count === 0) {
14+
console.log('Blast Off~!');
15+
return;
16+
}
17+
console.log(count);
18+
countDownRecursive(count - 1);
19+
}
20+
console.log('Recursive Countdown:');
21+
countDownRecursive(3);
22+
23+

countDown.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// import {describe, it} from 'node:test';
2+
import assert from 'node:assert/strict';
3+
import {jest} from '@jest/globals';
4+
import util from 'node:util';
5+
import {style} from './styles.js';
6+
import { countDownIterative } from './countDown.js';
7+
import { countDownRecursive } from './countDown.js';
8+
util.inspect.defaultOptions.depth = null; // show full objects
9+
// util.inspect.defaultOptions.depth = 0; // show truncated objects
10+
// util.inspect.defaultOptions.compact = true; // dont break objects to new lines
11+
// util.inspect.defaultOptions.compact = false; // break objects to new lines
12+
13+
// suppress jests tracing console logs
14+
import console from 'console';
15+
const jestConsole = console;
16+
17+
beforeEach(() => {
18+
global.console = console;
19+
console.log(style.color(255,0,255),'▷',style.reset,style.color(39),expect.getState().currentTestName,style.reset,'\n'); });
20+
21+
afterEach(() => {
22+
global.console = jestConsole;
23+
console.log(style.color(99), style.hr.double, style.reset);
24+
});
25+
26+
describe('countDown', () => {
27+
it('should count down recursively and iteratively', () => {
28+
console.log('Iterative Countdown:');
29+
countDownIterative(3);
30+
console.log('Recursive Countdown:');
31+
countDownRecursive(3);
32+
});
33+
34+
});
35+
36+

data-structures/graph/Vertex.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Edge from './Edge';
1+
import Edge from './Edge.js';
22

33
class Vertex {
44
constructor(data) {

sortProp.js

Whitespace-only changes.

0 commit comments

Comments
 (0)