Skip to content

Instantly share code, notes, and snippets.

View ithinkihaveacat's full-sized avatar

Michael Stillwell ithinkihaveacat

View GitHub Profile
@ithinkihaveacat
ithinkihaveacat / jsx.11ty.tsx
Created April 16, 2020 16:17
eleventy + jsx
//
@ithinkihaveacat
ithinkihaveacat / .gitignore
Last active October 2, 2019 22:05
jsx demo
node_modules
*.js
*.jsx
@ithinkihaveacat
ithinkihaveacat / removeUnused.js
Last active September 22, 2019 20:58
Removes all classes and ids from HTML that do not affect styling
// Returns e as HTML string
function serialize(e) {
const s = new XMLSerializer();
return s.serializeToString(e);
}
// Recursively removes all classes and ids that do not affect styling from element e
function removeUnused(e) {
const s0 = window.getComputedStyle(e).cssText;
const c = Array.from(e.classList.values());
function compose<T>(...fna: Array<(p: T) => Promise<T>>) {
return (p: T) => {
return fna.reduce((a, fn) => a.then(fn), Promise.resolve(p));
};
}
const pipe = compose(foo, bar, baz);
const res1 = pipe(arg);
// equivalent
@ithinkihaveacat
ithinkihaveacat / curl-to-headers.ts
Last active October 22, 2018 14:01
Extracts HTTP headers from curl command-line args
// convert headers object to object
Array.from(headers).reduce((a: { [k: string]: string }, kv) => (a[kv[0]] = kv[1], a), {})
// parse curl-compatible -H arguments in command-line into object
function seq(first: number, last: number): number[] {
if (first < last) {
return [first].concat(seq(first + 1, last));
} else if (first > last) {
return [last].concat(seq(first, last - 1));
} else {
@ithinkihaveacat
ithinkihaveacat / Code.js
Created December 21, 2017 15:30
Google Sheets: Resize Cells
// Resize rows and columns to fit the data we're inserting. This requires special
// permission for some reason.
function setSizes() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
for (var i=0; i < sheets.length; i++) {
var sheet = sheets[i];
var range = sheet.getDataRange();
var numRows = range.getNumRows();
for (var r=2; r <= numRows; r++) {
@ithinkihaveacat
ithinkihaveacat / pboolean.js
Last active December 8, 2017 17:04
Promise-compatible boolean AND and OR functions
// Not completely sure these do the right thing on reject...
function pand(...args) {
return args.length != 0 ? new Promise((resolve, reject) => {
let [tc, fc] = [0, 0];
function f(v) {
tc += v ? 1 : 0;
fc += v ? 0 : 1;
if (tc == args.length) resolve(true);
if (fc == 1) resolve(false);
@ithinkihaveacat
ithinkihaveacat / proxy.js
Last active August 3, 2017 14:49
Simple Node.js proxy server
const BASE = "https://beebo.org";
const http = require("http");
const https = require("https");
const URL = require("url").URL;
const server = http.createServer((req0, res0) => {
const options = new URL(req0.url, BASE);
console.log(`${req0.method} ${options.toString()}`);
window.myLibrary = (() => { let a, p = new Promise((...args) => a = args); [p.resolve, p.reject] = a; return p; })();
window.myLibrary.resolve("jjj"); // when library ready
window.myLibrary.then(console.log); // outputs "jjj"
@ithinkihaveacat
ithinkihaveacat / cacheTools.js
Last active April 18, 2017 14:20
Simple functions for querying the SW cache
// Simple functions for querying the SW cache.
// Only approximate! (1) Only includes bodies. (2) Can't count opaque resources.
function cacheDump(c) {
return c.keys().then(a => {
return Promise.all(a.map(req =>
c.match(req).then(res => res.clone().blob().then(b => [req.url, b.size]))
)).then(a => new Map(a));
});