Skip to content

Instantly share code, notes, and snippets.

View babakness's full-sized avatar

Babak B. babakness

View GitHub Profile
@babakness
babakness / disable-back-gesture-on-mac-by-browser.md
Last active February 20, 2024 19:17
A list of commands to disable the back gesture for Chrome, FireFox, Brave, and Safari

When working with online editor, the back gesture on a Mac can become very frustrating. Here are some helpful commands disable the back gesture:

# Disable back gesture in specific apps

# Chrome
defaults write com.google.Chrome AppleEnableSwipeNavigateWithScrolls -bool FALSE
# FireFox
defaults write org.mozilla.firefox AppleEnableSwipeNavigateWithScrolls -bool FALSE
# Brave
@babakness
babakness / use-interval.ts
Created February 4, 2019 18:54
Typescript version of Dan Abramov's useInterval requiring non-null value for delay
import * as React from 'react'
const { useState, useEffect, useRef } = React
type IntervalFunction = () => ( unknown | void )
function useInterval( callback: IntervalFunction, delay: number ) {
const savedCallback = useRef<IntervalFunction| null>( null )
@babakness
babakness / caselessDictionary.py
Created October 16, 2012 18:46 — forked from bloomonkey/caselessDictionary.py
A Python dictionary sub-class that is case-insensitive when searching, but also preserves the keys as inserted.
class CaselessDictionary(dict):
"""Dictionary that enables case insensitive searching while preserving case sensitivity
when keys are listed, ie, via keys() or items() methods.
Works by storing a lowercase version of the key as the new key and stores the original key-value
pair as the key's value (values become dictionaries)."""
def __init__(self, initval={}):
if isinstance(initval, dict):
for key, value in initval.iteritems():
@babakness
babakness / stdlib.ts
Created June 30, 2018 05:35 — forked from KiaraGrouwstra/stdlib.ts
Type-level standard library for TypeScript
// NOTE: code now moved to https://github.com/tycho01/typical
// older revision left here, but it no longer runs well in TS Playground
export type Obj<T> = { [k: string]: T };
export type NumObj<T> = { [k: number]: T };
// export type List = ArrayLike; // no unapplied generic types :(
export type List<T> = ArrayLike<T>;
// progress: https://github.com/Microsoft/TypeScript/issues/16392
export function force<T, V extends T>() {}

Flattening Arbitrarily Nested Arrays in JavaScript

Introduction

JavaScript's immense flexibility affords the creation of arrays nested arbitrarily. Each element in an array can be of any type and of varying depths. A function that completely flatten various nested arrays can be a useful tool.

There are two general approaches to creating this flattening function, recursion and iteration, and many variations therein. As we'll see, the most elegant looking solutions to this kind of problem tend to be recursive. Alas, recursion in JavaScript is not well optimized. On the other hand, iterative solutions in JavaScript can be more complex and require extra care with regards to mutations.

Recursion

@babakness
babakness / .tmux.conf
Created March 17, 2020 20:47
My Tmux
# Use C-Space instead of C-b for tmux. C-Space C-Space to get "C-Space".
set-option -g default-shell /usr/local/bin/fish
set-option -g history-limit 100000
unbind-key C-b
set -g prefix C-Space
bind-key C-Space send-prefix
# Set the default terminal mode to 256color mode
set -g default-terminal "screen-256color"
@babakness
babakness / tsconfig.json
Created December 31, 2019 17:55
Typical tsconfig.json
{
"compilerOptions": {
"baseUrl": "src",
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
@babakness
babakness / XorExtend.ts
Created December 24, 2018 19:35
generic type and typed function that conditionally extend an object if the keys / value type of the first object are not in the second
type XorExtend<T extends {
[K in keyof T]: K extends keyof A
? A[K] extends T[K]
? never
: T[K]
: T[K]
}, A extends object,> = T & A
declare function xorExtend<O, P extends { [K in keyof P]: K extends keyof O
? O[K] extends P[K]
@babakness
babakness / no-excess-partials.ts
Last active November 25, 2018 01:27
A collection of partial types which do not allow for excess properties.
// Inspired by https://stackoverflow.com/users/2887218/jcalz
export type NoExcessStrictPartial<O, P> = {
[K in keyof P]: K extends keyof O ? O[K] : never
}
export type NoExcessDeepPartialValueFlexType<O, P> = {
[K in keyof P]:
K extends keyof O
? NoExcessDeepPartialValueFlexType<O[K],P[K]>
: never
@babakness
babakness / Immutable Arrays, Object in TypeScript
Created October 14, 2018 06:12
staticImmutableExample.ts
type ReadonlyObj<T> = { readonly [K in string]: T }
const foo: ReadonlyArray<T> = [1,2,3]
const bar: ReadonlyObj = { a: 1, b: 2, c: 3 }