Skip to content

Instantly share code, notes, and snippets.

View nfarina's full-sized avatar

Nick Farina nfarina

View GitHub Profile
@nfarina
nfarina / package.json
Created March 15, 2024 16:34
Reproduction of `tsc --watch` not working
{
"name": "tsc-test",
"version": "1.0.0",
"description": "tsc watch problem repro",
"license": "ISC",
"scripts": {
"start": "tsc test.ts --watch"
},
"dependencies": {
"typescript": "^5.4.2"
@nfarina
nfarina / stripeiterators.ts
Last active January 5, 2023 22:56
Helpful utility functions for iterating objects from the Stripe API using new async iterators
import Stripe from "stripe";
/**
* Utility functions for iterating objects from the Stripe API.
*/
/**
* Iterates over a Stripe API list, automatically handling pagination.
*
'Declare statements
DECLARE SUB playGame ()
DECLARE SUB setParms ()
DECLARE SUB clearTanks ()
DECLARE SUB checkGround ()
DECLARE SUB explodeTank ()
DECLARE SUB fireShotRight ()
DECLARE SUB fireShotLeft ()
DECLARE SUB getInfoRight ()
@nfarina
nfarina / useResettableState.ts
Created August 6, 2021 16:24
A version of React's `useState` that resets the value to initial whenever the given dependency array changes. Very helpful when you need to reset some internal state as the result of getting new props.
import { DependencyList, Dispatch, SetStateAction, useState } from "react";
/**
* This is like useState() but with the added feature of returning the initial
* value whenever the dependency list changes. This is super useful for allowing
* components to "reset" some internal state as a result of getting new props.
*/
export function useResettableState<S>(
initial: S | (() => S),
deps: DependencyList,
#!/bin/bash
#
# Backs up my entire website, in case Tumblr or CloudApp goes down someday.
# Last time I ran this, it took 18 minutes.
#
wget \
--mirror `# turns on recursion and timestamping, basically says we want to "mirror" the whole site` \
--convert-links `# after download, convert all links to point to localhost` \
/**
* A "fast" version of @types/styled-components that does not impact the
* performance of the TypeScript language service (which is directly related to
* the performance of VSCode).
*
* NOTE: This implements only a fraction of the features provided by the
* @types/styled-component package. Notably, it does not support typing the
* "props" parameter when interpolating within template strings. If someone
* knows how to type that without impacting performance, please let me know!
*/
@nfarina
nfarina / duolingohelper.js
Last active February 28, 2022 16:10
UserScript adding useful features to Duolingo, including more keyboard shortcuts, and hiding the "hint text" for translation exercises to focus on listening skills.
// ==UserScript==
// @name Duolingo Helper
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Adds useful features to Duolingo, including more keyboard shortcuts, and hidden "hint text" for translation exercises to concentrate on listening skills.
// @author Nick Farina
// @match https://*.duolingo.com/*
// @grant none
// ==/UserScript==
@nfarina
nfarina / useLocalStorage.ts
Last active December 24, 2022 15:04
Fix for Safari
import { useCallback, useEffect, useState } from "react";
// Adapted from:
// https://github.com/rehooks/local-storage/blob/master/src/use-localstorage.ts
type Setter<S> = (value: S) => any;
/**
* React hook to enable updates to state via localStorage.
* This updates when the {writeLocalStorage} function is used, when the returned function
@nfarina
nfarina / AutoThemeSwitcher.js
Created May 14, 2019 19:04
Auto Dark Theme for Storybook
import addons from '@storybook/addons';
import { FORCE_RE_RENDER } from '@storybook/core-events';
import { themes } from '@storybook/theming';
// Automatically switch light/dark theme based on system pref.
addons.register("auto-theme-switcher", api => {
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)');
let lastTheme;
@nfarina
nfarina / mock-storage.js
Last active November 23, 2023 15:50
Mock Google Cloud Storage for JS
//
// Quick & Dirty Google Cloud Storage emulator for tests. Requires
// `stream-buffers` from npm. Use it like this:
//
// `new MockStorage().bucket('my-bucket').file('my_file').createWriteStream()`
//
class MockStorage {
buckets: {[name: string]: MockBucket};