mattrguerra/meridian-orders-mcpmainPrivate on GitHub, published here in full

collections.ts

Language TypeScript1.1 KB28 lines

mcp-server/src/lib/collections.ts

  1. /**
  2. * Parser helpers for the API's PHP-array quirks.
  3. *
  4. * Type-switching collections: an empty collection arrives as `[]` (array) but a
  5. * populated one as an object keyed "1".."N". On orders.splitShipments,
  6. * `split_lines` can be BOTH an array and an ordinal-keyed object within one
  7. * response. Normalize before iterating.
  8. */
  9. export function normalizeCollection<T = unknown>(value: unknown): T[] {
  10. if (value == null) return [];
  11. if (Array.isArray(value)) return value as T[];
  12. if (typeof value === "object") return Object.values(value as Record<string, T>);
  13. return [];
  14. }
  15. /**
  16. * Numeric-looking fields return as strings in some records and numbers in
  17. * others (legacy-row dependent). Coerce defensively; returns undefined for
  18. * empty/unparseable input rather than NaN.
  19. */
  20. export function toNumber(value: unknown): number | undefined {
  21. if (typeof value === "number") return Number.isFinite(value) ? value : undefined;
  22. if (typeof value === "string" && value.trim() !== "") {
  23. const n = Number(value);
  24. return Number.isFinite(n) ? n : undefined;
  25. }
  26. return undefined;
  27. }