UNPKG

@cloudbet/market-helper

Version:

SDK to generate localized sport market information

292 lines 12.3 kB
import { isDynamicOutcome, outcomeTranslationLookupKey, parseVariablesFromParams } from './helpers'; import { getMarketDefinitions, getSportsLocale } from './locales'; import { Locale } from './sports-core-types'; var eventDataRequiredErr = new Error('eventData is required for this market name'); var aliasMap = { inningnr: 'innings', }; function applyEventDataToTemplate(template, eventData, enableAbbreviatedTeamName) { var _a, _b, _c, _d; var homeName = 'home'; var awayName = 'away'; if (eventData) { homeName = (enableAbbreviatedTeamName ? (_a = eventData.home) === null || _a === void 0 ? void 0 : _a.abbreviation : (_b = eventData.home) === null || _b === void 0 ? void 0 : _b.name) || 'home'; awayName = (enableAbbreviatedTeamName ? (_c = eventData.away) === null || _c === void 0 ? void 0 : _c.abbreviation : (_d = eventData.away) === null || _d === void 0 ? void 0 : _d.name) || 'away'; } var homeRegex = /{{home}}/g; var awayRegex = /{{away}}/g; var nameRegex = /{{name}}/g; if (homeRegex.test(template)) { template = template.replace(homeRegex, homeName); } if (awayRegex.test(template)) { template = template.replace(awayRegex, awayName); } // Replace the name field with event name var eventName = eventData === null || eventData === void 0 ? void 0 : eventData.name; if (nameRegex.test(template) && eventName) { template = template.replace(nameRegex, eventName); } return template; } function handicapValueDisplay(key, sbvHandicapValue) { var _a; var hdp = Number(sbvHandicapValue); if (isNaN(hdp)) { return [ {}, new Error("Please make sure the handicap value " + sbvHandicapValue + " is a number"), ]; } var flippedValue = hdp * -1; return [ (_a = {}, _a["{{" + key + "}}"] = "" + (hdp > 0 ? '+' : '') + hdp, _a["{{-" + key + "}}"] = "" + (flippedValue > 0 ? '+' : '') + flippedValue, _a), null, ]; } function applyTemplateVariables(template, marketVariables, eventData, enableAbbreviatedTeamName) { var _a, _b; if (!template) { return ['', new Error('missing function arg: template')]; } var homeName = ''; var awayName = ''; var marketName = ''; //Specific to outrights if (eventData) { homeName = (_a = eventData.home) === null || _a === void 0 ? void 0 : _a.name; awayName = (_b = eventData.away) === null || _b === void 0 ? void 0 : _b.name; marketName = (eventData === null || eventData === void 0 ? void 0 : eventData.name) || ''; } for (var _i = 0, _c = Object.keys(marketVariables); _i < _c.length; _i++) { var key = _c[_i]; var v = marketVariables[key]; if (!v) { continue; } if (v === 'home') { if (!homeName) { return ['', eventDataRequiredErr]; } v = homeName; } else if (v === 'away') { if (!awayName) { return ['', eventDataRequiredErr]; } v = awayName; } else if (v === 'market_name') { if (!marketName) { return ['', eventDataRequiredErr]; } v = marketName; } // This is needed to handle both innings and inngingnr if (aliasMap[key]) { key = aliasMap[key]; } if (key === 'handicap') { var _d = handicapValueDisplay(key, v), hdp = _d[0], err = _d[1]; if (err) { return ['', err]; } template = template.replace(new RegExp("{{-" + key + "}}", 'g'), hdp["{{-" + key + "}}"]); template = template.replace(new RegExp("{{" + key + "}}", 'g'), hdp["{{" + key + "}}"]); } else if ((key === 'player' || key === 'server') && eventData) { if (eventData.players) { var player = eventData.players[v]; if (player) { template = template.replace(new RegExp("{{" + key + "}}", 'g'), player.name); } } } else { template = template.replace(new RegExp("{{" + key + "}}", 'g'), v); } } template = applyEventDataToTemplate(template, eventData, enableAbbreviatedTeamName); if (template.match(/{{.*}}/)) { return [ '', new Error("market name " + template + " still have tempalate variables"), ]; } return [template, null]; } function applyOutcomeVariables(template, outcomes, eventData, enableAbbreviatedTeamName) { var _a, _b, _c, _d, _e, _f; var outcomeVariables = outcomes; var homeName = 'home'; var awayName = 'away'; if (eventData) { homeName = (enableAbbreviatedTeamName ? (_a = eventData.home) === null || _a === void 0 ? void 0 : _a.abbreviation : (_b = eventData.home) === null || _b === void 0 ? void 0 : _b.name) || ((_c = eventData.home) === null || _c === void 0 ? void 0 : _c.name); awayName = (enableAbbreviatedTeamName ? (_d = eventData.away) === null || _d === void 0 ? void 0 : _d.abbreviation : (_e = eventData.away) === null || _e === void 0 ? void 0 : _e.name) || ((_f = eventData.away) === null || _f === void 0 ? void 0 : _f.name); } for (var key in outcomes) { switch (key) { case 'handicap': var _g = handicapValueDisplay(key, outcomeVariables[key]), hdp = _g[0], err = _g[1]; if (err) { return [outcomeVariables, err]; } if (template.includes("{{" + key + "}}")) { outcomeVariables[key] = hdp["{{" + key + "}}"]; } else if (template.includes("{{-" + key + "}}")) { outcomeVariables[key] = hdp["{{-" + key + "}}"]; } break; case 'home': if (!homeName) { return [ outcomeVariables, new Error(template + " requires home name"), ]; } outcomeVariables['team'] = homeName; break; case 'away': if (!awayName) { return [ outcomeVariables, new Error(template + " requires away name"), ]; } outcomeVariables['team'] = awayName; break; case 'home_pitcher': case 'away_pitcher': { if (!eventData || !eventData.players) { return [ outcomeVariables, new Error(template + " requires player data"), ]; } var name_1 = (eventData.players[outcomeVariables[key]] || {}).name; if (!name_1) { name_1 = outcomeVariables[key] .replace(/-/g, ' ') .replace(/\w\S*/g, function (w) { return w.replace(/^\w/, function (c) { return c.toUpperCase(); }); }); } outcomeVariables[key] = name_1; break; } default: outcomes[key] = applyEventDataToTemplate(outcomes[key], eventData, enableAbbreviatedTeamName); break; } if (outcomeVariables[key].length == 0) { delete outcomeVariables[key]; } } return [outcomeVariables, null]; } function getMarketDescription(locale, marketType) { var _a; var marketDefinition = getMarketDefinitions(locale); var translation = (_a = marketDefinition[marketType]) === null || _a === void 0 ? void 0 : _a.Description; if (!translation) { translation = ''; } return [translation, null]; } function getMarketName(locale, selection, marketType, eventData) { var _a; if (!selection) { return ['', new Error('no selection in the submarket')]; } if (!!((_a = marketType === null || marketType === void 0 ? void 0 : marketType.match(/.outright.v3/g)) === null || _a === void 0 ? void 0 : _a[0]) && (eventData === null || eventData === void 0 ? void 0 : eventData.name)) { return [eventData === null || eventData === void 0 ? void 0 : eventData.name, null]; } var _b = parseVariablesFromParams(selection.params), variables = _b[0], error = _b[1]; if (error) { return ['', error]; } var translations = getMarketDefinitions(locale); if (!translations[marketType]) { return ['', new Error("missing translation for market " + marketType)]; } return applyTemplateVariables(translations[marketType].Name, variables, eventData); } function hasMarketDefinition(marketType, locale) { var marketDefinitions = getMarketDefinitions(locale); if (!marketDefinitions) { return false; } return !!marketDefinitions[marketType]; } function getOutcome(locale, eventData, marketType, selection, enableAbbreviatedTeamName) { var _a, _b, _c; if (!hasMarketDefinition(marketType, locale)) { return [ null, new Error("missing market definition for market " + marketType + " in locale " + locale), ]; } var marketDefinitions = getMarketDefinitions(locale); var marketDefinition = marketDefinitions[marketType]; var dynamic = isDynamicOutcome(marketType, marketDefinition); if (!selection) { return [null, new Error('no selection in the submarket')]; } var _d = parseVariablesFromParams(selection.outcome + "&" + selection.params), variables = _d[0], error = _d[1]; if (error) { return [null, error]; } var template = marketDefinitions[marketType].Outcomes[outcomeTranslationLookupKey(selection.outcome, dynamic)] || marketDefinitions[marketType].Outcomes[outcomeTranslationLookupKey(selection.outcome, false)]; // Fallback to check if there is a template that is not dynamic as the isDynamicOutcome will be true if there is one dynamic outcome in the definition // handle outright v3, it has no template or definition from the core // Only apply the no template logic here for outright v3, because for other market we want to keep the failing logics if its not found if (!!((_a = marketType === null || marketType === void 0 ? void 0 : marketType.match(/.outright.v3/g)) === null || _a === void 0 ? void 0 : _a[0]) && !template) { return [ { name: ((_b = eventData === null || eventData === void 0 ? void 0 : eventData.names) === null || _b === void 0 ? void 0 : _b[selection.outcome]) || selection.outcome, variables: {}, }, null, ]; } // Only try to get the name by the outcome when there is no template if (!template) { var selectionOutcomeName = (_c = eventData === null || eventData === void 0 ? void 0 : eventData.names) === null || _c === void 0 ? void 0 : _c[selection.outcome]; if (selectionOutcomeName) { return [ { name: selectionOutcomeName, variables: {}, }, null, ]; } } var _e = applyTemplateVariables(template, variables, eventData, enableAbbreviatedTeamName), name = _e[0], err = _e[1]; if (err) { return [null, err]; } var outcomeVariables = applyOutcomeVariables(template, variables, eventData, enableAbbreviatedTeamName)[0]; return [ { name: name, variables: outcomeVariables, }, null, ]; } function getSportsName(sportsKey, locale) { sportsKey = sportsKey.trim().replace(/\s/g, '_').replace(/-/g, '_'); return (getSportsLocale(locale)[sportsKey] || getSportsLocale(Locale.en)[sportsKey]); } export { getSportsName, getMarketName, getOutcome, getMarketDescription, getMarketDefinitions, hasMarketDefinition, }; //# sourceMappingURL=market-definitions.js.map