Skip to content

Instantly share code, notes, and snippets.

@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))
@bolencki13
bolencki13 / BTOTransparentViewController.h
Created October 31, 2017 20:16
Transparent View Controller
//
// BTOTransparentViewController.m
//
// Created by Brian Olencki on 10/31/17.
//
#import "BTOTransparentViewController.h"
@interface BTOTransparentViewController ()
@bolencki13
bolencki13 / gist:243420f68663664d06288dd8c6bf773f
Created August 11, 2017 15:26
Regex Password Validation
/*
Valid password condition:
• At least 8 characters long
• At least one upper-case letter
• At least one lower-case letter
• At least one numeric digit
*/
((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])){8,}