* @param {unknown} rawAwContext * @returns {{ item_type: string, item_number: number | null } | null}
(rawAwContext)
| 54 | * @returns {{ item_type: string, item_number: number | null } | null} |
| 55 | */ |
| 56 | function parseAwContext(rawAwContext) { |
| 57 | /** |
| 58 | * @param {unknown} parsed |
| 59 | * @returns {{ item_type: string, item_number: number | null } | null} |
| 60 | */ |
| 61 | function validateAndNormalizeParsedContext(parsed) { |
| 62 | if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) { |
| 63 | return null; |
| 64 | } |
| 65 | const parsedObj = /** @type {Record<string, unknown>} */ parsed; |
| 66 | const itemTypeValue = parsedObj["item_type"]; |
| 67 | const itemNumberValue = parsedObj["item_number"]; |
| 68 | const itemType = typeof itemTypeValue === "string" ? itemTypeValue : ""; |
| 69 | const itemNumber = parsePositiveInteger(itemNumberValue); |
| 70 | return { item_type: itemType, item_number: itemNumber }; |
| 71 | } |
| 72 | |
| 73 | if (rawAwContext == null) { |
| 74 | return null; |
| 75 | } |
| 76 | if (typeof rawAwContext === "string") { |
| 77 | const trimmed = rawAwContext.trim(); |
| 78 | if (!trimmed) { |
| 79 | return null; |
| 80 | } |
| 81 | try { |
| 82 | const parsed = JSON.parse(trimmed); |
| 83 | return validateAndNormalizeParsedContext(parsed); |
| 84 | } catch { |
| 85 | return null; |
| 86 | } |
| 87 | } |
| 88 | return validateAndNormalizeParsedContext(rawAwContext); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Parses a value into a positive integer. |
no test coverage detected