Skip to content

Instantly share code, notes, and snippets.

View joemaffei's full-sized avatar

Joe Maffei joemaffei

View GitHub Profile
@joemaffei
joemaffei / delete-merged-branches.ps1
Last active November 28, 2019 18:46
Delete branches merged to dev (powershell)
# 1. get list of branches merged to dev, filter out dev and master, without spaces in the beginning
# 2. delete each branch
git branch --merged dev --list "[!dev|master]*" --format "%(refname:short)" | ForEach-Object { git branch $_ -d }
@joemaffei
joemaffei / fibonacci.js
Created February 19, 2019 05:27
Calculate nth Fibonacci number
function fibonacci(n) {
return Math.round((Math.pow(1.618033988749895, n) - Math.pow(-0.618033988749895, n)) / 2.23606797749979);
}
@joemaffei
joemaffei / object-level-metadata.js
Created May 10, 2022 20:21
Object with root-level metadata
/**
* Getters and setters are not enumerable and, obviously, neither are private fields.
*/
class MetadataObject extends Object {
#metadata = undefined;
get metadata() { return this.#metadata; }
set metadata(value) { this.#metadata; }
}
@joemaffei
joemaffei / delete-merged-branches.sh
Last active May 10, 2022 20:27
Delete branches merged to dev (bash)
# 1. get list of branches merged to dev, filter out dev and master
# 2. delete each branch
git branch --merged dev --list "[\!dev|master]*" | xargs -n 1 git branch -d
@joemaffei
joemaffei / useState.ts
Created July 25, 2022 18:07
React's useState in Vue
// inspired by https://markus.oberlehner.net/blog/usestate-and-usereducer-with-the-vue-3-composition-api/
import { readonly, ref, UnwrapRef } from "vue";
// "corporate" version
export function useState<T>(initialState: T | undefined) {
type InitialState = T | undefined;
type State = UnwrapRef<T> | undefined;
type StateFn = (currentState: State) => State;
@joemaffei
joemaffei / pick.ts
Last active August 11, 2022 17:24
Array.map callback for returning partial objects
/**
* Use this as the callback function for an Array.map that returns a partial
* object. It's the functional equivalent of the Pick type.
*
* @example
* const arr = [{a: 1, b: 1, c: 1}, {a: 2, b: 2, c: 2}]
* const oldWay = arr.map(({ a, c }) => ({ a, c }));
* const newWay = arr.map(pick("a", "c"));
*/
function pick<T extends object, K extends keyof T>(
@joemaffei
joemaffei / form-data.ts
Last active September 7, 2022 20:37
Convert object to FormData
/**
* I started building this, then came across a more robust solution:
* {@link https://github.com/therealparmesh/object-to-formdata}
*/
import { isPlainObject } from "is-plain-object";
type Value = string | Blob | number | boolean | null | undefined | Date;
function convert<T extends Value>(value: T): string | Blob {
@joemaffei
joemaffei / NullableToOptional.ts
Last active September 19, 2022 16:26
Make optional any properties whose types that have a union with null
/**
* Inspired by this article by Rafael Bardini:
* {@link https://rbardini.com/making-optional-properties-nullable-typescript/}
*/
type NullUnion<T> = T | null;
// type NullableProps<T> = {
// [K in keyof T]: NullUnion<T[K]> extends T[K] ? K : never;
// }[keyof T];
@joemaffei
joemaffei / binary-enum.ts
Created November 7, 2022 21:40
Iterate through enum keys and values; test binary digits
enum BinaryEnum {
One = 0b0001,
Two = 0b0010,
Four = 0b0100,
Eight = 0b1000,
}
const keys = Object.keys(BinaryEnum).filter(x => isNaN(Number(x))) as (keyof typeof BinaryEnum)[];
const values = Object.values(BinaryEnum).filter(x => typeof x === 'number') as number[];
@joemaffei
joemaffei / log-focused-element.js
Created November 7, 2022 21:44
Log focused element
document.addEventListener('focusin', (e) => console.log(e.target), true);