Skip to content

Instantly share code, notes, and snippets.

View antfu's full-sized avatar

Anthony Fu antfu

View GitHub Profile
@antfu
antfu / git.sh
Created July 16, 2020 03:03
Git remove files from all history
git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch example.js'
type Proxy = any[]
type Pop<T> = T extends [infer A, ...infer B] ? B : never
type Push<T> = T extends [...infer B] ? [any, ...B] : never
type Add<T extends Proxy, V extends Proxy> = [...T, ...V]
type Double<T extends Proxy> = [...T, ...T]
type Get<A extends Proxy> = A['length']
type AddOne<A extends Proxy> = Push<A>
type SubOne<A extends Proxy> = Pop<A>
import { UnionToIntersection } from 'utility-types';
type String<T> = T extends string ? T : any
type Keys<T, Field extends keyof T> =
T extends { [P in Field]: infer A }
? { [P in String<A>]: T }
: never
type LookupTable<Union, key extends keyof Union> = UnionToIntersection<Keys<Union, key>>
type Unexact<T> =
T extends null
? null
: T extends undefined
? undefined
: T extends number
? number
: T extends boolean
? boolean
: T extends string
@antfu
antfu / sqlchemy_declarative_base_mixin.py
Created September 2, 2016 10:08
[Python|SqlAlchemy] as_dict for SqlAlchemy declarative base
from sqlalchemy.ext.declarative import declarative_base
class Mixin:
def as_dict(self):
return {c.name: getattr(self, c.name) for c in self.__table__.columns}
def as_clear_dict(self):
_dict = {}
for c in self.__table__.columns:
if c.foreign_keys:
continue
@antfu
antfu / swap.md
Created April 11, 2019 22:45
Create swap
function closest(num, arr) {
let idx = 0
let curr = arr[idx]
let diff = Math.abs(num - curr);
for (var val = 0; val < arr.length; val++) {
var newdiff = Math.abs(num - arr[val])
if (newdiff < diff) {
diff = newdiff
curr = arr[val]
idx = val
@antfu
antfu / hotel.vbs
Created July 1, 2017 18:27
Hotel startup script
CreateObject("Wscript.Shell").Run "cmd /c """"C:\Program Files\nodejs\node.exe"" ""C:\Users\Antho\AppData\Roaming\npm\node_modules\hotel\lib\daemon"" > ""C:\Users\Antho\.hotel\daemon.log""""", 0, true
@antfu
antfu / MathConverter.cs
Created March 23, 2017 17:30
[WPF] BindingMathConverter
// Does a math equation on the bound value.
// Use @VALUE in your mathEquation as a substitute for bound value
// Operator order is parenthesis first, then Left-To-Right (no operator precedence)
public class MathConverter : IValueConverter
{
private static readonly char[] _allOperators = new[] { '+', '-', '*', '/', '%', '(', ')' };
private static readonly List<string> _grouping = new List<string> { "(", ")" };
private static readonly List<string> _operators = new List<string> { "+", "-", "*", "/", "%" };