Skip to content

Instantly share code, notes, and snippets.

@Sleavely
Sleavely / promise-catch-skip.js
Created October 19, 2017 11:41
Simple showcase of how faulty catch() might behave different than you expect.
Promise.resolve(true)
.then(() => {
console.log('First then() was called!');
throw new Error('error!!!');
return true;
})
.catch((err) => {
console.log('First catch() was called');
console.log('Error: ', err);
@Sleavely
Sleavely / JsonpDetector.js
Last active April 4, 2018 11:43
A middleware for automatically converting AdonisJS responses to JSONP
'use strict'
const Config = use('Config')
/**
* JSON-P middleware for AdonisJS 4.1
* https://gist.github.com/Sleavely/da1ac50e1ea614b4171beae7868ec3ed
*/
class JsonpDetector {
async handle ({ request, response }, next) {
@Sleavely
Sleavely / move-tab-new-window.js
Created August 31, 2018 21:09
An IIFE that moves your Chrome tab to a new window. Must be run from an extension with appropriate permissions.
(() => {
const _currentTab = new Promise((resolve) => {
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
resolve(tabs[0])
})
});
const _currentWindow = new Promise((resolve) => {
chrome.windows.getCurrent((win) => resolve(win))
});
@Sleavely
Sleavely / api.js
Last active August 30, 2022 09:31
A helper for running a local webserver against lambda-api
// Require the framework and instantiate it
const api = require('lambda-api')()
// Define a route
api.get('/status', async (req, res) => {
return { status: 'ok' }
})
api.get('/README.md', async (req, res) => {
res.sendFile('./README.md')
@Sleavely
Sleavely / jira.sh
Created March 26, 2019 14:42
A bash function for quickly opening tickets in Jira
# put this in your ~/.profile or equivalent
JIRA_HOST="foobar.atlassian.net"
function jira {
# Uppercase the argument
TARGET_TICKET=${TARGET_TICKET^^}
# Inject dash if its not there
TARGET_TICKET=$(echo ${1} | sed -E --expression='s/([A-Z]+)([0-9]+)/\1-\2/g')
@Sleavely
Sleavely / download-file.js
Last active August 31, 2022 16:45 — forked from javilobo8/download-file.js
Download files from Lambda with AWS Amplify
await API.get('myCloudApi', '/items', {
responseType: 'blob',
response: true
})
.then((response) => {
const blob = new Blob([response.data], { type: 'application/octet-stream' })
const filename = response.headers['content-disposition'].split('"')[1]
if (typeof window.navigator.msSaveBlob !== 'undefined') {
import React, { useEffect, useState } from 'react'
import { CartProvider } from "use-cart"
import { LoadCart, SaveCart } from "./cartStorage"
function App() {
return (
<CartProvider initialCart={LoadCart()}>
<SaveCart />
<RestOfApp> ... </RestOfApp>
@Sleavely
Sleavely / lodash-get.js
Created October 7, 2019 13:33
lodash.get alternative that covers _most_ cases.
// Graciously stolen from lodash's stringToPath
const charCodeOfDot = '.'.charCodeAt(0)
const reEscapeChar = /\\(\\)?/g
const rePropName = RegExp(
// Match anything that isn't a dot or bracket.
'[^.[\\]]+' + '|' +
// Or match property names within brackets.
'\\[(?:' +
// Match a non-string expression.
'([^"\'][^[]*)' + '|' +
@Sleavely
Sleavely / process.env.js
Last active March 22, 2020 14:14
AWS Lambda environment variables when running with a default configuration
// The original values have been redacted, but their general format remains for display purposes.
{
AWS_ACCESS_KEY_ID: 'ASIA5USITABHEEWHMCOL',
AWS_DEFAULT_REGION: 'eu-west-1',
AWS_EXECUTION_ENV: 'AWS_Lambda_nodejs12.x',
AWS_LAMBDA_FUNCTION_MEMORY_SIZE: '128',
AWS_LAMBDA_FUNCTION_NAME: 'tmp1337',
AWS_LAMBDA_FUNCTION_VERSION: '$LATEST',
AWS_LAMBDA_LOG_GROUP_NAME: '/aws/lambda/tmp1337',
AWS_LAMBDA_LOG_STREAM_NAME: '2020/01/14/[$LATEST]425d2a3419de4db38f86fc1896cc1cc1',
@Sleavely
Sleavely / attributeResolution.js
Created March 22, 2020 13:55
Product attribute schema POC
const attributes = {
// Attribute Keys are always localized
'color': {
name: {
'en-US': 'Color',
'sv-SE': 'Färg',
},
},
'size': {