Skip to content

Instantly share code, notes, and snippets.

View thomaswilburn's full-sized avatar
🦝

Thomas Wilburn thomaswilburn

🦝
View GitHub Profile
@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/std@0.208.0/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() {
@thomaswilburn
thomaswilburn / pigeon.js
Last active March 28, 2024 21:53
Convert CSS to nested CSS
import { parse } from "https://deno.land/x/css@0.3.0/mod.ts";
var file = Deno.args[0];
var input = await Deno.readTextFile(file);
var parsed = parse(input, { value: true });
var root = {
rules: [],
@thomaswilburn
thomaswilburn / decode.js
Last active July 15, 2023 05:06
Decoding function for Google's polyline encoded strings
/*
This is more complicated than it would normally be, because A) it delivers
output in pairs, and B) those pairs are all offsets from the previous pair.
If you're just delivering a stream of values, this is about 1/2 as long.
*/
function decode(str) {
// last known coordinate for adding offsets
var from = [0, 0];
var value = 0;
var shift = 0;