Skip to content

Instantly share code, notes, and snippets.

View itsMapleLeaf's full-sized avatar

itsMapleLeaf

View GitHub Profile
@itsMapleLeaf
itsMapleLeaf / arrow-fns.js
Created November 7, 2020 19:44
arrow function explanation
// this has a block, needs a return statement
const fn1 = () => {
return 1
}
// this does the same as the above,
// if you're returning something without other stuff, no need for curly braces
const fn2 = () => 1
// adding parentheses is just a stylistic choice, it does the same thing
@itsMapleLeaf
itsMapleLeaf / recommendations.md
Created November 5, 2020 20:30
frontend build tool recommendations!

I believe zero-config with the option for minimal extensibility is the way forward for modern build tooling. All tools listed here have typescript support, since that's basically a requirement for me nowadays

1. Vite

Pros:

  • Vite is very fast due to its reliance on browser ES modules at dev time
  • Uses esbuild under the hood, very fast transpiling
  • Uses rollup to make very light production bundles
  • The out-of-the-box config works well for most frontend frameworks, including React, Vue, and Svelte
  • Config is minimal but extensive enough to support pretty much whatever you need to do
@itsMapleLeaf
itsMapleLeaf / example.js
Last active May 2, 2021 06:56
passing state setters
const [media, setMedia] = useState([])
// no good
// the media state is in the parent,
// but we have to check here _and_ in the child to see how media gets updated,
// and MediaUploader is tightly coupled to this component's state
<MediaUploader
media={media}
setMedia={setMedia}
/>
{
"quoteProps": "consistent",
"semi": false,
"trailingComma": "all",
"useTabs": true
}
@itsMapleLeaf
itsMapleLeaf / example.js
Last active October 23, 2020 01:58
websockets with hooks
const socketRef = useRef();
const [connected, setConnected] = useState(false);
// this ref based off of the "connected" state lets us read it in `connect`
// without having to declare it as a dependency
const connectedRef = useRef(connected);
useEffect(() => {
connectedRef.current = connected;
});
@itsMapleLeaf
itsMapleLeaf / useDict.ts
Last active September 28, 2020 17:08
useDict
import { useCallback, useState } from "react"
const emptyDict = {}
export default function useDict<T>(
initialItems: Record<string, T | undefined> = emptyDict,
) {
const [items, setItems] = useState(initialItems)
const add = useCallback(
@itsMapleLeaf
itsMapleLeaf / validation.ts
Last active July 6, 2022 20:05
validation take 2
type ValidatorResult<T> = { type: "valid" } | { type: "invalid"; error: string }
type ValidateFn<T> = (value: unknown) => ValidatorResult<T>
type Validator<T = unknown> = {
validate: ValidateFn<T>
parse: (value: unknown) => T
is: (value: unknown) => value is T
}
type Validator<T = unknown> = {
parse: (value: unknown) => T
is: (value: unknown) => value is T
}
type ValidatorType<V> = V extends Validator<infer T> ? T : never
type ValueOf<T> = T extends readonly (infer V)[] ? V : T[keyof T]
const raise = (error: string): never => {
@itsMapleLeaf
itsMapleLeaf / atom.ts
Created March 8, 2020 00:47
state atom
import { useEffect, useState } from "react"
export type Atom<T> = {
set(newValue: T): void
get(): T
listen(listener: (value: T) => void): () => void
}
export function createAtom<T>(initialValue: T): Atom<T> {
let currentValue = initialValue
@itsMapleLeaf
itsMapleLeaf / helpers.ts
Created February 6, 2020 03:05
functional emotion lol
import { Interpolation } from "@emotion/core"
import { css } from "./styled"
type SizeUnit =
| 0
| 1
| 2
| 3
| 4
| 5