Skip to content

Instantly share code, notes, and snippets.

View johnhunter's full-sized avatar
🌊
Working on net-zero energy

John Hunter johnhunter

🌊
Working on net-zero energy
View GitHub Profile
@johnhunter
johnhunter / HttpError.ts
Last active September 30, 2023 17:30
Augmenting native fetch to add error state handling and types
const getErrorMessage = (response?: Response): string => {
const fallbackMsg = 'an unknown error';
if (!response) {
return fallbackMsg;
}
const code = typeof response.status === 'number' ? response.status : '';
const status = `${code} ${response.statusText || ''}`.trim();
return status ? `status code ${status}` : fallbackMsg;
@johnhunter
johnhunter / dependabot.yml
Created September 8, 2023 17:49
Example .github dependabot config for using the new groups feature
version: 2
updates:
- package-ecosystem: npm
directory: /
schedule:
interval: weekly
day: "saturday"
time: "08:00"
groups:
patches:
@johnhunter
johnhunter / utilities.ts
Created September 2, 2023 15:16
Test utility that augments testing-library with the user-event setup
import { render as renderComponent } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
type RenderArgs = Parameters<typeof renderComponent>;
export * from '@testing-library/react';
/**
* Augments the RTL render with a userEvent user
*/
/**
* Given color saturation and lightness percentages,
* returns a function that generates a unique css `hsl`
* color value for a given string.
*/
const hashColor = (saturation: number, lightness: number) => (
str: string,
): string => {
let hash = 0;
for (let i = 0; i < str.length; i++) {
@johnhunter
johnhunter / killnode
Last active July 26, 2023 17:27
Kill node processes bound to a specific port (MacOS)
#!/bin/bash
touch temp.text
lsof -n -i4TCP:$1 | awk '{print $2}' >temp.text
pidToStop=$( (sed '2q;d' temp.text))
>temp.text
if [[ -n $pidToStop ]]; then
kill -9 $pidToStop
echo "Killed process $pidToStop running on port $1."
else
@johnhunter
johnhunter / rename.sh
Created July 19, 2023 09:18
Find and git move files
# rename all JS files in src/ to TS
find src -name '*.js' -exec bash -c 'git mv "$0" "${0%.js}.ts"' "{}" \;
@johnhunter
johnhunter / hover.html
Created March 9, 2023 22:28
Using has to highlight adjacent elements
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Adjacent hover highlight</title>
<style>
body {
font-family: sans-serif;
}
@johnhunter
johnhunter / Espresso Soda.tmTheme
Created June 16, 2012 19:22
Sublime Espresso Soda theme with added styles for sublime linter.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>name</key>
<string>Espresso Soda</string>
<key>settings</key>
<array>
<dict>
<key>settings</key>
@johnhunter
johnhunter / bash-git-alias.sh
Last active April 23, 2022 10:42
Git Bash aliases
# git aliases
alias gs='git status'
alias ga='git add $1'
alias gb='git branch $1'
alias gcl='git clone $1'
alias gco='git checkout $1'
alias gm='git commit -m $1'
alias gma='git commit -am $1'
alias gph='git push'
alias gpl='git pull'
@johnhunter
johnhunter / app.js
Created December 10, 2011 21:53
Nodejs chat with socket.io
/*
Websockets using http://socket.io/
Also with express web mvc framework http://expressjs.com/
*/
var app = require('express').createServer(),
io = require('socket.io').listen(app),
port = 88;