UNPKG

@voiceflow/google-types

Version:
40 lines (39 loc) 2.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getInvocationNameError = void 0; const common_1 = require("@voiceflow/common"); const RESERVED_WORDS = ['ok', 'google', 'launch', 'ask', 'tell', 'load', 'game', 'action', 'assistant', 'skill', 'app']; const RESERVED_PHRASES = ['exit quit', 'volume up']; 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 Google'; } 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.toLowerCase().split(' '); if (match) { return `${error} - Invalid Characters: "${match.join()}"`; } if (RESERVED_WORDS.some(matchesKeyword(splitName))) { return `Invocation name cannot contain reserved words e.g. ${RESERVED_WORDS.join(', ')}`; } if (RESERVED_PHRASES.some((phrase) => name.match(RegExp(`\b${phrase}\b`)))) { return `Invocation name cannot contain reserved phrases e.g. ${RESERVED_PHRASES.join(', ')}`; } return null; }; exports.getInvocationNameError = getInvocationNameError;