Skip to content

Instantly share code, notes, and snippets.

@bolencki13
bolencki13 / proxy_assignment_react.tsx
Last active July 28, 2025 12:58
Implement a Proxy on an object to allow assignment of values while masking setState/dispatch in react
import { useMemo, useReducer } from 'react';
const isProxy = Symbol('is_proxy');
function setNestedValue<T extends object>(obj: T, path: string, value: any): T {
const parts = path.split('.');
let current: any = obj;
for (let i = 0; i < parts.length; i++) {
const part = parts[i];
@bolencki13
bolencki13 / game.ts
Created July 7, 2025 15:39
TicTacToe in typescript types
// Game setup
type BoardPosition = [number, number];
type Space<Location extends BoardPosition, X extends BoardPosition[], Y extends BoardPosition[]> = Location extends X[number] ? 1 : Location extends Y[number] ? 2 : 0;
type Board<X extends BoardPosition[], Y extends BoardPosition[]> = [
[Space<[0,0], X, Y>,Space<[0,1], X, Y>,Space<[0,2], X, Y>],
[Space<[1,0], X, Y>,Space<[1,1], X, Y>,Space<[1,2], X, Y>],
[Space<[2,0], X, Y>,Space<[2,1], X, Y>,Space<[2,2], X, Y>]
@bolencki13
bolencki13 / match.ts
Created October 2, 2023 18:58
A match/when function set that deep partial matches values
type DeepPartial<T> = Partial<{
[key in keyof T]: DeepPartial<T[key]> | ((obj: T[key]) => boolean)
}> | ((obj: T) => boolean);
type MatchHandlerFunc<T> = (obj: T) => void;
interface IMatch<T> {
when (matchObj: DeepPartial<T>, cb?: MatchHandlerFunc<T>): IMatch<T>;
default (cb: MatchHandlerFunc<T>): IMatch<T>;
evaluate (): void;
@bolencki13
bolencki13 / endpoint-path-typestring.ts
Created August 15, 2023 18:14
An example outlining typescript and path parameter injection to strings.
/****************Data model type***************/
interface Endpoint<
T_Path extends string = string,
> {
path: T_Path
}
// Examples
@bolencki13
bolencki13 / AbortablePromise.ts
Last active November 15, 2023 02:04
Use the abort controller to stop the promise chain from continuing if abort is called.
export type MaybeAbortablePromise<T> = AbortablePromise<T> | Promise<T>;
export class AbortablePromise<T> implements PromiseLike<T> {
readonly promise: Promise<T>;
readonly abortController: AbortController;
static resolve<T>(value: T, abortController?: AbortController): AbortablePromise<T>;
static resolve(abortController?: AbortController): AbortablePromise<void>;
static resolve<T>(value: T | undefined, abortController: AbortController = new AbortController()) {
return new AbortablePromise(Promise.resolve(value), abortController)
type CompareNumbers<
T extends number,
U extends number,
A extends any[] = [],
> = T extends U
? 0 : A['length'] extends T
? -1
: A['length'] extends U
? 1
: CompareNumbers<T, U, ['a', ...A]>
@bolencki13
bolencki13 / PromiseBuilder.ts
Created June 10, 2022 19:08
Expose promise methods on a class that resolve type T
/**
* Exposes internal promise methods on the class that resolves a result of type T
*/
abstract class PromiseBuilder<T> implements Promise<T> {
/**
* Result to be returned when builder is resolved
*/
protected abstract _fetchResult(): Promise<T>
@bolencki13
bolencki13 / DefaultValueDecorator.ts
Last active June 3, 2022 19:30
The purpose of this decorator is to re-assign the default value when the incoming value is null or undefined.
/**
* The purpose of this decorator is to re-assign the default value when the incoming value is null or undefined.
* This can be modified to ignore null or undefined as an incoming value.
*/
type UseDefaultOptions = {
whenNull?: boolean;
whenUndefined?: boolean;
};
@bolencki13
bolencki13 / change-author.sh
Created May 18, 2019 11:43
Change author information in a git repo
#!/bin/sh
#
# This was taken from [here](https://help.github.com/en/articles/changing-author-info)
git filter-branch --env-filter '
OLD_EMAIL="your-old-email@example.com"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="your-correct-email@example.com"
@bolencki13
bolencki13 / Bash Shell
Created February 1, 2019 20:34
Remove all global npm packages
npm r -g $(node -e "console.log(process.argv.filter((e) => e !== '├──' && e !== '└──').filter((_, i) => i > 1).map((e) => e.split('@')[0]).filter((e) => e !== 'npm').join(' '))" $(npm list -g -depth 0))