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 / 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 / control-props.js
Created December 17, 2021 11:47
React Patterns: Control Props
// Control Props
import * as React from 'react'
import warning from 'warning'
import {Switch} from '../switch'
const mergeAll =
(...fns) =>
(...args) =>
fns.forEach(fn => fn?.(...args))
@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 / 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>
type AppleLiteral = 'Apple'
let appleName: AppleLiteral = 'Apple'
type LiteralIsStringType<Type> = Type extends string
? string
: Type extends number
? number
: Type extends boolean
? boolean
@Neo42
Neo42 / context-module-functions.js
Last active January 8, 2022 14:20
React Patterns: context module functions
// Context Module Functions
import * as React from 'react'
import {dequal} from 'dequal'
import * as userClient from '../user-client'
import {useAuth} from '../auth-context'
const UserContext = React.createContext()
UserContext.displayName = 'UserContext'
@Neo42
Neo42 / command.js
Last active January 17, 2022 14:56
Design patterns in JavaScript
class OrderManager {
orders = new Set()
execute(command, ...args) {
return command.run(this.orders, ...args)
}
}
class Command {
constructor(steps) {
@Neo42
Neo42 / promise.js
Last active January 18, 2022 10:43
Simple promise implementation.
const states = {
PENDING: 'PENDING',
FULFILLED: 'FULFILLED',
REJECTED: 'REJECTED',
}
const isThenable = (thing) => thing && typeof thing.then === 'function'
class MyPromise {
_state = states.PENDING
@Neo42
Neo42 / __webpack_require__.js
Created January 19, 2022 16:15
__webpack_require__
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ var cachedModule = __webpack_module_cache__[moduleId]
/******/ if (cachedModule !== undefined) {
/******/ return cachedModule.exports
/******/
}
/******/ // Create a new module (and put it into the cache)
/******/ var module = (__webpack_module_cache__[moduleId] = {
/******/ // no module.id needed
@Neo42
Neo42 / readme.md
Last active January 20, 2022 08:07
When to use code splitting (dynamic import) in webpack?
  1. Heavy library but not needed initially (threejs)
  2. Temporal components (that come and go or are conditionally loaded): tooltips, model, notification, dialog, page scrolling
  3. Routes