Skip to content

Instantly share code, notes, and snippets.

View pbzona's full-sized avatar
🤠
having fun on the computer

Phil Z pbzona

🤠
having fun on the computer
View GitHub Profile
@pbzona
pbzona / organize.js
Created August 14, 2018 21:15
Organize list of resources in cfn template
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 = {};
@pbzona
pbzona / colors.js
Created July 20, 2018 17:46
colors 2
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']
@pbzona
pbzona / colors.js
Created July 20, 2018 17:39
color randomizer
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']
@pbzona
pbzona / index.js
Created July 2, 2018 19:25
Alternate Promise Syntax
// 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);
@pbzona
pbzona / index.js
Created June 8, 2018 17:57
array thing
// Say you have an array of "items" that you want to iterate over...
const someArray = [
{
name: 'A',
number: 1
},
{
name: 'B',
number: 2
},
@pbzona
pbzona / something.js
Last active May 25, 2018 02:36
why this thing works
// 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]
@pbzona
pbzona / example.js
Created May 24, 2018 13:14
anonymous functions example
// "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
@pbzona
pbzona / index.js
Created April 30, 2018 22:40
Serverless Weather pt 6
}).catch((e) => {
// Some basic error handling
if (e.code === 'ENOTFOUND') {
callback('Unable to connect to API servers');
}
else {
callback(e.message);
}
});
};
@pbzona
pbzona / index.js
Created April 30, 2018 22:38
Serverless Weather pt 5
SNS.publish(params, (err, data) => {
if (err) {
console.log(err.stack);
callback(err);
}
callback(null, data);
})
@pbzona
pbzona / index.js
Created April 30, 2018 22:28
Serverless Weather pt 4
// 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
}