Skip to content

Instantly share code, notes, and snippets.

View ds300's full-sized avatar
🏳️‍🌈

David Sheldrick ds300

🏳️‍🌈
View GitHub Profile
class Stats {
periods = 0
totals = {} as Record<string, number>
starts = {} as Record<string, number>
start(name: string) {
this.starts[name] = performance.now()
}
end(name: string) {
if (!this.starts[name]) throw new Error(`No start for ${name}`)
@ds300
ds300 / usePoller.ts
Created September 15, 2023 09:52
usePoller.ts
import { useCallback, useEffect, useMemo } from 'react'
export class Poller<T> {
private interval: ReturnType<typeof setInterval> | null = null
constructor(
private readonly callback: (arg: { didCancelRef: { current: boolean } }) => T,
private readonly intervalMs: number
) {}
readonly didCancelRef = { current: false }
start() {
{
"vim.easymotion": true,
"vim.leader": "<space>",
"files.autoSave": "afterDelay",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": ["source.addMissingImports"],
"vim.visualModeKeyBindingsNonRecursive": [
{
"before": ["<leader>", "g"],
"commands": ["editor.action.smartSelect.grow"]
@ds300
ds300 / add-extensions.ts
Last active July 24, 2024 16:01
Add .js, .mjs, or .cjs file extensions to imports/exports in (transpiled) ESM files
import { existsSync, readFileSync, statSync, writeFileSync } from 'fs'
import glob from 'glob'
import path from 'path'
import { parse, print, visit } from 'recast'
const extensions = ['.js', '.mjs', '.cjs']
function resolveRelativePath(importingFile: string, relativePath: string) {
if (!relativePath.startsWith('.')) {
return relativePath
}
import React, { useContext, useEffect, useMemo, useState } from "react"
type ProviderComponent<T> = React.FC<{ initialValue: T }>
interface PrivateContextValue<T> {
useGlobalStateProxy(): T
}
class GlobalStateContext<T> {
constructor(private context: React.Context<PrivateContextValue<T>>, public Provider: ProviderComponent<T>) {}
}
/**
@ds300
ds300 / clean.sh
Last active May 12, 2019 09:29 — forked from zephraph/clean_node_modules.sh
Clean up node_modules
#!/bin/bash
DAYS_SINCE_LAST_CHANGE=14
TOTAL_BYTES_REMOVED=0
Mb=1000000
Kb=1000
node_modules=$(find . -name "node_modules" -type d -prune)
@ds300
ds300 / yarn-flat-auto.js
Created June 28, 2017 11:45
Automatically choose the latest versions of all packages when running yarn --flat
#!/usr/bin/env node
# run this before using: yarn add --dev node-pty semver
const os = require("os")
const pty = require("node-pty")
const semver = require("semver")
function semverComparator(a, b) {
if (semver.lt(a, b)) {
@ds300
ds300 / index.js
Created August 18, 2016 07:36
requirebin sketch
const d = require("derivable")
const name = d.atom("World"); // the name of the user
const countryCode = d.atom("en"); // for i18n
// static constants don't need to be wrapped
const greetings = {
en: "Hello",
de: "Hallo",
es: "Hola",
@ds300
ds300 / index.js
Created July 25, 2016 13:11
requirebin sketch
// Welcome! require() some modules from npm (like you were using browserify)
// and then hit Run Code to run your code on the right side.
// Modules get downloaded from browserify-cdn and bundled in your browser.
var patch = require('snabbdom').init([
require('snabbdom/modules/props'),
]);
var h = require('snabbdom/h');
var elem = h('div', [
List(
Map(
"id" -> "d979c0f0c2f4b1ec310aff3ff48f75aa4e54c07752ae4fb76161739c6fd2980a2f0f7c469706aac421c449336f34147b8d72f89e2ecd6e7c6e286395a6548b7d",
"timestamp" -> "1462452325349",
"name" -> "Haim",
"country" -> "Curacao",
"amount" -> "4571.62"
),
Map(
"id" -> "857f1e5f2b8c0683aa450970dcf8c528e3fa87a9bf56e046b6b7ca5560ca0a76dd555b6666b88c35d908980218915e203882edc1fe79af6687d612357625e6e8",