This file contains hidden or 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 fs = require('fs'); | |
// Change this line to your template | |
const templateFile = './example-template.json'; | |
const cfn = JSON.parse(fs.readFileSync(templateFile)); | |
const resources = cfn.Resources; | |
const keys = Object.keys(resources); | |
let obj = {}; |
This file contains hidden or 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 colors1 = ['#111', '#222', '#333']; | |
const colors2 = ['#444', '#555', '#666']; | |
const colorChoice = (n) => { | |
return n[Math.floor(Math.random() * n.length)]; | |
}; | |
colorChoice(colors1); | |
// Returns one of: ['#111', '#222', '#333'] |
This file contains hidden or 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 colors1 = ['#111', '#222', '#333']; | |
const colors2 = ['#444', '#555', '#666']; | |
const colorChoice = (colors) => { | |
return colors[Math.floor(Math.random() * colors.length)]; | |
}; | |
colorChoice(colors1); | |
// Returns one of: ['#111', '#222', '#333'] |
This file contains hidden or 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
// Set up a promise | |
const promise = new Promise((resolve, reject) => { | |
const number = Math.floor(Math.random() * 2); | |
setTimeout(() => { | |
if (number > 0) { | |
resolve('Success!'); | |
} else { | |
reject('Failure!!!'); | |
} | |
}, 2000); |
This file contains hidden or 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
// Say you have an array of "items" that you want to iterate over... | |
const someArray = [ | |
{ | |
name: 'A', | |
number: 1 | |
}, | |
{ | |
name: 'B', | |
number: 2 | |
}, |
This file contains hidden or 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
// Set up the function, using the spread operator to structure arguments | |
var removeFromArray = function(...args) { | |
// Define the array as the first argument | |
const array = args[0] | |
// Returns a new array that does not contain any of [args] | |
return array.filter(val => !args.includes(val)) | |
} | |
// So for the following array: | |
var someArray = [1, 2, 3, 4, 5] |
This file contains hidden or 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
// "normal" way of writing (uses an anonymous function) | |
words.filter(function(word) { | |
return word.length < 6; | |
}); | |
// arrow function (uses an anonymous function) | |
words.filter(word => word.length < 6); | |
// but you don't have to use an anonymous function... | |
// filter takes a function as an argument, and you can define that separately |
This file contains hidden or 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
}).catch((e) => { | |
// Some basic error handling | |
if (e.code === 'ENOTFOUND') { | |
callback('Unable to connect to API servers'); | |
} | |
else { | |
callback(e.message); | |
} | |
}); | |
}; |
This file contains hidden or 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
SNS.publish(params, (err, data) => { | |
if (err) { | |
console.log(err.stack); | |
callback(err); | |
} | |
callback(null, data); | |
}) |
This file contains hidden or 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
// Construct a format for the message - since we're using SNS, the maximum length will be 140 chars | |
const message = `Tomorrow's weather: \nHi: ${high}\xB0\nLo: ${low}\xB0\nRain: ${precip}%\n\n${summary}`; | |
// Create an object to define how we'll invoke our call to SNS | |
const params = { | |
Message: message, | |
TopicArn: TOPIC | |
} |