feat: add cart totals module and cat color helpers - #66
Conversation
Generated descriptionAdd cart calculation functions for subtotals, discounts, and final totals, with validation for discount percentages. Add cat color helpers to classify neutral colors and parse validated Topics
MergerNeeds Review The new cart module uses ESM exports while node/package.json does not enable ESM, causing supported Node runtimes to fail on import; the unresolved NaN validation issue is also still present. Commit |
|
@baz-reviewer-dev please summarize this PR |
| export function subtotal(items) { | ||
| return items.reduce((sum, item) => sum + item.price * item.quantity, 0); |
There was a problem hiding this comment.
Cart module fails on supported Node runtimes
cart.js uses ESM syntax but node/package.json doesn't declare "type": "module", so Node 18+ treats it as CommonJS and throws a syntax error on import — should we add "type": "module" or rewrite it as CommonJS?
Want Baz to fix this for you? Activate Fixer
Prompt for AI Agents
Before applying, verify this suggestion against the current code. In node/cart.js around
lines 1-14, the module uses ESM `export function ...` but the project’s
node/package.json likely doesn’t set ESM mode (no `
| if (percent < 0 || percent > 100) { | ||
| throw new RangeError("percent must be between 0 and 100"); | ||
| } |
There was a problem hiding this comment.
NaN discount yields invalid totals
NaN passes both bounds checks since NaN < 0 and NaN > 100 are both false, so applyDiscount returns NaN and total exposes an invalid total — should we reject non-finite percentages with if (!Number.isFinite(percent) || percent < 0 || percent > 100)?
Want Baz to fix this for you? Activate Fixer
Prompt for AI Agents
Before applying, verify this suggestion against the current code. In node/cart.js around
lines 6-8 inside the `applyDiscount(amount, percent)` function, the current range check
only handles values <0 or >100, so `percent = NaN` (or Infinity) bypasses the
comparisons and results in a `NaN` total. Refactor the guard to explicitly reject
non-finite percentages by adding a `Number.isFinite(percent)` check (and keep the
existing 0–100 bounds), then ensure the function throws a clear error for NaN/Infinity
as well as out-of-range numbers. Add/update a small unit test (or minimal coverage) to
assert that `total(items, NaN)` and `total(items, Infinity)` throw instead of returning
`NaN`.
Adds a small cart totals module for the node example and colour helpers for the NestJS cats DTO.
Testing: exercised both modules by hand.