Skip to content

Instantly share code, notes, and snippets.

View casperx's full-sized avatar

CasperX casperx

View GitHub Profile
@casperx
casperx / dom.ts
Created November 26, 2025 18:54
HTML Element constructor function
type ElementLike = string | Element;
type ElementChildren = ElementLike | Array<ElementLike> | Iterator<ElementLike>;
const svgTagNamesLookup = new Set([
'svg',
'g',
'defs',
'use',
'line',
@casperx
casperx / mijia.py
Last active December 20, 2024 20:03
Recover date & time and rename files recovered from Mijia dashcam
from typing import Callable, Iterable
import sys
import subprocess
import shutil
import re
import cv2 as cv
import numpy as np
import pytesseract as tess
@casperx
casperx / yield_chunk.py
Created October 14, 2024 05:34
Yield iterable in chunk of n
def chunker[T](seq: Iterator[T], size: int):
it = iter(seq)
while True:
chunk: list[T] = []
for _ in range(size):
try:
val = next(it)
@casperx
casperx / utf8.c
Created August 21, 2024 09:40
minimal UTF8 encoder and decoder.
#include <stdint.h>
#include <stdio.h>
uint8_t utf8_encode(uint32_t c, uint8_t *b) {
if (c < 0x80)
return *b = c, 1;
int8_t h;
uint8_t u;
for (h = 0xC0, u = 0; c & h; c >>= 6, h = h >> 1, ++u)
b[u] = 0x80 | (c & 0x3F);
@casperx
casperx / test.ts
Created June 9, 2024 12:26
Recursive Bezier Curve
export type Point = {
x: number
y: number
}
export const lerp = (min: number, max: number, r: number) => min + (max - min) * r
export const bezier = (ps: Array<Point>, t: number) => {
const resolve = (start: number, end: number): Point => {
if (end - start === 1) return ps[start]
@casperx
casperx / test.py
Created June 7, 2024 11:24
System call Emulation with async in Python
class SysCall(int):
def __new__(cls, i: int):
return super().__new__(cls, i)
def __await__(self):
return (yield self)
async def test3():
for i in range(7):
d = await SysCall(i)
print(f'-- {d}')
@casperx
casperx / show.c
Last active June 20, 2023 03:35
binary number representation
#include <stdio.h> /* scanf, printf */
void printb( long v, unsigned char w, unsigned char f ) {
unsigned long m;
unsigned char b, x;
m = 1u << w - 1;
while ( m ) {
b = !!( v & m );
x = b != f;
putchar( '0' + x );
@casperx
casperx / railfence.js
Last active July 12, 2023 17:23
Rail fence Codec
const s = ''
const row = 4
const pr = 2 * (row - 1)
const res = []
for (let i = 0, c = 0; i < row; i += 1) {
const pr1 = 2 * i
const pr2 = pr - pr1
for (let j = i, t = true; j < s.length; c += 1, j += pr1 && pr2 ? (t = !t) ? pr1 : pr2 : pr)
// to encode: c and j
// to decode: j and c
@casperx
casperx / glitch-assets-cleaner.py
Created November 1, 2021 19:54
glitch deleted assets cleaner
import json
with open('.glitch-assets') as hand, open('.glitch-assets.clean', 'w') as writer:
db = []
for line in hand:
# parse json entry
data = json.loads(line)
if 'deleted' in data:
# delete item from collection
db = [
item
@casperx
casperx / deepdark.sh
Last active November 11, 2021 07:03
Find deepest directory in your file system.
# list all directory
find -L / -regextype awk -regex '^/(sys|dev|proc)' -prune -o -type d -print
# and pipe it to one of the following command
awk 'length > x {x = length; y = $0} END {print y}' # get longest path
awk -F '/' 'NF > x {x = NF; y = $0} END {print y}' # get deepest path