All files / src/helpers shipping.ts

95.23% Statements 20/21
87.5% Branches 7/8
100% Functions 1/1
95.23% Lines 20/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 631x                   1x   1x             1x                   1x                                 1x 1x 85x 5x 5x 80x 85x 85x 44x 85x 85x 36x 85x   85x 85x  
/**
 * Shipping-destination logic, split out of `SupplierBase` so the UI can answer
 * "does this supplier ship to me?" without importing the supplier layer. The
 * module deliberately has **no imports**: it is reached from the results table and
 * the column drawer, which mount before any search has run.
 *
 * @module shipping
 * @category Country Helpers
 * @group Suppliers
 * @source
 */
 
/**
 * The shipping fields a supplier exposes — either as `static` class members
 * (`SupplierStaticMeta`) or as an entry in `SUPPLIER_META`. Accepts a readonly
 * `shipsTo` so both shapes satisfy it.
 * @category Country Helpers
 * @group Suppliers
 * @source
 */
export interface ShippingMeta {
  /** The supplier's coarse shipping scope. */
  readonly shipping: ShippingRange;
  /** The supplier's home country (ISO 3166-1 alpha-2). */
  readonly country: CountryCode;
  /** Optional explicit destination allowlist; overrides {@link ShippingMeta.shipping} when set. */
  readonly shipsTo?: readonly CountryCode[];
}
 
/**
 * Whether a supplier with the given shipping metadata ships to `location`. Prefers
 * the explicit `shipsTo` allowlist when the supplier declares one; otherwise falls
 * back to the coarse `shipping` scope — `"worldwide"`/`"international"` ship
 * anywhere, while `"domestic"`/`"local"` ship only within the supplier's own
 * `country`.
 * @param meta - The supplier's shipping/country/`shipsTo` metadata.
 * @param location - Destination country (ISO 3166-1 alpha-2).
 * @returns True when the supplier ships to `location`.
 * @example
 * ```ts
 * shipsToCountry({ shipping: 'domestic', country: 'US' }, 'DE'); // => false
 * shipsToCountry({ shipping: 'domestic', country: 'US', shipsTo: ['US', 'CA'] }, 'CA'); // => true
 * ```
 * @category Country Helpers
 * @group Suppliers
 * @source
 */
export function shipsToCountry(meta: ShippingMeta, location: CountryCode): boolean {
  if (meta.shipsTo) {
    return meta.shipsTo.includes(location);
  }
  switch (meta.shipping) {
    case 'worldwide':
    case 'international':
      return true;
    case 'domestic':
    case 'local':
      return meta.country === location;
    default:
      return true;
  }
}