Skip to content

Instantly share code, notes, and snippets.

View mindon's full-sized avatar
🙃
zig, deno and hx, sometimes go & qt

mindon mindon

🙃
zig, deno and hx, sometimes go & qt
View GitHub Profile
@mindon
mindon / db$.js
Created May 7, 2023 17:06
simple api to using indexeddb
const _DT = {id: 'de', name: 'data'};
// indexeddb
export function db$(id, name, todo) {
if (!win.indexedDB) {
return;
}
const { mode = "readonly" } = todo;
let req = indexedDB.open(id);
req.onsuccess = (evt) => {
const db = evt.target.result;
@mindon
mindon / fraction.js
Last active November 21, 2024 09:54
try to get a fraction of a number
// fraction gets up/down from a number
export function fraction(v: number, { precise = 5, tries = 100 }): string {
const [ul, dl] = [[0, 1], [1, 0]];
const max = parseInt((v - Math.floor(v)).toString().substring(2));
let [previous, current] = [NaN];
let c = v;
for (let i = 2; i < tries; i++) {
const n = Math.floor(c);
const uc = n * ul[i - 1] + ul[i - 2];
if (Math.abs(ul[i]) > max) {
@mindon
mindon / class-reuse.js
Created November 3, 2022 07:38
use (selector) in class to reuse classes from first element of selector
// (selector) in class to reuse classes from first element of selector
// define a class-ref <div id="my" class="a b c d"></div>
// use class ref <div class="ref x y (#my) z"></div>
// result in <div class="ref x y a b c d z"></div>
function refClass(root) {
[].slice.apply((root || document).querySelectorAll('.ref')).forEach(d => {
const c = d.className.replace(/\([:#.]?[\w.-]+\)/g, (s) => {
const q = s.charAt(1) == ':'
? `[class-ref="${s.substr(2, s.length - 3)}"]`
: s.substr(1, s.length -2);
@mindon
mindon / qconvert.ts
Last active October 11, 2022 03:26
qconvert typescript for deno, a Quantum Computing Language Converter
// qconvert for deno
// updated: 2022-10-11, Mindon<[email protected]>
// e.g. `deno run --allow-read --allow-write --unstable qconvert.ts -i hello.qasm -s qasm -o hello.quil -t quil`
// original: https://github.com/quantastica/qconvert-js
import { default as QuantumCircuit } from "npm:quantum-circuit";
import { parse } from "https://deno.land/std/flags/mod.ts";
import { exists } from "https://deno.land/std/fs/mod.ts";
import { assert } from "https://deno.land/std/testing/asserts.ts";
@mindon
mindon / cadder.go
Last active March 22, 2023 02:19
Simple command tool to start|stop|reload CaddyServer in current directory without merging all configs into one (for Caddy2+)
package main
// cadder to start|stop|reload simple caddy servers in current directory, without merge all configs into one
// example: /site0/Caddyfile, /site1/caddy.json, `cd /site0/ && cadder start` then `cd /site1/ && cadder start``
//
// How to build cadder?
// you need download and install golang from <https://go.dev/dl/> then `go build cadder.go`
//
// author: [email protected]
// created: 2022-07-14
@mindon
mindon / git-a-patch-zip.sh
Last active November 29, 2024 03:16
patch last git submit as a zip
git diff HEAD@{1} --diff-filter=ACMR --name-only -z | xargs -0 git archive HEAD -o patch_$(date +'%Y%m%d').zip --
@mindon
mindon / ipr.ts
Last active June 16, 2022 03:29
deno typescript server to provide ip external for client or visitor at server
import {
serve,
type ConnInfo,
type Handler,
type ServeInit,
} from 'https://deno.land/[email protected]/http/server.ts';
function assertIsNetAddr(addr: Deno.Addr): asserts addr is Deno.NetAddr {
if (!['tcp', 'udp'].includes(addr.transport)) {
throw new Error('Not a valid network address');
@mindon
mindon / optimize-embed-images-size-in-svg.js
Created May 26, 2022 12:36
Let's say, what if your designer providing a svg file with large embedded images... how to optimize the svg file size?
(function(svgdoc, scale) {
// optimize large embed images in a svg
// usage: <html><body><svg>your own real svg with too large embeded images...</svg><script src="optimize-embed-images-size-in-svg.js">< /script></body></html>
// author: [email protected]
function optimize(img, cb) {
const src = img ? img.getAttribute("xlink:href") : undefined;
let c = document.createElement("canvas");
const done = () => {
cb && cb();
@mindon
mindon / lost-in-cos.go
Created May 25, 2022 11:12
Find any file sync failed folders under Tencent Cloud COS bucket 查找腾讯云COS有同步(移动)失败文件的文件夹
package main
import (
"context"
"log"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
@mindon
mindon / drc_hook.rb
Created April 6, 2022 02:35
hook (previouse) for KLayout DRC, put this file under folder bin-release/drc/
# $autorun-early
require 'open3'
# [email protected] 2022-04-02
module DRC
module Hook
def self.on(drc, macro)
name = 'KLAYOUT_HOOK_DRC'
body = macro.text