Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions node/cart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export function subtotal(items) {
return items.reduce((sum, item) => sum + item.price * item.quantity, 0);
Comment on lines +1 to +2

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Severity web_search

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 `

}

export function applyDiscount(amount, percent) {
if (percent < 0 || percent > 100) {
throw new RangeError("percent must be between 0 and 100");
}
Comment on lines +6 to +8

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)?

Severity

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`.

return amount * (1 - percent / 100);
}

export function total(items, discountPercent = 0) {
return applyDiscount(subtotal(items), discountPercent);
}
12 changes: 12 additions & 0 deletions typescript/nestjs/src/cats/dto/cat.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,15 @@ export enum CatColor {
WHITE = "white",
GRAY = "gray",
}

export function isNeutralColor(color: CatColor): boolean {
return color === CatColor.WHITE || color === CatColor.GRAY;
}

export function parseCatColor(value: string): CatColor {
const match = Object.values(CatColor).find((color) => color === value);
if (!match) {
throw new Error(`Unknown cat color: ${value}`);
}
return match;
}