File size: 2,163 Bytes
646b326
 
 
9f1fdb9
 
 
 
 
 
 
 
646b326
 
9f1fdb9
7da65ca
9f1fdb9
646b326
9f1fdb9
 
8fa8287
646b326
 
8fa8287
646b326
9f1fdb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
646b326
 
dc7f0d5
646b326
 
 
9f1fdb9
 
 
 
 
 
dc7f0d5
 
 
9f1fdb9
 
 
 
 
 
 
 
 
 
 
 
 
dc7f0d5
9f1fdb9
 
dc7f0d5
9f1fdb9
 
 
 
dc7f0d5
9f1fdb9
 
 
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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,
  };
}