Skip to content

Instantly share code, notes, and snippets.

View haggen's full-sized avatar

Arthur Corenzan haggen

View GitHub Profile
@haggen
haggen / useForm.ts
Created October 21, 2023 18:03
Draft of zod validated form state hook.
import { ChangeEvent, createContext, useMemo, useReducer } from "react";
import { AnyZodObject, ZodError, z } from "zod";
import { mapValues } from "~/lib/map";
type FieldState = {
name: string;
value: string;
error: undefined | ZodError;
touched: boolean;
};
@haggen
haggen / package.json
Last active July 1, 2023 13:52
Git hook to lint (eslint) and format (prettier) staged files and abort the commit if anything has changed.
{
"scripts": {
"prepare": "test -f .git/hooks/pre-commit || cp scripts/pre-commit .git/hooks",
"format": "prettier . --cache",
"lint": "eslint . --report-unused-disable-directives --max-warnings 0 --cache",
}
}
@haggen
haggen / css.d.ts
Last active May 24, 2023 22:38
TypeScript type for CSS modules. e.g. import styles from "styles.module.css".
/**
* Stylesheet module classes.
*/
declare module "*.module.css" {
const stylesheet: {
[key: string]: string;
};
export = stylesheet;
}
@haggen
haggen / extended-utility-types.d.ts
Last active December 4, 2022 12:48
Extended TypeScript utility types.
/**
* Make all properties of T optional, except those in U. If exempt keys were already optional they'll stay optional.
*/
type Semipartial<T, U extends keyof T> = Partial<T> & Pick<T, U>;
/**
* Make all properties of T required, except those in U. If exempt keys were already required they'll stay required.
*/
type Semirequired<T, U extends keyof T> = Required<T> & Pick<T, U>;
@haggen
haggen / ForwardConn.ps1
Created July 9, 2022 00:12
Forward connection via PowerShell.
param($addr, $port)
netsh interface portproxy delete v4tov4 $port
netsh interface portproxy add v4tov4 listenport=$port listenaddress=0.0.0.0 connectport=$port connectaddress=$addr
@haggen
haggen / shrink-wsl-vhdx.ps1
Created July 9, 2022 00:10
Redeem space from WSL virtual disk.
Optimize-VHD -Mode Full %APPDATA%\Local\Packages\CanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fndgsc\LocalState\ext4.vhdx
@haggen
haggen / create-component.sh
Last active May 6, 2023 17:34
Script to automate creating new components in a React application.
#!/usr/bin/env bash
#
# Create React Component.
#
# Sane defaults.
# -e Exit on error.
# -u Whine on undefined variables.
# -o pipefail Exit main script if pipe process fails.
set -euo pipefail
@haggen
haggen / README.md
Created May 17, 2022 22:42
Run npm scripts per NODE_ENV value.

In your package.json:

"scripts": {
  "start": "per-env",
  "start:development": "echo We're in development!",
  "start:production": "echo We're in production!",
}
@haggen
haggen / map.ts
Last active March 4, 2023 12:52
Map function in TypeScript that works for Arrays as well as Objects.
/**
* Unknown array type alias.
*/
type TArray = unknown[];
/**
* Unknown object type alias.
*/
type TObject = Record<string | symbol, unknown>;
@haggen
haggen / haggen.opml
Created December 18, 2021 13:23
RSS feed in OPML
<?xml version="1.0" encoding="UTF-8"?>
<opml version="1.0">
<head>
<title>Arthur subscriptions in feedly Cloud</title>
</head>
<body>
<outline text="Everything" title="Everything">
<outline type="rss" text="TechCrunch" title="TechCrunch" xmlUrl="http://feeds.feedburner.com/Techcrunch" htmlUrl="https://techcrunch.com" />
<outline type="rss" text="TorrentFreak" title="TorrentFreak" xmlUrl="http://feeds.feedburner.com/Torrentfreak" htmlUrl="https://torrentfreak.com" />