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 / eleventy-load-obfuscate.js
Created April 17, 2022 15:34
Eleventy-load obfuscate for use with eleventy-load-html plugin
/**
* Escape RegExp special characters
*
* Inlined from escape-string-regexp@5.0.0 as we don't use ESM here.
*
* @see https://github.com/sindresorhus/escape-string-regexp
*
* @param {string} string
* @returns {RegEx}
*/
@johnhunter
johnhunter / grid_mixins.scss
Last active January 29, 2019 08:35 — forked from ksenzee/grid_mixins.scss
Mixins that make it possible to use CSS Grid in IE 10/11 as well as in modern browsers. Based on mixins by Sascha Fuchs at https://medium.com/@gisugosu/css-grid-layout-spec-2-to-spec-1-transpiler-with-sass-415dff4dd31b.
//
// Grid mixins to support IE11
// https://gist.github.com/johnhunter/1c7d332e7c2ed8351e36c40695b94d4f
//
/// Add Gap between the boxes
///
/// @author Sascha Fuchs
///
/// @group core - cssgrid
exports.default = function (socket) {
return function (store) {
return function (next) {
return function (action) {
if (action.meta && action.meta.remote) {
var clientId = store.getState().get('clientId');
socket.emit('action', objectAssign({}, action, { clientId: clientId }));
}
return next(action);
};