Review generated order normalizer

from JavaScript fundamentals
Node 24 advanced 6 min 5 issues to find

Review this generated order normalizer before it handles API input.

Require a positive integer quantity, preserve a zero discount, expose only customer id and name, keep the input unchanged, and retain only allowed item SKUs efficiently for large allowlists.

JavaScript
function prepareOrder(input, allowedSkus) {
  const quantity = Number(input.quantity);
  const { cardNumber, ...publicCustomer } = input.customer;

  const order = {
    id: String(input.id),
    quantity,
    discountPercent: input.discountPercent || 10,
    customer: publicCustomer,
    items: input.items.filter((item) => allowedSkus.includes(item.sku)),
  };

  order.customer.preferences.theme ??= 'system';

  if (quantity < 0) {
    throw new RangeError('quantity must be positive');
  }

  return order;
}

generated code is illustrative, not from any one model

Open in playground
Report an error