collections.ts
Language TypeScript1.1 KB28 lines
/*** Parser helpers for the API's PHP-array quirks.** Type-switching collections: an empty collection arrives as `[]` (array) but a* populated one as an object keyed "1".."N". On orders.splitShipments,* `split_lines` can be BOTH an array and an ordinal-keyed object within one* response. Normalize before iterating.*/export function normalizeCollection<T = unknown>(value: unknown): T[] {if (value == null) return [];if (Array.isArray(value)) return value as T[];if (typeof value === "object") return Object.values(value as Record<string, T>);return [];}/*** Numeric-looking fields return as strings in some records and numbers in* others (legacy-row dependent). Coerce defensively; returns undefined for* empty/unparseable input rather than NaN.*/export function toNumber(value: unknown): number | undefined {if (typeof value === "number") return Number.isFinite(value) ? value : undefined;if (typeof value === "string" && value.trim() !== "") {const n = Number(value);return Number.isFinite(n) ? n : undefined;}return undefined;}