Skip to content

feat: add typed, pure library API and fix data-structure bugs - #5

Open
Sakibpedia wants to merge 1 commit into
zonayedpca:masterfrom
Sakibpedia:feat/library-api-v2
Open

Sakibpedia wants to merge 1 commit into
zonayedpca:masterfrom
Sakibpedia:feat/library-api-v2

Conversation

@Sakibpedia

Copy link
Copy Markdown

A proposal to change the foundation

AlgoDS.js currently teaches algorithms as standalone folders whose index.js prints a demo. That is great for learning, but it means the code is not usable as a library: there is no package entry point despite "main": "index.js", no types, functions assume valid input, and several sort/chunk helpers mutate the caller's array.

This PR adds a second, additive foundation: AlgoDS as a pure, typed, importable library. The lesson folders are untouched. Nothing is removed.

What is added

  • index.js — a real public API with four namespaces: strings, arrays, math, patterns, plus dataStructures classes. All functions return values, validate arguments, and never mutate inputs (sorts/chunk copy their arrays first).
  • index.d.ts — full TypeScript declarations for the whole API.
  • tests/library.test.js — 41 tests using Node's built-in test runner, so the suite runs with zero installed dependencies: node --test tests/library.test.js.
  • npm run test:library script, types, exports, files metadata, and corrected repository/bugs/homepage URLs (they pointed to the old Algorithm.js name).
  • README section documenting both worlds side by side, including the philosophy table and usage examples.

Real bugs found by the new suite

Building a tested API exposed defects in the original implementations. They are fixed here, with regression tests:

File Bug Fix
stack/stack.js, qFromStack/stack.js peek() returned data[0] while pop() removes from the end return the last element
stackFromQ/stack.js peek() read this.data (never set) → TypeError two-queue push-costly algorithm with real peek/pop
stackFromQ/queue.js helper queue was LIFO, so "stack from queue" behaved as a queue proper FIFO helper
qFromStack/queue.js moved all elements on every remove, returning the newest instead of the oldest transfer only when s2 is empty
linkedList/fromLast.js off by one: fromLast(list, 2) returned the 3rd node from the end correct two-pointer loop + validation

Verification

  • 41/41 library tests pass on Node 22.
  • tsc --noEmit --strict --skipLibCheck index.d.ts passes.
  • Legacy demos (node fibonacci/index.js, node bubbleSort/index.js) still run unchanged.

This is intentionally a proposal: if you prefer a different namespace layout or want the lesson code frozen completely, I can split the bug fixes into a separate PR and reshape the API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant