Skip to content

Instantly share code, notes, and snippets.

View aleclarson's full-sized avatar

Alec Larson aleclarson

View GitHub Profile
@aleclarson
aleclarson / select_and_drop.php
Created August 12, 2018 21:30
WordPress script for dropping tables based on their name
$tables = $wpdb->get_results("
SELECT table_name FROM information_schema.tables WHERE table_name LIKE 'i9JAv_%';
");
$tables = array_map(function($tbl) {
return $tbl->table_name;
}, $tables);
$tables = implode(', ', $tables);
$wpdb->query("
DROP TABLE IF EXISTS $tables;
");
function path_relative($dir, $dest) {
$ds = DIRECTORY_SEPARATOR;
$dir = explode($ds, rtrim($dir, $ds));
$dest = explode($ds, $dest);
while ($dir && $dest && ($dir[0] == $dest[0])) {
array_shift($dir);
array_shift($dest);
}
return str_repeat('..'.$ds, count($dir)) . implode($ds, $dest);
}
@aleclarson
aleclarson / dlv.ts
Created September 25, 2018 23:22
Typings
declare module 'dlv' {
function dlv<T, K extends keyof T>(
obj: T | undefined,
key: K,
def?: T[K]
): T[K] | undefined
function dlv(obj: object | undefined, key: string | string[], def?: any): any
export default dlv
}
@aleclarson
aleclarson / object.ts
Created September 26, 2018 23:57
Object-related type helpers
/** Eliminate intersections */
type Id<T> = { [K in keyof T]: T[K] }
/** Get the value type of the given key (if defined) */
type Get<T extends object, P> = P extends keyof T ? UnPartial<T>[P] : never
/** Make all properties non-partial */
type UnPartial<T extends object> = { [K in keyof T]-?: T[K] }
@aleclarson
aleclarson / deepCompare.ts
Last active October 24, 2018 14:26
deepCompare
const { toString } = Object.prototype
/** Any object type */
type AnyObj = { [key: string]: any }
export function deepCompare(a: any, b: any): boolean {
// Strict equality
if (a === b) return true
// Some types get special treatment
@aleclarson
aleclarson / asyncReduce.ts
Created November 14, 2018 19:31
asyncReduce
export async function asyncReduce<U, T = any>(
arr: ReadonlyArray<T>,
iter: (
memo: U,
value: T,
index: number,
arr: ReadonlyArray<T>
) => U | Promise<U>,
startValue: U | Promise<U>
) {
@aleclarson
aleclarson / setup.sh
Last active December 24, 2022 22:30
macOS setup
export PATH="$HOME/dev/bin:$PATH"
mkdir ~/dev && cd $_
mkdir ./bin
# oh-my-zsh
sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
# zsh port of rupa/z
git clone https://github.com/agkozak/zsh-z $ZSH_CUSTOM/plugins/zsh-z --depth 1
{
"separateMinorPatch": true,
"patch": {
"automerge": true,
"automergeType": "branch"
}
}
# Create a directory and switch to it.
mkdir foo && ./$_
/** Trigger a compiler error when a value is _not_ an exact type. */
declare const exactType: <T, U extends T>(
draft?: U,
expected?: T
) => T extends U ? T : 1 & 0