Skip to content

Instantly share code, notes, and snippets.

View huozhi's full-sized avatar
🎈

Jiachi Liu huozhi

🎈
View GitHub Profile
@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,
})
@huozhi
huozhi / readOnDrop.js
Created January 24, 2018 06:23
read content when drop file with drag events
const readOnDrop = (element, onRead) => {
element.addEventListener('dragover', ev => ev.preventDefault())
element.addEventListener('drop', ev => {
ev.preventDefault()
const dt = ev.dataTransfer
const file = dt.files[0]
const reader = new FileReader()
reader.onload = event => onRead(event.target.result)
reader.readAsText(file)
})
@huozhi
huozhi / ssl.sh
Created January 16, 2018 03:13
generate ssl keys for localhost
openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout key.pem -out cert.pem
# only enter the Common Name: your ip address or your domain name