UNPKG

@cloudbet/market-helper

Version:

SDK to generate localized sport market information

110 lines 3.75 kB
import { getMarketDefinitions } from './market-definitions'; import { Locale } from './sports-core-types'; var dynamicOutcomeMarket = {}; function isDynamicOutcome(marketType, marketDefinition) { var cache = dynamicOutcomeMarket[marketType]; if (typeof cache !== 'undefined') { return cache; } var dynamic = Object.keys(marketDefinition.Outcomes).some(function (outcome) { return outcome.includes('dynamic.'); }); dynamicOutcomeMarket[marketType] = dynamic; return dynamic; } function hasLay(submarket) { return submarket.selections.some(function (sel) { return !!sel.side && sel.side.toLowerCase() === 'lay'; }); } function possibleOutcomeCount(marketType) { var marketDefinitions = getMarketDefinitions(Locale.en); var marketDefinition = marketDefinitions[marketType]; if (!marketDefinition) { return [0, new Error("unknown market type " + marketType)]; } if (isDynamicOutcome(marketType, marketDefinition)) { return [ 0, new Error("undefined number of possible outcomes for dynamic market " + marketType), ]; } return [Object.keys(marketDefinition.Outcomes).length, null]; } function selectionCountPerLine(marketType, submarket) { var marketDefinitions = getMarketDefinitions(Locale.en); var marketDefinition = marketDefinitions[marketType]; var falsyReturnValue = { count: 0, hasLay: false, }; if (!marketDefinition) { return [falsyReturnValue, new Error("unknown market type " + marketType)]; } var lay = hasLay(submarket); if (isDynamicOutcome(marketType, marketDefinition)) { return [ { count: submarket.selections.length, hasLay: lay, }, null, ]; } var outcomes = Object.keys(marketDefinition.Outcomes); if (outcomes.length < 1) { return [ falsyReturnValue, new Error('market definition is missing outcomes'), ]; } var count = outcomes.length; if (lay) { count *= 2; } return [ { count: count, hasLay: lay, }, null, ]; } function outcomeTranslationLookupKey(outcome, dynamicOutcome) { var outcomeTranslationKey = outcome .split('&') .map(function (param) { var index = param.indexOf('='); return index > -1 ? param.substring(0, index) : param; }) .join('_'); if (dynamicOutcome) { outcomeTranslationKey = "dynamic." + outcomeTranslationKey; } return outcomeTranslationKey; } function parseVariablesFromParams(params) { return [ params .split('&') .reduce(function (accumulator, value) { var _a; var _b = value.split('='), k = _b[0], v = _b[1]; if (!v && k && ((_a = params.split('&')) === null || _a === void 0 ? void 0 : _a.length) === 1) { accumulator['outcome'] = decodeURIComponent(k); } else { accumulator[k] = v ? decodeURIComponent(v) : ''; } return accumulator; }, {}), null, ]; } function overUnderMarket(marketType) { var marketDefinitions = getMarketDefinitions(Locale.en); var marketDefinition = marketDefinitions[marketType]; var outcomes = Object.keys(marketDefinition.Outcomes); if (outcomes.length !== 2) { return false; } return outcomes.includes('over') && outcomes.includes('under'); } export { selectionCountPerLine, isDynamicOutcome, parseVariablesFromParams, outcomeTranslationLookupKey, possibleOutcomeCount, overUnderMarket, }; //# sourceMappingURL=helpers.js.map