| function parseStake(input) { |
| const source = String(input ?? '').trim(); |
| const currencyMatch = source.match(/^\$?\s*(\d+(?:\.\d{1,2})?)$/); |
|
|
| if (!currencyMatch) { |
| return null; |
| } |
|
|
| return Number(currencyMatch[1]); |
| } |
|
|
| function parseOdds(sourceInput) { |
| const source = String(sourceInput ?? '').trim(); |
|
|
| const americanMatch = source.match(/^([+-]\d{3,})$/); |
| if (americanMatch) { |
| return americanMatch[1]; |
| } |
|
|
| const decimalMatch = source.match(/^(\d+(?:\.\d{1,3})?)$/); |
| if (decimalMatch) { |
| const decimal = Number(decimalMatch[1]); |
| if (decimal >= 1.01) { |
| return decimalMatch[1]; |
| } |
| } |
|
|
| return null; |
| } |
|
|
| function normalizeDecimalOdds(oddsInput) { |
| if (!oddsInput) { |
| return null; |
| } |
|
|
| if (/^[+-]\d+$/.test(oddsInput)) { |
| const american = Number(oddsInput); |
| if (american > 0) { |
| return Number((1 + american / 100).toFixed(4)); |
| } |
|
|
| return Number((1 + 100 / Math.abs(american)).toFixed(4)); |
| } |
|
|
| const decimal = Number(oddsInput); |
| if (Number.isFinite(decimal) && decimal >= 1.01) { |
| return Number(decimal.toFixed(4)); |
| } |
|
|
| return null; |
| } |
|
|
| export function parseBetInput(fields) { |
| const book = String(fields.book ?? '').trim(); |
| const sport = String(fields.sport ?? '').trim(); |
| const prop = String(fields.prop ?? '').trim(); |
| const oddsInput = parseOdds(fields.odds); |
| const stake = parseStake(fields.stake); |
| const normalizedDecimalOdds = normalizeDecimalOdds(oddsInput); |
| const missingFields = []; |
|
|
| if (!book) { |
| missingFields.push('book'); |
| } |
| if (!sport) { |
| missingFields.push('sport'); |
| } |
| if (!oddsInput || !normalizedDecimalOdds) { |
| missingFields.push('odds'); |
| } |
| if (!prop) { |
| missingFields.push('prop'); |
| } |
| if (stake === null || stake <= 0) { |
| missingFields.push('stake'); |
| } |
|
|
| return { |
| ok: missingFields.length === 0, |
| missingFields, |
| bet: missingFields.length === 0 |
| ? { |
| book, |
| sport, |
| oddsInput, |
| normalizedDecimalOdds, |
| prop, |
| stake, |
| rawInput: `book: ${book}\nsport: ${sport}\nprop: ${prop}\nodds: ${oddsInput}\nstake: ${stake}`, |
| } |
| : null, |
| }; |
| } |
|
|