Skip to content

Instantly share code, notes, and snippets.

View babakness's full-sized avatar

Babak B. babakness

View GitHub Profile
@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,

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 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 / 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 }
@babakness
babakness / sample-pipe.ts
Created October 4, 2018 03:32
Sample pipe function using overloads in TypeScript
// 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;
@babakness
babakness / simple-flatten-type.ts
Last active August 6, 2018 16:00
Simple Flatten Type
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;