Skip to content

Instantly share code, notes, and snippets.

View micahjon's full-sized avatar

Micah Engle-Eshleman micahjon

View GitHub Profile
@TonyGravagno
TonyGravagno / zodDefaultInstance.ts
Last active March 21, 2024 09:31
Create a default object from a Zod schema
import { z } from 'zod'
/**
* @summary Function returns default object from Zod schema
* @version 23.05.15.2
* @link https://gist.github.com/TonyGravagno/2b744ceb99e415c4b53e8b35b309c29c
* @author Jacob Weisenburger, Josh Andromidas, Thomas Moiluiavon, Tony Gravagno
* @param schema z.object schema definition
* @param options Optional object, see Example for details
* @returns Object of type schema with defaults for all fields
@pesterhazy
pesterhazy / indexeddb-problems.md
Last active April 19, 2024 02:40
The pain and anguish of using IndexedDB: problems, bugs and oddities

This gist lists challenges you run into when building offline-first applications based on IndexedDB, including open-source libraries like Firebase, pouchdb and AWS amplify (more).

Note that some of the following issues affect only Safari. Out of the major browsers, Chrome's IndexedDB implementation is the best.

Backing file on disk (WAL file) keeps growing (Safari)

When this bug occurs, every time you use the indexeddb, the WAL file grows. Garbage collection doesn't seem to be working, so after a while, you end up with gigabytes of data.

Random exceptions when working with a large number of indexeddb databases (Safari)

@dukejones
dukejones / cloudflare-worker-rollbar.ts
Created March 13, 2020 05:56
Log errors and messages to Rollbar from a Cloudflare Worker
import ErrorStackParser from 'error-stack-parser'
const ROLLBAR_ACCESS_TOKEN = '[ACCESS TOKEN]'
var rollbarUrl = `https://api.rollbar.com/api/1/item/`
// https://github.com/rollbar/rollbar.js/blob/master/src/errorParser.js
function Frame(stackFrame: ErrorStackParser.StackFrame) {
var data:any = {};
// data._stackFrame = stackFrame;
@rviscomi
rviscomi / CrashCourseInCrUX.md
Last active May 7, 2019 10:52
"Crash Course in CrUX" for the PerfMatters conference, April 2019
@vktr
vktr / rule.js
Created February 10, 2018 18:54
Add Stripe Customer Id to Auth0 via custom rule
function (user, context, callback) {
user.app_metadata = user.app_metadata || {};
if ('stripe_customer_id' in user.app_metadata) {
context.idToken['https://example.com/stripe_customer_id'] = user.app_metadata.stripe_customer_id;
return callback(null, user, context);
}
var stripe = require('stripe')('sk_....');
var customer = {
@maxmilton
maxmilton / asynchronous-sentry-js-error-tracking.html
Last active April 27, 2020 15:43
How to load the Sentry JavaScript error tracking script 'raven.js' asynchronously and still capture errors before the script is loaded.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Asynchronous Sentry JavaScript Error Tracking</title>
<!-- putting your CSS near the top is always a good idea -->
<link rel="stylesheet" href="/app.css"></link>
@timdown
timdown / trim_canvas.js
Created July 26, 2017 16:15
Returns a copy of a canvas element with surrounding transparent space removed
var trimCanvas = (function() {
function rowBlank(imageData, width, y) {
for (var x = 0; x < width; ++x) {
if (imageData.data[y * width * 4 + x * 4 + 3] !== 0) return false;
}
return true;
}
function columnBlank(imageData, width, x, top, bottom) {
for (var y = top; y < bottom; ++y) {
@XerxesNoble
XerxesNoble / animate_values.js
Last active March 9, 2024 13:07
Basic javascript linear value animation that can accept easing functions and provides update & complete callbacks
/**
* @desc Basic linear value animation that can accept simple easing functions and provides update & complete callbacks
* @param {Object} values - Object with numerical values. eg. { value1: 0, value2: 20, someKey: 55 }
* @param {Number} duration - How long (in milliseconds) the animation will be
* @param {Object} options - target values, update callback & complete callback
* @param {Function} [options.onComplete=(values) => values] - Callback that fires once animation is complete
* @param {Function} [options.onUpdate=(values) => values] - Callback that fires when animation frame updates
* @param {Function} [options.ease=(t) => t] - easing method eg. https://gist.github.com/gre/1650294
* @example
*
@micahjon
micahjon / snippets.sh
Last active May 2, 2016 15:41
Bash Snippets
### Find & Replace
## basic examples
# replaces "urchin" with "analytics" in all files in current directory and backs up originals as .bak files
find . -type f -exec sed -i.bak "s/urchin.js/analytics.js/g" {} \;
# reverses the prior command, restoring all the original (now .bak) files
find . -name "*.bak" -exec sh -c 'mv -f $0 ${0%.bak}' {} \;
@rupl
rupl / sw-font-load.js
Created December 7, 2015 11:54
Use Service Worker to load fonts async/direct via client-side logic.
// Load fonts async by default
// Load sync from SW cache when available.
//
// Snippet assumes your Service Worker caches fonts as part of installation, so that
// an 'activated' Service Worker means the fonts are cached with 100% confidence.
if (
'serviceWorker' in navigator &&
navigator.serviceWorker.controller !== null &&
navigator.serviceWorker.controller.state === 'activated'
) {