CodeVetter · Repo Unpack · Data flow
trekhleb/javascript-algorithms
Data flow is simple and uniform: callers (tests or the playground) invoke a module's default export with plain JS values; the module mutates or returns in-memory structures; results are compared with Jest matchers. There is no persistence, network, or external state — the only file IO is the seam-carving test streaming PNG fixtures through pngjs. The Comparator-injection pattern and traversal callbacks are the two mechanisms that let callers steer internal behavior without subclassing.
- Inputs are plain JS values passed directly to functions/classes (e.g., factorial(number), quickSort.sort(array), linkedList.append(value)); there is no request/response, CLI parsing, or stdin handling anywhere in src/.
- Comparator is the primary data-normalization seam: constructors accept an optional compareFunction, and all ordering decisions flow through comparator.equal/lessThan/greaterThan rather than native operators.
- Sorting transforms input -> cloned array -> pivot-partitioned left/center/right arrays -> recursively sorted concatenated output; QuickSort clones the input (`[...originalArray]`) so the caller's array is never mutated.
- Traversal algorithms move data through a callback pipeline: initCallbacks builds a closed-over `seen` map for allowTraversal, then each vertex flows enterVertex -> neighbor iteration gated by allowTraversal -> leaveVertex.
- Queue is a thin adapter over LinkedList: enqueue delegates to append (tail), dequeue delegates to deleteHead (front), isEmpty checks linkedList.head — showing how data-structures compose as building blocks.
- HashTable.set flows key -> hash() (char-code sum mod bucket count) -> buckets[hash] LinkedList -> find by callback -> append or update node.value.value; get/delete follow the same path.
- Seam carving is the only flow touching the filesystem, and only in tests: pngLoad streams a .png via fs.createReadStream into pngjs, converts to an ImageData-shaped object, then resizeImageWidth mutates img.data in place while tracking a separate logical {w,h} size.
- State owners are instance fields on the structures: LinkedList.head/tail, Graph.vertices/edges (plain objects keyed by getKey()), HashTable.buckets/keys, Heap.heapContainer array, LRUCache.nodesMap/size plus sentinel nodes.
- Outputs are returned values (sorted arrays, found nodes, traversal orders accumulated by caller-supplied callbacks) plus toString() representations that tests use as the primary assertion mechanism (e.g., expect(linkedList.toString()).toBe('1,2')).
- Graph.getAdjacencyMatrix materializes a derived NxN weight matrix filled with Infinity for non-edges by iterating vertices and their neighbors — an explicit transform from adjacency-list storage to matrix output for algorithms like floyd-warshall.