UNPKG

@voiceflow/common

Version:

Junk drawer of utility functions

48 lines (47 loc) 2.38 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.mapSlotAnnotations = exports.getValueWithSynonyms = exports.getAllSamples = exports.getUniqueSamples = exports.addPrebuiltEntities = void 0; const uniqBy_js_1 = __importDefault(require("lodash/uniqBy.js")); const constants_1 = require("../constants"); const addPrebuiltEntities = (entities, prebuiltEntities) => entities.map((entity) => { if (prebuiltEntities[entity.key]) { return { ...entity, inputs: [...entity.inputs, ...prebuiltEntities[entity.key]], }; } return entity; }); exports.addPrebuiltEntities = addPrebuiltEntities; const getUniqueSamples = (input) => (0, uniqBy_js_1.default)(input.split(','), (sample) => sample.toLowerCase()); exports.getUniqueSamples = getUniqueSamples; // spread all synonyms into string array ['car, automobile', 'plane, jet'] => ['car', 'automobile', 'plane', 'jet'] const getAllSamples = (inputs = []) => inputs.flatMap((input) => input.split(',')).filter((sample) => !!sample.trim()); exports.getAllSamples = getAllSamples; /** * Return a tuple of synonyms, the first value being the first synonym, the next being the remaining synonyms */ const getValueWithSynonyms = (input) => { const [value, ...synonyms] = input.split(',').map((str) => str.trim()); return [value, synonyms]; }; exports.getValueWithSynonyms = getValueWithSynonyms; /** * Map through all slot annotations in the given string input. * For each annotation, the callbackFn will be called with the slot's key and name, returning a key and name. * @param input String with slot annotations. * @param callbackFn Map function called with the key and name of the slot. * @returns Input with mapped slot annotations * @example const result = mapSlotAnnotations("Hello {{[slot].id}}", ({key, name}) => ({key: key + '2', slot: slot + '2'}); * result === "Hello {{[slot2].id2}}" */ const mapSlotAnnotations = (input, callbackFn) => { return input.replace(constants_1.SLOT_REGEXP, (_, slotName, slotKey) => { const { key, name } = callbackFn({ key: slotKey, name: slotName }); return `{{[${name}].${key}}}`; }); }; exports.mapSlotAnnotations = mapSlotAnnotations;