UNPKG

@cloudbet/market-helper

Version:

SDK to generate localized sport market information

256 lines 9.6 kB
import { getSportsName, hasMarketDefinition, isLoaded, isLoading, loadLocale, Locale, MarketType, overUnderMarket, possibleOutcomeCount, selectionCountPerLine, } from './sports-core'; import { getMarketDefinitions, getMarketDescription, getMarketName, getOutcome } from './sports-core/market-definitions'; function throwIfInvalidMarketType(marketType) { if (!marketType || !Object.values(MarketType).includes(marketType)) { throw new Error("Got " + marketType + ", please pass in one of the supported marketType (" + Object.values(MarketType) + ")"); } } function throwIfUndefined(v, name) { if (typeof v === "undefined") { throw new Error("Missing argument " + name); } } function throwIfArrayEmpty(v, name) { if (!v || v.length < 1) { throw new Error("Missing argument " + name); } } function sanitizedSubmarketKeys(marketType, submarketKeys) { var marketDefinitions = getMarketDefinitions(Locale.en); var marketDefinition = marketDefinitions[marketType]; if (!marketDefinition) { return submarketKeys; } var marketSplits = marketDefinition.Split.split("&"); return submarketKeys.reduce(function (accumulator, currentValue) { if (currentValue.includes("{{")) { return accumulator; } var splits = currentValue.split("&"); if (splits.length !== marketSplits.length) { return accumulator; } accumulator.push(currentValue); return accumulator; }, []); } function getMarket(eventApiData, marketType, marketOptions) { if (marketOptions === void 0) { marketOptions = { locale: Locale.en, }; } throwIfUndefined(eventApiData, "eventApiData"); throwIfInvalidMarketType(marketType); var returnedMarkets = []; var _a = getSubmarketsData(eventApiData, marketType), submarketsData = _a[0], error = _a[1]; if (error) { return [returnedMarkets, error]; } for (var _i = 0, _b = sanitizedSubmarketKeys(marketType, submarketsData.keys); _i < _b.length; _i++) { var submarketKey = _b[_i]; var submarket = submarketsData.data[submarketKey]; if (!submarket) { continue; } var _c = getSubmarket(marketType, eventApiData, submarket, submarketKey, marketOptions), market = _c[0], error_1 = _c[1]; if (error_1) { continue; } returnedMarkets.push(market); } return [returnedMarkets, null]; } function hasAnyMarkets(eventApiData, marketTypes, marketOptions) { if (marketOptions === void 0) { marketOptions = { locale: Locale.en, }; } throwIfUndefined(eventApiData, "eventApiData"); throwIfArrayEmpty(marketTypes, "marketTypes"); if (!(eventApiData === null || eventApiData === void 0 ? void 0 : eventApiData.markets)) { return false; } for (var _i = 0, marketTypes_1 = marketTypes; _i < marketTypes_1.length; _i++) { var marketType = marketTypes_1[_i]; if (isPrimaryMarket(marketType)) { return true; } if (!hasMarketDefinition(marketType, marketOptions.locale)) { continue; } var market = eventApiData.markets[marketType]; if (market && market.submarkets) { for (var _a = 0, _b = Object.keys(market.submarkets); _a < _b.length; _a++) { var submarketKey = _b[_a]; if (market.submarkets[submarketKey].selections.length > 0) { return true; } } } } return false; } function isBoundToPitchers(selection) { return (selection.params.includes("home_pitcher") && selection.params.includes("away_pitcher")); } function getSubmarket(marketType, eventApiData, submarket, submarketKey, marketOptions) { var _a, _b, _c; if (marketOptions === void 0) { marketOptions = { locale: Locale.en, }; } throwIfInvalidMarketType(marketType); throwIfUndefined(eventApiData, "eventApiData"); throwIfUndefined(submarket, "submarket"); throwIfUndefined(submarketKey, "submarketKey"); var errorReturnValue = { key: "", primary: false, lines: [], name: "", description: "", }; if (submarket.blank) { return [errorReturnValue, new Error("blank submarket")]; } if (!hasMarketDefinition(marketType, marketOptions.locale)) { return [ errorReturnValue, new Error("no market def and translation for " + marketType + " in locale " + marketOptions.locale), ]; } var selections = submarket.selections; if (selections.length < 1) { return [ errorReturnValue, new Error("no selections for " + marketType + " > " + submarketKey), ]; } var lineSelectionCount; var error; _a = selectionCountPerLine(marketType, submarket), lineSelectionCount = _a[0], error = _a[1]; if (error) { return [errorReturnValue, error]; } var marketName; _b = getMarketName(marketOptions.locale, submarket.selections[0], marketType, eventApiData), marketName = _b[0], error = _b[1]; if (error) { return [ errorReturnValue, new Error("unable to resolve market name for " + marketType + " > " + submarketKey + " because " + error), ]; } var marketDescription; _c = getMarketDescription(marketOptions.locale, marketType), marketDescription = _c[0], error = _c[1]; if (error) { return [errorReturnValue, error]; } var lines = []; for (var i = 0; i < selections.length; i++) { var selection = selections[i]; var _d = getOutcome(marketOptions.locale, eventApiData, marketType, selection), outcome = _d[0], error_2 = _d[1]; if (error_2) { return [ errorReturnValue, new Error("unable to resolve outcome translations for " + marketType + " > " + submarketKey + " > " + selection.outcome + " because " + error_2), ]; } if (outcome === null) { return [errorReturnValue, new Error("outcome is impossible to be null")]; } var line = []; if (i % lineSelectionCount.count === 0) { lines.push(line); } else { line = lines[lines.length - 1]; } var variables = { handicap: outcome.variables["handicap"], total: outcome.variables["total"], }; if (lineSelectionCount.hasLay) { if (i % 2 == 0) { line.push({ name: outcome.name, back: selection, variables: variables, }); } else { line[line.length - 1].lay = selection; } } else { line.push({ name: outcome.name, back: selection, variables: variables, }); } } return [ { name: marketName, primary: isPrimaryMarket(marketType), description: marketDescription, key: submarketKey, lines: lines, }, null, ]; } function getSubmarketsData(eventApiData, marketType) { throwIfInvalidMarketType(marketType); throwIfUndefined(eventApiData, "eventApiData"); var emptyReturnValue = { data: {}, keys: [], }; var markets = eventApiData.markets; if (!markets) { return [emptyReturnValue, null]; } var market = markets[marketType]; if (!market) { return [emptyReturnValue, null]; } var submarkets = market.submarkets; if (!submarkets) { return [emptyReturnValue, null]; } return [ { data: submarkets, keys: Object.keys(submarkets).sort(), }, null, ]; } function getSubmarketKeys(eventApiData, marketType, marketOptions) { if (marketOptions === void 0) { marketOptions = { locale: Locale.en, }; } if (!hasMarketDefinition(marketType, marketOptions.locale)) { return [[], null]; } var _a = getSubmarketsData(eventApiData, marketType), submarketsData = _a[0], error = _a[1]; if (error) { return [[], error]; } return [sanitizedSubmarketKeys(marketType, submarketsData.keys), null]; } function isPrimaryMarket(marketType) { var marketDefinitions = getMarketDefinitions(Locale.en); var marketDefinition = marketDefinitions[marketType]; return !!(marketDefinition === null || marketDefinition === void 0 ? void 0 : marketDefinition.Primary); } function isCashoutMarket(marketType) { var marketDefinitions = getMarketDefinitions(Locale.en); var marketDefinition = marketDefinitions[marketType]; return !!(marketDefinition === null || marketDefinition === void 0 ? void 0 : marketDefinition.CashoutAvailable); } export { getSportsName, getMarket, hasAnyMarkets, getSubmarket, getSubmarketKeys, loadLocale, possibleOutcomeCount, overUnderMarket, isPrimaryMarket, isCashoutMarket, isBoundToPitchers, isLoaded, isLoading, Locale, MarketType, }; export { getAllEventMarketsBySport, getAllOutrightMarketsBySport, getAllMarketsBySport, } from "./marketUtils"; export { getSdkSportKey } from "./sportUtils"; export { getParlayStakeLimit, getSingleStakeLimit } from "./liability"; export { getSportPeriods, getSportPeriodName, getSportPeriodDescription, } from "./sportsPeriodUtils"; //# sourceMappingURL=index.js.map