Skip to content

Instantly share code, notes, and snippets.

View lzehrung's full-sized avatar

Luke Zehrung lzehrung

  • ClearGov Inc.
  • South Bend, IN
  • 03:49 (UTC -04:00)
View GitHub Profile
@lzehrung
lzehrung / remove-node_modules-recurse.ps1
Created May 10, 2024 17:44
powershell remove node_modules recursively
Get-ChildItem -Path "." -Include "node_modules" -Recurse -Directory | Remove-Item -Recurse -Force
@lzehrung
lzehrung / all-settled-with-concurrency.ts
Last active March 31, 2025 13:20
`promise.allSettled` but with a concurrency limit
type PromiseGenerator<T> = () => Promise<T>;
interface PromiseResult<T> {
status: 'fulfilled' | 'rejected';
value: T | Error;
}
/**
* Executes a list of promise-generating functions with a specified maximum concurrency,
* returning a promise that resolves to an array of results. Each result includes the
@lzehrung
lzehrung / test-express-timeout.ts
Last active July 14, 2023 14:40
Test express connect-timeout middleware
import express from 'express';
import requestTimeout from 'connect-timeout';
express()
.get(
'/long-running',
requestTimeout('3s', {
respond: false,
}),
(req, res) => {
const alpha = 'abcdef'.toUpperCase().split('');
const randomNumber = () => Math.floor(Math.random() * 10).toString();
const randomLetter = () => alpha[Math.floor(Math.random() * alpha.length)];
const randomCharacter = () => Math.random() > 0.5 ? randomLetter() : randomNumber();
function generateOtp(length = 6) {
return Array.from({ length }, randomCharacter).join('');
}
const pass = generateOtp();
// https://www.typescriptlang.org/play?#code/IYZwngdgxgBAZgV2gFwJYHsIwA4Cd0C2qIApgIIA2FAFAJQwDeAUDKzFJiMjACbDIkYAXhgQSAdxgARfiToBuFm2DjgqbgAV8RUgDpgVagG0lbNmMlbCxOdVwkQ6CgDcS9IQD5GpszFLIAFVQCEnQEZDsHJ1cAGhgAJgAGZNpFXzYAX1oYnzMLGCsdW3tHFzdhL2Z09P8gkLCIkuiSOKSUtOqsgF0fVJ8OCFKSXQp0AHNqAAMDChhkdHQAaxgAEgZ8mQE6XWcDBBIAeTg6GABaXlkdvcPj2gyCEEnaJiYMl9BIWEQUDCxSAEd9hA0AYTlU2AMuBcBMJRBJpLIFD4VGpuPlCjZqJFSq53JVcmxasFQuFsc1WslEn0zFkOqwUeo4ZZtJiyWU8d5qn4SIFiQ02bEEpTqZkRaxIU5hqMJpNSLhUAY5gtlmsNojaFcKPsjidznwBJrtbd7o9nm8mNQPtAYCdPJzlKpGXhrKRKDQxTAGdwAUCQe7FFkFEA
async function promiseAll() {
const date = new Date();
await Promise.all([
new Promise((resolve) => {
setTimeout(resolve, 2000);
}),
new Promise((resolve) => {
setTimeout(resolve, 2000);
@lzehrung
lzehrung / nextjs_nested_dynamic_routes.md
Last active May 19, 2022 02:46
next.js nested dynamic routes

url: /states/IN/counties/12345

folder structure:

/pages
  /states
    /[state]
      /counties
        [county].tsx
 index.tsx
@lzehrung
lzehrung / win-foreign-addr.ps1
Created May 6, 2022 20:58
open connections
netstat -p TCP -fan | Select -Skip 4 | ForEach {
$Properties = $_.Trim() -split '[\s]+'
[PSCustomObject]@{
'Proto'= $Properties[0]
'Local Address'= $Properties[1]
'Foreign Address'= $Properties[2]
'State'= $Properties[3]
}
} | Select -Expand 'Foreign Address'
@lzehrung
lzehrung / find-remove-files.ps1
Last active December 10, 2022 07:41
Find all child files modified in the last day and remove them (ignoring errors and confirm prompts)
Get-ChildItem -Recurse -Filter *.csv | Where-Object {$_.LastWriteTime -gt (Get-Date).AddDays(-1)} | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue
// inspired by https://schneidenbach.gitbooks.io/typescript-cookbook/content/nameof-operator.html
/** Ensure the string value is a valid property name of type `T` */
function propName<T>(name: keyof T): string {
return name.toString();
}
/** Ensure the string value is a valid property name of instance of type `T`. */
function propOf<T>(instance: T, name: keyof T): string {
return propName<T>(name);
@lzehrung
lzehrung / get-process-using-port.md
Last active August 28, 2024 15:57
Get Process Using Port

Windows:

netstat -aon | findstr '8080'

powershell Get-Process -Id (Get-NetTCPConnection -LocalPort 8080).OwningProcess