Skip to content

Instantly share code, notes, and snippets.

@YouMinTW
YouMinTW / changeCase.ts
Last active February 14, 2023 17:24
Change case from snake to came
// typescript version is 4.6.3
// type CamelCase<S extends string> = S extends `${infer P1}_${infer P2}${infer P3}`
// ? `${Lowercase<P1>}${Uppercase<P2>}${CamelCase<P3>}`
// : Lowercase<S>
// type CamelCase<S extends string> = S extends `${infer T}_${infer U}` ? `${T}${Capitalize<CamelCase<U>>}` : S;
// import {CamelCase} from 'type-fest'
// Opaque reference from: https://blog.beraliv.dev/2021-05-07-opaque-type-in-typescript
import { useRef, useEffect } from "react";
function useWhyDidYouUpdate(name, props) {
// Get a mutable ref object where we can store props ...
// ... for comparison next time this hook runs.
const previousProps = useRef();
useEffect(() => {
if (previousProps.current) {
// Get all keys from previous and current props
const allKeys = Object.keys({ ...previousProps.current, ...props });
// Use this object to keep track of changed props
@YouMinTW
YouMinTW / machine.js
Created October 8, 2021 17:13
Generated by XState Viz: https://xstate.js.org/viz
const doorMachinceConfig = {
id: "door",
initial: "關著",
states: {
關著: {
on: { 開門: "開了" }
},
開了: {
on: {
@YouMinTW
YouMinTW / machine.js
Created October 8, 2021 16:52
Generated by XState Viz: https://xstate.js.org/viz
const inputMachine = Machine({
id: "inputMachine_v2",
type: "parallel",
states: {
enabled: {
initial: "enabled",
states: {
disabled: {
@YouMinTW
YouMinTW / machine.js
Last active October 8, 2021 16:48
Generated by XState Viz: https://xstate.js.org/viz
const inputMachine = Machine({
id: "inputMachine_v1",
type: "parallel",
states: {
valid: {
initial: "valid",
states: {
invalid: {
@YouMinTW
YouMinTW / machine.js
Last active October 8, 2021 13:18
Generated by XState Viz: https://xstate.js.org/viz
const parallelMachine = Machine({
id: "myParallelMachineId",
type: "parallel",
states: {
state1: {
initial: "state1-1",
states: {
"state1-1": {
on: {
@YouMinTW
YouMinTW / news-version-1.js
Created October 4, 2021 16:53
news version-1
import { createMachine } from 'xstate';
const machineConfig = {
id: 'news-version-1',
initial: "draft",
states: {
draft: {
on: {
REVIEW: {
target: "reviewing",
@YouMinTW
YouMinTW / machine.js
Created September 11, 2021 19:13
Generated by XState Viz: https://xstate.js.org/viz
const doorMachine = Machine({
// Machine identifier
id: "door",
// Initial state
initial: "關著",
// State definitions
states: {
關著: {
type Simplify<T> = T extends Function
? T
: T extends Record<string, any>
? { [K in keyof T]: Simplify<T[K]> }
: T;
type Author = {
lastName: string;
firstName: string;
};
type Example<T> = T extends Record<string, any> ? keyof T : T;
// type T1 = number | keyof string[]
type T1 = Example<string[]>;
// type T2 = "a" | "b"
type T2 = Example<{ a: number; b: string }>;
// type T3 = boolean
type T3 = Example<boolean>;