Skip to content

Instantly share code, notes, and snippets.

Avatar

Joe Maffei joemaffei

View GitHub Profile
@joemaffei
joemaffei / NullableToOptional.ts
Last active Sep 19, 2022
Make optional any properties whose types that have a union with null
View NullableToOptional.ts
/**
* 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 / form-data.ts
Last active Sep 7, 2022
Convert object to FormData
View form-data.ts
/**
* 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 / pick.ts
Last active Aug 11, 2022
Array.map callback for returning partial objects
View pick.ts
/**
* 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 / useState.ts
Created Jul 25, 2022
React's useState in Vue
View useState.ts
// 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 / object-level-metadata.js
Created May 10, 2022
Object with root-level metadata
View object-level-metadata.js
/**
* 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.bat
Last active Nov 28, 2019
Delete branches merged to dev (windows terminal)
View delete-merged-branches.bat
REM Loop through
REM (list of branches merged to dev, filter out dev and master, without leading whitespaces)
REM and delete each branch
FOR /F %i IN ('git branch --merged dev --list "[!dev|master]*" --format "%(refname:short)"') DO git branch -d %i
@joemaffei
joemaffei / delete-merged-branches.ps1
Last active Nov 28, 2019
Delete branches merged to dev (powershell)
View delete-merged-branches.ps1
# 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 / delete-merged-branches.sh
Last active May 10, 2022
Delete branches merged to dev (bash)
View delete-merged-branches.sh
# 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
View machine.js
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
@joemaffei
joemaffei / refactoring-reducer-helper.js
Last active Sep 20, 2019
Refactoring React: reducer helper function
View refactoring-reducer-helper.js
// Original
function clearTempStatuses(state) {
if (state == null) return [];
return state.map((quote) => {
const options = quote.options.map((option) => {
const { tempStatus: optionTempStatus, ...optionWithoutTempStatus } = option;
if (optionTempStatus) {
return { ...optionWithoutTempStatus, status: optionTempStatus };
}
return optionWithoutTempStatus;