UNPKG

@voiceflow/alexa-types

Version:
40 lines (39 loc) 2.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getInvocationNameError = void 0; const common_1 = require("@voiceflow/common"); const WAKE_WORDS = ['Alexa', 'Amazon', 'Echo', 'Skill', 'App']; const LAUNCH_PHRASES = ['launch', 'ask', 'tell', 'load', 'begin', 'enable']; const NON_LATIN_REGIONS = new Set(['ja-JP', 'hi-IN']); const matchesKeyword = (splitName) => (keyword) => splitName.find((split) => split === keyword.toLowerCase()); const getInvocationNameError = (name, locales = []) => { if (!name?.trim()) { return 'Invocation name required for Alexa'; } let invalidLocales = locales.filter((locale) => !NON_LATIN_REGIONS.has(locale)).join(','); let error = `[${invalidLocales}] Invocation name may only contain Latin characters, apostrophes, periods and spaces`; let characters = common_1.VALID_LATIN_CHARACTER; if (locales.length === 1 && NON_LATIN_REGIONS.has(locales[0])) { error = 'Invocation name may only contain language characters, apostrophes, periods and spaces'; characters = common_1.VALID_SPOKEN_CHARACTER; } else if (locales.some((locale) => locale.includes('en'))) { invalidLocales = locales.filter((locale) => locale.includes('en')).join(','); error = `[${invalidLocales}] Invocation name may only contain alphabetic characters, apostrophes, periods and spaces`; characters = common_1.VALID_CHARACTER; } const validRegex = `[^${characters}.' ]+`; const match = name.match(validRegex); const splitName = name.split(' ').map((splits) => splits.toLowerCase()); if (match) { return `${error} - Invalid Characters: "${match.join()}"`; } if (WAKE_WORDS.some(matchesKeyword(splitName))) { return `Invocation name cannot contain Alexa keywords e.g. ${WAKE_WORDS.join(', ')}`; } if (LAUNCH_PHRASES.some(matchesKeyword(splitName))) { return `Invocation name cannot contain Launch Phrases e.g. ${LAUNCH_PHRASES.join(', ')}`; } return null; }; exports.getInvocationNameError = getInvocationNameError;