Skip to content

Instantly share code, notes, and snippets.

View vnphanquang's full-sized avatar
😴
Sleep Coding

Quang Phan vnphanquang

😴
Sleep Coding
View GitHub Profile
@vnphanquang
vnphanquang / fluid-calc.js
Last active April 29, 2024 04:39
Fluid Calculator (for Typography)
/**
* @public
* @typedef {'rem' | 'px'} Unit
*/
/**
* @public
* @typedef {`${number}${Unit}`} Length
*/
#!/usr/bin/env fish
set --local count $argv[1]
set --local percentage $(math 100 / $count)
for i in (seq 0 $(math $count - 1))
set --local offset $(math $percentage / 100 \* $i)
magick image.png -gravity North -crop "100%x$percentage%+0+%[fx:h*$offset]" "cropped-$i".jpg
end
@vnphanquang
vnphanquang / snapshot.ts
Created August 11, 2023 10:19
batching for snapshot feature in svelte-kit
/* eslint-disable @typescript-eslint/no-explicit-any */
import { getContext, setContext } from 'svelte';
type Snapshot<T = any> = {
capture: () => T;
restore: (value: T) => void;
};
export function provideSnapshot() {
const snapshots: Record<string, Snapshot> = {};
@vnphanquang
vnphanquang / GridCollapsible.svelte
Created July 27, 2023 07:23
Dropdown with vanilla CSS transition
<script lang="ts">
export let open = false;
let cls ='';
export { cls as class };
</script>
<div class="grid-collapsible {cls}" data-open={open.toString()}>
<div class="grid-collapsible-content">
<slot />
</div>
@vnphanquang
vnphanquang / zod-i18n-validation.ts
Created May 25, 2023 03:04
Validation of i18n translation json using zod
// https://zod.dev/
import { z } from 'zod';
import ja from './ja.json';
import en from './en.json';
type RecursiveStringRecord = {
[key: string]: string | RecursiveStringRecord;
}
@vnphanquang
vnphanquang / one_hundred_prisioners_riddle.py
Created August 6, 2022 10:15
One Hundred Prisoners Riddle
# idea from Vertasium video: https://www.youtube.com/watch?v=iSNsgj1OCLA
from typing import Callable
import numpy as np
import random
import matplotlib.pyplot as plt
from matplotlib.ticker import PercentFormatter
def no_strategy(number: int, boxes: list[int]) -> bool:
open_boxes = random.sample(boxes, int(len(boxes) / 2))
type DeepObject<T> = {
[key: string]: T | DeepObject<T>;
};