Skip to content

Instantly share code, notes, and snippets.

Avatar

Babak B. babakness

View GitHub Profile
View .tmux.conf
# 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 Dec 31, 2019
Typical tsconfig.json
View tsconfig.json
{
"compilerOptions": {
"baseUrl": "src",
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
View flatten-discussion.md

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 / disable-back-gesture-on-mac-by-browser.md
Last active Oct 4, 2021
A list of commands to disable the back gesture for Chrome, FireFox, Brave, and Safari
View disable-back-gesture-on-mac-by-browser.md

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 Feb 4, 2019
Typescript version of Dan Abramov's useInterval requiring non-null value for delay
View use-interval.ts
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 / XorExtend.ts
Created Dec 24, 2018
generic type and typed function that conditionally extend an object if the keys / value type of the first object are not in the second
View XorExtend.ts
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 Nov 25, 2018
A collection of partial types which do not allow for excess properties.
View no-excess-partials.ts
// 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
View Immutable Arrays, Object in TypeScript
type ReadonlyObj<T> = { readonly [K in string]: T }
const foo: ReadonlyArray<T> = [1,2,3]
const bar: ReadonlyObj = { a: 1, b: 2, c: 3 }
@babakness
babakness / sample-pipe.ts
Created Oct 4, 2018
Sample pipe function using overloads in TypeScript
View sample-pipe.ts
// Example taken from @type/ramda at the time of this writing.
pipe<T1>(fn0: () => T1): () => T1;
pipe<V0, T1>(fn0: (x0: V0) => T1): (x0: V0) => T1;
pipe<V0, V1, T1>(fn0: (x0: V0, x1: V1) => T1): (x0: V0, x1: V1) => T1;
pipe<V0, V1, V2, T1>(fn0: (x0: V0, x1: V1, x2: V2) => T1): (x0: V0, x1: V1, x2: V2) => T1;
pipe<T1, T2>(fn0: () => T1, fn1: (x: T1) => T2): () => T2;
pipe<V0, T1, T2>(fn0: (x0: V0) => T1, fn1: (x: T1) => T2): (x0: V0) => T2;
pipe<V0, V1, T1, T2>(fn0: (x0: V0, x1: V1) => T1, fn1: (x: T1) => T2): (x0: V0, x1: V1) => T2;
View simple-flatten-type.ts
type Flatten<T> = T extends any[] ? T[number] : T;
// If you want to flatten no lower than one level
type FlattenArray<T> = T extends any[][] ? T[number] : T;