Skip to content

Instantly share code, notes, and snippets.

View haggen's full-sized avatar

Arthur Corenzan haggen

View GitHub Profile
@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 / 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 / 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 / migrations.sql
Created April 25, 2019 14:31
Pure SQL migration procedure for PostgreSQL.
-- Migration script in pure SQL.
-- This script is ran at server boot-up.
--
\set on_error_stop true
-- Migration temporary sequence.
--
create temporary sequence migration_steps;
@haggen
haggen / Caddyfile
Created June 9, 2017 03:14
Friendly development environment on macOS with Docker for Mac, dnsmasq and caddy.
http://test.dev {
proxy / 127.0.0.1:3000
}
@haggen
haggen / hyperv.ps1
Created August 12, 2021 15:20
Stop Hyper-V services, wait for any key then resume the services
if (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) {
Start-Process -FilePath PowerShell.exe -Verb RunAs -ArgumentList "-ExecutionPolicy Bypass -File ""$PSCommandPath"""
Exit
}
Write-Host "1. Stopping Hyper-V* services"
$s = Get-Service | Where-Object { $_.DisplayName -like "Hyper-V*" -and $_.Status -eq "Running" }
$s | ForEach-Object { Stop-Service -Force $_.ServiceName }
Write-Host -NoNewline "2. Press ENTER to continue"
@haggen
haggen / less.js
Created March 5, 2021 13:08
Next.js v9/v10 integration for LESS leveraging built-in support for SASS
const cloneDeep = require("clone-deep");
const { readFileSync, existsSync } = require("fs");
const path = require("path");
// next-less (official plugin to integrate Less) is now
// deprecated so we have to provide our own solution.
//
// Next.js already provides built-in integration with CSS and Sass, but it
// relies on a dynamic WebPack config. generation to do it optimally.
//
@haggen
haggen / use-local-stored-state.ts
Created June 12, 2020 12:21
TypeScript Local Storage backed state hook for React
import { useState, useEffect } from "react";
type Serializable =
| null
| boolean
| number
| string
| Date
| { toJSON(): string }
| Serializable[]