Skip to content

Instantly share code, notes, and snippets.

Avatar
😃

David Wells DavidWells

😃
View GitHub Profile
@DavidWells
DavidWells / rescursive-lowercase.js
Created May 12, 2020
rescursively lowercase an object values
View rescursive-lowercase.js
function recursiveLowerCase(object) {
if (Array.isArray(object)) {
return object.map((value) => recursiveLowerCase(value))
}
if (object !== null && object.constructor === Object) {
return Object.keys(object).reduce((result, key) => (
Object.assign(Object.assign({}, result), {
[key]: recursiveLowerCase(object[key].toLowerCase())
})
@DavidWells
DavidWells / add-user-to-private-repo.js
Created Apr 26, 2020
Add GitHub user to private GitHub repo utility function
View add-user-to-private-repo.js
const axios = require('axios')
const GITHUB_REPO = process.env.GITHUB_REPO
const GITHUB_API_TOKEN = process.env.GITHUB_API_TOKEN
const GITHUB_USERNAME = process.env.GITHUB_USERNAME
module.exports = function addUserToGithubRepo(username) {
const githubEndpoint = `https://api.github.com/repos/${GITHUB_REPO}/collaborators/${username}`
const config = {
'headers': {
'User-Agent': GITHUB_USERNAME,
@DavidWells
DavidWells / auto-install-npm-deps.js
Created Apr 24, 2020
Automatically install deps for any serverless project found
View auto-install-npm-deps.js
const path = require('path')
const globby = require('globby')
const execSync = require('child_process').execSync
globby(['*/serverless.yml']).then(paths => {
// Now run npm install
const command = `npm install`
paths.forEach((p) => {
var t = path.join(__dirname, p)
execSync(command, {
View avoid-null-when-possible.js
function test() {
// simple return or return void 0 to return undefined
return void 0
}
var x = test()
if (!x) {
console.log('no x! great check') // good
}
@DavidWells
DavidWells / queries.md
Created Apr 1, 2020
Cloudwatch insights queries
View queries.md

Cloudwatch insights queries

Find function timeouts

Get all the functions having pesky timeouts

fields @timestamp, @message
| filter @message like 'Task timed out after'
| sort @timestamp desc
@DavidWells
DavidWells / recursively-fetch-apigateway-apis.js
Created Mar 25, 2020
Recursively fetch API gateway rest API data
View recursively-fetch-apigateway-apis.js
const AWS = require('aws-sdk')
const apiGateway = new AWS.APIGateway()
async function fetchAllApis() {
let getRestApisResult = await apiGateway.getRestApis().promise()
let apis = getRestApisResult.items
while (getRestApisResult.$response.hasNextPage()) {
getRestApisResult = await (getRestApisResult.$response.nextPage()).promise()
apis = apis.concat(getRestApisResult.items)
}
View generate-random-password.js
const CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+-={}[]:;<>?/|\\';
function generatePassword(length = 12) {
let pass = '';
for (let i = length; i > 0; --i) pass += CHARS[Math.floor(Math.random() * CHARS.length)];
return pass
}
// Usage:
generatePassword(15)
@DavidWells
DavidWells / string-to-boolean.js
Created Mar 5, 2020
Convert a string to the equivalent boolean
View string-to-boolean.js
function stringToBoolean(str) {
const string = (typeof str === 'string') ? str.toLowerCase().trim() : str
switch (string) {
case "true": case "yes": case "1": return true
case "false": case "no": case "0": case null: return false
default: return Boolean(string)
}
}
// Usage
@DavidWells
DavidWells / user-feedback.md
Created Feb 15, 2020
Don Norman quote from The Invisible Computer
View user-feedback.md

Don Norman quote from The Invisible Computer:

"Don’t ask people what they want. Watch them and figure out their needs. If you ask, people usually focus on what they have and ask for it to be better: cheaper, faster, smaller. A good observer might discover that the task is unnecessary, that it is possible to restructure things or provide a new technology that eliminates the painstaking parts of their procedures. If you just follow what people ask for, you could end up making their lives even more complicated."

View aws-resource-tags-example.yml
Tags:
- Key: Name
Value: ${self:custom.analysis-tablename}
- Key: Environment
Value: ${self:custom.stage}
- Key: Project
Value: ${self:custom.app}
- Key: Application
Value: ${self:custom.service_acronym}