Skip to content

Instantly share code, notes, and snippets.

View thomaswilburn's full-sized avatar
🦝

Thomas Wilburn thomaswilburn

🦝
View GitHub Profile
class Cluster {
start = 0;
end = 0;
min = 0;
max = 0;
items = [];
constructor(items, start, end, min, max) {
this.items = items;
this.start = start;
@thomaswilburn
thomaswilburn / signals.js
Created June 18, 2024 17:40
Preact Signals But With EventTarget
var stack = [];
var verbose = false;
// make our own undefined sentinel value
var undefined = Symbol("undefined signal");
class Signal extends EventTarget {
#value = undefined;
#computation = null;
#lifetime = new AbortController();
@thomaswilburn
thomaswilburn / import-ants.js
Last active February 20, 2024 21:34
Async imports for CSS files
var links = document.querySelectorAll(`link[rel="stylesheet"][media="async"], link[rel="async-styles"]`);
for (var link of links) {
var resolved = new URL(link.href, window.location);
var processed = await getSheet(resolved.toString());
var style = document.createElement("style");
var constructed = new CSSStyleSheet();
var output = serializeCSS(processed);
constructed.replaceSync(output);
// console.log(output);
@thomaswilburn
thomaswilburn / blanks.html
Last active December 15, 2023 01:07
Fill In The Blanks
<!doctype html>
<style>
.card {
display: grid;
grid-template-columns: 1fr 2fr;
& img {
max-height: 200px;
}
}
@thomaswilburn
thomaswilburn / defaultdict.js
Created December 12, 2023 22:35
Defaultdict but make it JavaScript
const TARGET = Symbol("proxy target");
function defaultdict(missing) {
var dict = {};
var instantiate = missing;
if (missing instanceof Function) {
if (missing.constructor) {
instantiate = () => new missing();
}
} else if (missing instanceof Object) {
@thomaswilburn
thomaswilburn / index.js
Created December 11, 2023 17:50
ReactiveStore
var proxyRegistry = new WeakMap();
function observe(root, callback) {
var handler = {
get(target, property, rec) {
var value = target[property];
if (value instanceof Object && !(value instanceof Function)) {
if (proxyRegistry.has(value)) {
return proxyRegistry.get(value);
}
@thomaswilburn
thomaswilburn / index.js
Created December 5, 2023 21:04
Classy signals
var tracking = [];
var pending = new Set();
function anticipate(fn) {
pending.add(fn);
requestAnimationFrame(() => {
for (var p of pending) p();
pending.clear();
});
}
@thomaswilburn
thomaswilburn / index.html
Created December 3, 2023 03:39
Benchmark Lit iteration vs. replaceChildren
<!doctype html>
<ul id="ul"></ul>
<script type="module">
import { html, render } from "https://unpkg.com/lit-html";
import { repeat } from "https://unpkg.com/lit-html/directives/repeat.js";
import { map } from "https://unpkg.com/lit-html/directives/map.js";
function replacer(container, data, factory, update = () => {}) {
@thomaswilburn
thomaswilburn / datapatcher.js
Last active November 30, 2023 18:56
Update DW graphics from a Google Sheet
// run with deno run --allow-all datapatcher.js {SHEET_ID} {SHEET_TAB (optional)}
import { login, google } from "https://raw.githubusercontent.com/Chalkbeat/deno-google-login/main/index.js";
import * as flags from "https://deno.land/[email protected]/flags/mod.ts";
var args = flags.parse(Deno.args);
var spreadsheetId = args.sheet || args._[0];
@thomaswilburn
thomaswilburn / geocoding.md
Last active November 14, 2023 08:57
So You Want to Geocode an Address in Sheets

If you have fewer than a thousand addresses and you need them geocoded quickly and accurately, Google Sheets is generally the best way to do it. Here's how to set up the Apps Script necessary. Here is a demo sheet with everything set up, including the Apps Script code.

Create or open your sheet, and then in the Extensions menu, choose "Apps Script" to open the editor. Erase the placeholder contents in Code.gs and paste this in:

const SHEET_NAME = "data";
const ADDRESS_COLUMN = 2;
const LATITUDE_COLUMN = 3;

function geocode() {