Skip to content

Instantly share code, notes, and snippets.

import { onMount } from "svelte";
const useLocalStorage = <T>(key: string, initialValue: T) => {
let value = $state<T>(initialValue);
onMount(() => {
const currentValue = localStorage.getItem(key);
if (currentValue) value = JSON.parse(currentValue);
});
@thanhnguyen2187
thanhnguyen2187 / cache-middleware.ts
Created September 7, 2025 10:31
Hono in-memory cache middleware
const CACHE_TIMEOUT_MS = 5 * 60 * 1000;
const cacheData = new Map<string, unknown>();
const cacheMiddleware = createMiddleware(async (c, next) => {
if (c.req.method !== "GET") {
await next();
return;
}
const key = c.req.url;
if (cacheData.has(key)) {
// biome-ignore lint/style/noNonNullAssertion: checked null above
@thanhnguyen2187
thanhnguyen2187 / vietnamese-detection.ts
Created July 26, 2025 15:37
A naive algorithm to detect if the input text is Vietnamese
const specialChars = `
á à ả ã ạ
ă ắ ằ ẳ ẵ ặ
â ấ ầ ẩ ẫ ậ
ó ò ỏ õ ọ
ơ ớ ờ ở ỡ ợ
ô ố ồ ổ ỗ ộ
ú ù ủ ũ ụ
ư ứ ừ ử ữ ự
í ì ỉ ĩ ị
@thanhnguyen2187
thanhnguyen2187 / sample.janet
Created December 22, 2021 15:29
A sample Janet program to read a "special" csv file, convert the data into weighted options, and randomly pick one
(math/seedrandom (os/cryptorand 8))
(math/random)
(defn read-raw-text
[path]
(-> path
(file/open :r)
(file/read :all)))
(defn read-special-csv-lines
"""
Write a recursive function named `remove_odd_digits` that accepts an
integer parameter n and returns the integer formed by removing the odd digits
from n. For example, the call of `remove_odd_digits(6342118) should return
6248.`
"""
def remove_odd_digits(n: int) -> int:
if (
n == 0 or
"""
Write a recursive function that prints a solution to the classic Towers of
Hanoi puzzle. Your function should accept three integer parameters representing
the number of disks, the starting peg number, and ending peg number. Your
function should print the solution to the game to move from the given start peg
to the given end peg. For example, the call of `hanoi(3, 1, 3)` should print
the folowing output to move the three pegs from peg #1 to peg #3.
```
move disk 1 from peg 1 to peg 3
" source .vimrc
set number " nu
set relativenumber " rnu
set scrolloff=999 " so
set sidescrolloff=999 " siso
set nowrap
set nohlsearch " nohl
set textwidth=80 " tw
set colorcolumn=80 " cc
""""""""""""
" Vim Plug "
""""""""""""
call plug#begin('~/.vim/bundle')
"""""""""""""""""""
" Auto Completion "
"""""""""""""""""""
" Plug 'Valloric/YouCompleteMe'