Skip to content

Instantly share code, notes, and snippets.

@tshddx
tshddx / DisableFilteringAndSortingByDefaultPlugin.ts
Created March 15, 2023 23:47
PostGraphile plugin to disable all filtering and sorting by default, and require you to opt in to each table and column you want to filter or sort.
import { Plugin } from 'postgraphile';
import type { PgClass, PgAttribute } from 'graphile-build-pg';
//
// This plugin disables all filtering and sorting by default, and requires you
// to opt in to each table and column you want to filter or sort.
//
// PostGraphile's default behavior is:
// - Every GraphQL type and connection has `condition` and `orderBy` args.
@tshddx
tshddx / shift-date-to-utc.js
Last active September 12, 2022 19:28
JavaScript utility to shift a date to UTC
/*
Note: if you only want to format a date as a string and not do any date
manipulation, you can use provided formatting functions to specify a time zone:
const date = new Date(dateString);
const formattedDate = date.toLocaleDateString('en-US', { timeZone: 'UTC' });
All JavaScript Date objects are in the browser's local time zone. There's no way
around that. This means that if you receive a date string from the backend and
create a Date object with `new Date(unixTimestampOrISOString)` that Date object
@tshddx
tshddx / input-autofocus-test.html
Created January 26, 2022 23:26
Testing input autofocus
<!DOCTYPE html>
<html>
<head>
<title>Parcel Sandbox</title>
<meta charset="UTF-8" />
</head>
<body>
<button onclick="document.write('<input autofocus />')">Add a new input with autofocus</button>
@tshddx
tshddx / wordle-bot-showdown.ts
Created January 23, 2022 20:05
Script to test Wordle bots against each other
type Word = string;
// A game is a series of guesses and a status (note that a Game might still be
// in progress, and the current game status is the same as the status of the
// last guess).
type Game = {
readonly guesses: Guess[];
readonly status: Status;
};
@tshddx
tshddx / solution.js
Last active December 8, 2021 22:27
Solution to hallway challenge from "Fake coding interview with Dan Abramov"
// From: https://youtu.be/-w-P4u0x8ig?t=3075
function countMeetings(hallway) {
// Let's go over the hallway from left to right and keep track of how many
// people are moving right behind us at that point. Each time we encounter
// someone moving left, we know they will meet everyone who is moving right
// behind us at that point, so we add that number to the meetings count.
let peopleMovingRightBehindUs = 0;
let meetings = 0;
for (const char of hallway) {
@tshddx
tshddx / asdfasdf
Created February 9, 2021 20:56
asdfasdf
asdfadsf
Flexport https://www.flexport.com San Francisco
SpaceX http://www.spacex.com Hawthorne, California
Coinbase https://www.coinbase.com/ San Francisco
InVision https://www.invisionapp.com/ New York City, New York
Carta (company) http://carta.com San Francisco
Ripple (cryptocurrency) https://ripple.com/ San Francisco
Calm (company) https://www.calm.com/ San Francisco
WeWork https://www.wework.com/ Manhattan, New York City
We can make this file beautiful and searchable if this error is corrected: It looks like row 7 should actually have 12 columns, instead of 1 in line 6.
Series_reference,Period,Data_value,STATUS,UNITS,Subject,Group,Series_title_1,Series_title_2,Series_title_3,Series_title_4,Series_title_5
CEPQ.S2371,1996.12,899,FINAL,Index,Capital Goods Price Index - CEP,"Price Index by Item - Plant, Machinery and Equipment (Base: Sept 1999 = 1000)",Glass and glass products,,,,
CEPQ.S2371,1997.03,884,FINAL,Index,Capital Goods Price Index - CEP,"Price Index by Item - Plant, Machinery and Equipment (Base: Sept 1999 = 1000)",Glass and glass products,,,,
CEPQ.S2371,1997.06,925,FINAL,Index,Capital Goods Price Index - CEP,"Price Index by Item - Plant, Machinery and Equipment (Base: Sept 1999 = 1000)",Glass and glass products,,,,
CEPQ.S2371,1997.09,932,FINAL,Index,Capital Goods Price Index - CEP,"Price Index by Item - Plant, Machinery and Equipment (Base: Sept 1999 = 1000)",Glass and glass products,,,,
CEPQ.S2371,1997.12,929,FINAL,Index,Capital Goods Price Index - CEP,"Price Index by Item - Plant, Machinery and Equipment (Base: Sept 1999 = 1000)",Glass and glass products,,,,
CEPQ.S2
@tshddx
tshddx / spinner_trig.js
Created January 22, 2020 23:20
Some trigonometry for drawing spinning squares inside other squares
function generateStationarySquares(maxRad, squareCount) {
const squares = [];
for (let i = 0; i < squareCount; i++) {
// Divide maxRad by sqrt(2) i times.
const rad = maxRad / Math.pow(Math.sqrt(2), i);
// We want the outermost stationary square to be rotated 90 degrees, like a
// diamond.
const ang = i % 2 === 0 ? Math.PI / 4 : 0;
squares.push([rad, ang]);
}
@tshddx
tshddx / preferred_numbers.py
Created July 12, 2019 20:41
Preferred numbers (Python)
def preferred_numbers(x, precision = 2):
"""
>>> preferred_numbers(2)
[1.0, 3.16]
>>> preferred_numbers(3)
[1.0, 2.15, 4.64]
>>> preferred_numbers(5)
[1.0, 1.58, 2.51, 3.98, 6.31]
>>> preferred_numbers(10)
[1.0, 1.26, 1.58, 2.0, 2.51, 3.16, 3.98, 5.01, 6.31, 7.94]