Created
October 23, 2020 07:48
-
-
Save eduardocalazansjr/eac6ae4ae52c1c3c436ccb94770e932e to your computer and use it in GitHub Desktop.
js-to-bricks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const parsePayload = (payload, intentName) => { | |
let commands | |
const actions = [] as any | |
const conditionalSplit = payload.split("}} ").map(r => r.trim()).filter(r => r !== "") | |
if (conditionalSplit.length == 1) { | |
commands = payload.split(";").map(r => r.trim()).filter(r => r !== "") | |
commands.forEach(cmd => actions.push(handleCommand(cmd, intentName))) | |
} else if (conditionalSplit.length > 1) { | |
conditionalSplit.forEach(c => { | |
if (c.startsWith("if") || c.startsWith("else if")) { | |
const action = handleCondition(c, intentName) | |
if (action) actions.push(action) | |
} else { | |
if (c.split(" if ").length > 1) { | |
commands = c.split(" if ")[0].split(";").map(r => r.trim()).filter(r => r !== "") | |
commands.forEach(cmd => actions.push(handleCommand(cmd, intentName))) | |
const action = handleCondition(`if ${c.split(" if ")[1]}`, intentName) | |
if (action) actions.push(action) | |
} else if (c.split(";").length > 0) { | |
commands = c.split(";").map(r => r.trim()).filter(r => r !== "") | |
commands.forEach(cmd => actions.push(handleCommand(cmd, intentName))) | |
} else { | |
console.log("WTF") | |
console.log(c) | |
} | |
} | |
}) | |
} | |
return actions | |
} | |
const handleCommand = (command, intentName) => { | |
let insiders | |
try { | |
switch (true) { | |
case command.startsWith("setVar"): | |
insiders = getRawCommand(command).map(q => q.replace(/"/g, "")) | |
return { | |
classifier: "vars", | |
type: insiders[1] == "user.queryInput" || insiders[1] == `$${intentName}` ? "user-input" : "variable", | |
value: insiders[1], | |
varname: insiders[0] | |
} | |
case command.startsWith("msg"): | |
insiders = getRawCommand(command).map(q => q.replace(/"/g, "")) | |
return { | |
classifier: "msg", | |
type: insiders[1], | |
value: insiders[0] | |
} | |
case command.startsWith("event"): | |
insiders = command.match(/\((.*)\)/).pop().replace(/"/g, "") | |
return { | |
classifier: "event", | |
value: insiders | |
} | |
} | |
} catch (err) { | |
console.log(err) | |
return undefined | |
} | |
} | |
const handleCondition = (command, intentName) => { | |
try { | |
let insiders = command.match(/\(([^()]+)\)/g)[0].replace(/"/g, "").replace(/\(/g, "").replace(/\)/g, "").split(" == ") | |
let action = { | |
classifier: "if", | |
type: insiders[0] == `$${intentName}` ? "user-input" : "variable", | |
value: insiders[1], | |
variable: insiders[0], | |
actions: [] as any | |
} | |
let commands = command.split(" {{ ")[1].split(";").map(r => r.trim()).filter(r => r !== "") | |
commands.forEach(cmd => { if (handleCommand(cmd, intentName)) action.actions.push(handleCommand(cmd, intentName)) }) | |
return action | |
} catch (err) { | |
console.log(err) | |
return undefined | |
} | |
} | |
const getRawCommand = (command) => { | |
let rawCommand = command.match(/\((.*)\)/).pop().split("\", \"") | |
if (rawCommand.length <= 1) { | |
rawCommand = command.match(/\((.*)\)/).pop().split("`, \"") | |
if (rawCommand[0]) rawCommand[0] = rawCommand[0] + "`" | |
} | |
if (rawCommand.length <= 1) { | |
rawCommand = command.match(/\((.*)\)/).pop().split("`, `") | |
if (rawCommand[0]) rawCommand[0] = rawCommand[0] + "`" | |
if (rawCommand[1]) rawCommand[1] = "`" + rawCommand[1] | |
} | |
return rawCommand.length <= 1 ? null : rawCommand | |
} | |
export default parsePayload |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment