Destructuring Example
Destructuring allows binding using pattern matching, with support for matching arrays and objects. Destructuring is fail-soft, similar to standard object lookup foo["bar"]
, producing undefined
values when not found.
Reference Links
Simple Example
const user = {
firstName : "Steve",
lastName : "Tingiris",
email : "steve@dabblelab.com"
}
const {date = new Date, firstName : name, lastName, email} = user;
function fullName({firstName, lastName}) {
return `${firstName} ${lastName}`;
}
console.log(date); //current date
console.log(name); //Steve
console.log(lastName); //Tingiris
console.log(email); //steve@dabblelab.com
console.log(fullName(user)); //Steve Tingiris
Expanded Example
const axios = require('axios');
const App = {
Data:{
Urls: {
PROVIDER_NAME: "https://twilio-functions-1342-dev.twil.io/search-airtable?tableName=Campaign&phone_number=" + "2223334444" + "&variableName=phone_number",
SERVICE_PROVIDERS: 'https://cfservavailwebapi.cfdomains.com/api/search/1/'
},
},
Controllers: {
client: null,
remember_send_to_agent: { remember: { send_to_agent: true } },
promotion_response_success: { say: "These are the promotions in your area" },
promotion_response_fail: { say: "I'm sorry, you don't qualify" },
},
Methods: {
init: function({client}) {
App.Controllers.client = client;
},
getMessage: function(MessageSid){
return App.Controllers.client.messages(MessageSid).fetch();
},
},
Requests: {
getProviders: function() {
// TODO actual request
return {
'3333333333': { provider_name: 'earthlink' },
'4444444444': { provider_name: 'att' },
};
},
getProviderName: function(){
return axios.get(App.Data.Urls.PROVIDER_NAME);
},
getServiceProviders: function(zipName){
return axios.get(App.Data.Urls.SERVICE_PROVIDERS + zipName);
}
},
}
exports.handler = async(context, event, callback) => {
console.log('hola');
for(const elem in event)
console.log(`EVENT: ${elem} : ${event[elem]}`);
const actions = [];
App.Methods.init({client: context.getTwilioClient()});
const { twilio: { collected_data, chat: { MessageSid } } } = JSON.parse(event.Memory);
const { collect_address: { answers: { zip: { answer } } } } = collected_data;
if(!answer) callback('Not enough data');
try {
const { data: { provider_name } } = await App.Requests.getProviderName();
const { data: { providers } } = await App.Requests.getServiceProviders(answer);
const serviceProviders = providers.filter( provider => provider.providerName === provider_name );
if(serviceProviders.length > 0){
const { promotion_response_success, remember_send_to_agent } = App.Controllers;
promotion_response_success.say = `Congratulations! I see that your address is eligible for service with ${serviceProviders[0].providerName}. Let me connect you with a sales expert.`;
actions.push(remember_send_to_agent);
actions.push(promotion_response_success);
}
else{
//TODO: need api call to get a tracking number to tie the spillover call to a chat session
const { promotion_response_fail_say } = App.Controllers;
actions.push(promotion_response_fail_say);
}
const response = { actions };
callback(null, response);
} catch (error) {
console.log(error);
callback(error);
}
}