CodeVetter · Repo Unpack · Behavior traces
trekhleb/javascript-algorithms
Three flows were traced end-to-end. (1) Test execution: npm test -> jest -> testRegex match -> test file imports module under test -> assertions on return values/toString. (2) Sorting: QuickSort construction -> Sort.initSortingCallbacks -> sort() recursion driven by Comparator and visitingCallback. (3) BFS over a graph: callback init with seen-set -> Queue enqueue/dequeue -> getNeighbors -> allowTraversal gating. A fourth flow (seam carving) shows the only IO path: PNG load -> energy map -> DP seam -> in-place pixel shift.
- Test-run trace: `npm test` invokes jest, which applies testRegex '(/__tests__/.*|(\\.|/)(test|spec))\\.jsx?$' from jest.config.js, picks up files like src/data-structures/linked-list/__test__/LinkedList.test.js, which imports ../LinkedList, which imports LinkedListNode and ../../utils/comparator/Comparator.
- Sorting trace (QuickSort): new QuickSort(callbacks) -> Sort constructor -> Sort.initSortingCallbacks fills visitingCallback stub and wraps compareCallback in Comparator -> sort(originalArray) clones array -> shift() pivot -> partition loop calls callbacks.visitingCallback and comparator.equal/lessThan -> recursive sort of left/right -> concat result.
- BFS trace: breadthFirstSearch(graph, startVertex, callbacks) -> initCallbacks builds allowTraversal closure with `seen` map -> new Queue() -> enqueue(startVertex) -> while !isEmpty: dequeue -> enterVertex -> graph.getNeighbors(currentVertex) -> allowTraversal decides enqueue -> leaveVertex -> repeat.
- DFS trace: depthFirstSearch -> initCallbacks (same seen-set pattern as BFS) -> depthFirstSearchRecursive -> enterVertex -> forEach neighbor -> allowTraversal gate -> recurse -> leaveVertex; recursion depth equals longest path, no explicit stack structure.
- Seam-carving trace: test calls pngLoad (fs.createReadStream.pipe(new PNG()).on('parsed')) to build an ImageData-shaped object -> resizeImageWidth({img,toWidth}) -> per removed column: calculateEnergyMap (getPixelEnergy of left/middle/right RGB deltas) -> findLowEnergySeam (DP seamPixelsMap then backtrack via `previous` links) -> deleteSeam (row-wise setPixel left-shift) -> return {img, size}.
- HashTable write trace: set(key,value) -> hash(key) (charCode sum % buckets.length) -> this.keys[key]=keyHash -> bucket LinkedList.find({callback: nodeValue.key===key}) -> append new {key,value} node or overwrite node.value.value.
- LinkedList deletion trace: delete(value) strips all matching head nodes in a while loop, then walks currentNode.next unlinking matches via compare.equal, finally reassigns this.tail if the tail matched — O(n) with the injected comparator deciding equality.
- LRUCache.get trace: get(key) -> nodesMap lookup -> promote(node) which evicts then re-appends the node to the tail of the doubly-linked list -> return node.val; set() appends a new node and evicts head.next when size exceeds capacity.
- CI pipeline trace: push/PR to master -> actions/checkout@v4 -> setup-node 22.x -> npm i -> npm run lint (eslint ./src/**) -> npm run coverage (jest --coverage, enforcing 100/95/100/100 thresholds) -> codecov upload.