Skip to content

Instantly share code, notes, and snippets.

View ferdaber's full-sized avatar
🥩

Ferdy Budhidharma ferdaber

🥩
View GitHub Profile
@ferdaber
ferdaber / gist:eb4bd09ad88f65fa89ac7f7ff92fe341
Created February 9, 2019 02:02
strongly-typed-redux-2
type State = any
/**
* General action type
*/
export type Action<TType extends string = string, TPayload = any> = TPayload extends undefined
? {
type: TType
}
: {
@ferdaber
ferdaber / strongly-typed-redux.ts
Last active January 25, 2019 00:16
Strongly typed Redux
// definitions
import { Middleware } from 'redux'
export declare const dispatch: Dispatch
export type State = unknown
/**
* General action type, DO NOT USE AS A RETURN TYPE
@ferdaber
ferdaber / use-form.ts
Last active January 11, 2019 03:20
React Hooks
import { useReducer, ChangeEvent } from "react"
import { map } from "../util"
export type FieldSpec<T = any> = {
initialValue: T
validate?(value: T): boolean
}
export type FormAction<F, K extends keyof F = keyof F> = {
fieldName: K
@ferdaber
ferdaber / useful-types.ts
Last active December 2, 2021 18:56
Useful Types
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
type Diff<T, U> = Omit<T, Extract<keyof U, keyof T>>
// works because {} will extend a pick of an optional property
// i.e. {} extends { foo?: string } because it's potentially undefined
// abuses the fact that the compiler treats assignability specially for object types
type OptionalKeys<T> = { [K in keyof T]-?: {} extends Pick<T, K> ? K : never }[keyof T]
type RequiredKeys<T> = Exclude<keyof T, OptionalKeys<T>>
// in a weak type, the empty interface extends that type since all of its properties can be undefined
@ferdaber
ferdaber / lowestChild.js
Last active January 10, 2019 05:24
Programming interview questions
function getLowestChild(tree) {
if (!tree.children || !tree.children.length) {
return {
depth: 0,
node: tree.root,
}
}
const { depth, node } = tree.children.reduce(
(curLowest, subTree) => {
const nextLowest = getLowestChild(subTree)
@ferdaber
ferdaber / npm-unlink.md
Created April 7, 2018 15:56
How to unlink npm files at the global level
@ferdaber
ferdaber / xargs-notes.md
Created April 7, 2018 15:50
Notes on xargs usages

Useful flags

  • -t outputs what it's going to perform before performing it
  • -I replaces occurrences of the argument with whatever is piped into it

Piping multiple commands

cat file.txt | xargs sh -c "<command>; <commandtwo>"

Replace with arguments