Skip to content

Instantly share code, notes, and snippets.

View Neo42's full-sized avatar
:shipit:

Johnny (Hao) Jiang Neo42

:shipit:
View GitHub Profile
@Neo42
Neo42 / vis.sh
Created March 30, 2021 12:32
Code for changing one's own membership visibility in a github organization
curl \ 20:19:16
-H "Accept: application/vnd.github.v3+json" \
# replace :token below with your personal access token
-H "Authorization: token :token" \
-H "Content-Length: 0" \
-X PUT \
# replace ORGNAME with your organization name and USERNAME your username
https://api.github.com/orgs/ORGNAME/public_members/USERNAME
@Neo42
Neo42 / quickSort.js
Created May 4, 2021 15:42
Hoare's quicksort. More performant than Lomuto's.
const quickSort = (arr, low = 0, high = arr.length - 1) => {
if (low < high) {
const p = partition(arr, low, high)
quickSort(arr, low, p)
quickSort(arr, p + 1, high)
}
}
const partition = (arr, low, high) => {
const pivot = arr[Math.floor((high + low) / 2)]
let i = low
@Neo42
Neo42 / io.js
Last active May 7, 2021 04:05
IO monad in JS
const IO = effect =>
({
effect,
run() {
return this.effect()
},
map(fn) {
if (!fn ?? typeof fn !== 'function') {
throw new Error('.map requires a function argument.')
}
{
"semi": false,
"embeddedLanguageFormatting": "off",
"bracketSpacing": false,
"jsxBracketSameLine": true,
"proseWrap": "never",
"trailingComma": "all",
"singleQuote": true
}
@Neo42
Neo42 / useState-lazy-initialization.js
Last active December 6, 2021 15:34
useState lazy initialization by passing a callback function
const [name, setName] = React.useState(
someExpensiveComputation(props); // run on every render
)
const [state, setState] = useState(() => {
const initialState = someExpensiveComputation(props); // only run on the initial render
return initialState;
});
@Neo42
Neo42 / useState.js
Created December 14, 2021 20:11
`useState` implementation with `useReducer`
import React from "react";
const useStateReducer = (prevState, dispatchArg) =>
typeof dispatchArg === "function" ? dispatchArg(prevState) : dispatchArg;
const useStateInitializer = (initialArg) =>
typeof initialArg === "function" ? initialArg() : initialArg;
function useState(initialValue) {
return React.useReducer(useStateReducer, initialValue, useStateInitializer);
@Neo42
Neo42 / prop-collections.js
Created December 17, 2021 04:59
React Patterns: Prop Collections and Getters
// Prop Collections and Getters
// merge all passed functions into one
const mergeFn =
(...fns) =>
(...args) =>
fns.forEach(fn => fn?.(...args))
function useToggle() {
const [on, setOn] = React.useState(false)
@Neo42
Neo42 / generics.ts
Created January 7, 2022 16:53
Generics are to types as functions are to values.
function getFirstItem<Type>(list: Type[]): Type {
return list[0]
}
const item = getFirstItem([1])
const item2 = getFirstItem([''])
const item3 = getFirstItem([new Object()])
type Tree<Type> = {
value: Type
@Neo42
Neo42 / compound-components.js
Last active January 7, 2022 16:55
React Patterns: Compound Components
// Compound Components w/ `React.Children.map` & `React.cloneElement`
import * as React from 'react'
import {Switch} from '../switch'
function Toggle({children}) {
const [on, setOn] = React.useState(false)
const toggle = () => setOn(!on)
return React.Children.map(children, child =>
// tell if a child is a built-in DOM component
typeof child.type === 'string'
@Neo42
Neo42 / mapped-types-&-modifiers.ts
Last active January 8, 2022 06:47
Mapped types are the for...in loop for types.
interface Fruit {
readonly name?: string
readonly color?: string
readonly sweetness?: number
}
type Properties<Type> = keyof Type
type Values<Type> = Type[Properties<Type>]
type FruitProperties = Properties<Fruit>