Skip to content

Instantly share code, notes, and snippets.

View huozhi's full-sized avatar
🎈

Jiachi Liu huozhi

🎈
View GitHub Profile
@huozhi
huozhi / lazy.ts
Created January 12, 2025 16:58
lazy load
// Lazy load a value and cache it from the `getter`
function lazy<T>(getter: () => T): { value: T } {
return {
get value() {
const evaluated = getter()
Object.defineProperty(this, 'value', { value: evaluated })
return evaluated
}
}
@huozhi
huozhi / .zshrc
Created January 12, 2025 14:22
Enable tmux in iterm2 only and not in other terminals
# Check if running in iTerm2 and `tmux` is available
if [[ "$TERM_PROGRAM" == "iTerm.app" ]] && command -v tmux &>/dev/null; then
# Start a new tmux session or attach to an existing one
if [[ -z "$TMUX" ]]; then
tmux attach || tmux new
fi
fi
@huozhi
huozhi / checkout-react-compiler-with-nextjs.sh
Created May 16, 2024 23:47
checkout-react-compiler-with-nextjs.sh
pnpm add next@canary react@beta react-dom@beta
pnpm add -D babel-plugin-react-compiler
echo "module.exports = {
experimental: {
reactCompiler: true
},
}" > next.config.js
@huozhi
huozhi / ts-check.mjs
Created December 3, 2023 16:04
validate types of package
#!/usr/bin/env node
import * as core from '@arethetypeswrong/core'
import { groupProblemsByKind } from '@arethetypeswrong/core/utils'
import { execSync } from 'child_process'
import { readFile, stat, unlink } from 'fs/promises'
import path from 'path'
const problemFlags = {
NoResolution: 'no-resolution',
UntypedResolution: 'untyped-resolution',
@huozhi
huozhi / linear-graident-console-log.ts
Created October 23, 2023 00:47
create linear gradient color
const linearGradientText = (
text: string,
startColor: number[],
endColor: number[]
) => {
let gradient = ''
for (let i = 0; i < text.length; i++) {
const r = Math.floor(
startColor[0] + (i / (text.length - 1)) * (endColor[0] - startColor[0])
@huozhi
huozhi / get-keys.ts
Last active January 16, 2023 17:37
typescript cheat sheet
type KeysOfUnion<T> = T extends T ? keyof T : never
@huozhi
huozhi / insert-text-to-file.sh
Last active September 19, 2022 20:58
inesert text to files
# https://stackoverflow.com/questions/61546603/bash-find-file-and-insert-text-in-beginning-of-empty-and-non-empty-files
# insert `'client'\n` to all .client.tsx files
find . -type f -name '*.client.tsx' -print0 | while IFS= read -rd '' file; do ed -s "$file" <<< $'0a\n\'client\'\n\n.\nw\nq'; done
# rename files
ls | grep \.png$ | sed 'p;s/\.png/\.jpg/' | xargs -n2 mv
@huozhi
huozhi / filter-pack.sh
Created July 9, 2021 05:55
filter git pack files
git rev-list --objects --all \
| grep "$(git verify-pack -v .git/objects/pack/*.idx \
| sort -k 3 -n \
| tail -10 \
| awk '{print$1}')"
@huozhi
huozhi / webpack5-compile-twilio.js
Created October 24, 2020 04:20
webpack5 compile integration test of twilio.js
const path = require('path');
const webpack = require('webpack');
const fs = require('fs');
// Based on the NodeWatchFileSystem class at:
// - https://github.com/webpack/webpack/blob/master/lib/node/NodeWatchFileSystem.js
// which in turn exposes:
// - https://www.npmjs.com/package/watchpack
class CustomWatchFileSystem {
@huozhi
huozhi / simulate_mouse_action.js
Created March 31, 2019 14:14
simulate click and hover mouse action on web with js
const mouseEventOf = (eventType) => (element, x, y) => {
const rect = element.getBoundingClientRect()
const event = new MouseEvent(eventType, {
view: window,
bubbles: true,
cancelable: true,
clientX: rect.left + x,
clientY: rect.top + y,
})