Skip to content

Instantly share code, notes, and snippets.

@mrcoles
mrcoles / airtable_escape.py
Last active Dec 26, 2021
A helper function for escaping variables passed into formulas in the Airtable API
View airtable_escape.py
from typing import Any, Optional, Union
from decimal import Decimal
class AirtableBadTypeException(Exception):
def __init__(self, val: Any):
self.val = val
super().__init__(f"unexpected type variable type: {type(val)}")
@mrcoles
mrcoles / use-intersection-observer.ts
Last active Dec 20, 2021
A simple hook for running an IntersectionObserver inside a React FunctionComponent
View use-intersection-observer.ts
import { useEffect, useRef } from 'react';
export type OnIntersect = (elt: HTMLElement) => void;
/**
* Hook for running an IntersectionObserver.
* @param onIntersect this is used in the deps of useEffect, you can
* employ useCallback to prevent it from running
* after every render
* @param args
@mrcoles
mrcoles / make_imgs.py
Created Jan 17, 2018
Convert the MNIST CSV dataset from Kaggle to png images
View make_imgs.py
#!/usr/bin/env python3
import csv
import os
import pathlib
import imageio
import numpy as np
@mrcoles
mrcoles / dynamodb-auto-paginate-scan.ts
Last active Mar 29, 2021
Snippet for doing auto-pagination of a DynamoDB scan with an async generator in NodeJS
View dynamodb-auto-paginate-scan.ts
import { DocumentClient } from "aws-sdk/lib/dynamodb/document_client";
export async function* autoPaginateScan<I extends DocumentClient.AttributeMap>(
docClient: DocumentClient,
params: DocumentClient.ScanInput
) {
while (true) {
const data = await docClient.scan(params).promise();
if (data.Items && data.Items.length) {
@mrcoles
mrcoles / console-group-decorator.d.ts
Last active Mar 22, 2021
A decorator for wrapping a function in a `console.group(msg)` -> `console.groupEnd()` call.
View console-group-decorator.d.ts
export function groupCollapsedFn<F extends Function>(
msg: any, func: F,
): F;
export function groupFn<F extends Function>(
msg: any, func: F, isCollapsed?: boolean
): F;
@mrcoles
mrcoles / aws-amplify-build-settings-cache-static-files.yml
Last active Mar 9, 2021
Sample file caching custom headers to add to an AWS Amplify build-settings.yml file
View aws-amplify-build-settings-cache-static-files.yml
frontend:
customHeaders:
# cache static assets! (can I just use a glob for this, e.g., '**/*.{js,css,gif,ico,jpg,png,svg,mp4,webm}' ?)
# js
- pattern: '**/*.js'
headers: [ { key: 'Cache-Control', value: 'public,max-age=31536000,immutable' } ]
# css
- pattern: '**/*.css'
headers: [ { key: 'Cache-Control', value: 'public,max-age=31536000,immutable' } ]
# images
@mrcoles
mrcoles / AsyncStripeProvider.js
Last active Dec 8, 2020
A react provider abstraction for loading the stripe.js file asynchronously to use with Stripe Elements
View AsyncStripeProvider.js
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import { StripeProvider } from 'react-stripe-elements';
export default class AsyncStripeProvider extends Component {
static propTypes = {
apiKey: PropTypes.string.isRequired
};
// constructor
@mrcoles
mrcoles / detect-autoplay.js
Created May 8, 2013
A script to detect browser support for the autoplay attribute on the HTML5 Audio element.
View detect-autoplay.js
// Detect autoplay
// ---------------
// This script detects whether the current browser supports the
// autoplay feature for HTML5 Audio elements, and it sets the
// `AUTOPLAY` variable accordingly.
// Used in the Meteor app [PicDinner](http://picdinner.com)
@mrcoles
mrcoles / pause-gifs.css
Created Feb 15, 2018
A lightweight approach to pausing gifs on a webpage.
View pause-gifs.css
/** .pause-gif */
.wrap-pause-gif {
position: relative;
text-align: center;
}
.pause-gif {
visibility: hidden;
}
@mrcoles
mrcoles / BlockUnload.tsx
Created Aug 24, 2020
A simple React component meant for form editing that will conditionally block page unloads and React Router navigation
View BlockUnload.tsx
import React from 'react';
import { useBeforeunload } from 'react-beforeunload';
import { Prompt } from 'react-router';
const DEFAULT_MESSAGE = 'You will lose unsaved changes if you leave this page.';
type BlockUnloadProps = {
when?: boolean;
message?: string;
};