Skip to content

Instantly share code, notes, and snippets.

@mindon
mindon / sort_by_attrs.js
Last active March 13, 2024 06:46
sort a object array with string attributes
const arr = [{attr:'x', value: 2}, {attr:'y', value: 2}, {attr:'z', value: 1}];
// descend
arr.sort((a,b,x='attr')=>+(a[x]<b[x])-+(a[x]>b[x]));
console.log(arr.slice(0));
// ascend
arr.sort((a,b,x='attr')=>+(a[x]>b[x])-+(a[x]<b[x]));
console.log(arr.slice(0));
@mindon
mindon / MixedFilterModel.cpp
Last active November 24, 2023 02:48
Qt Multi-Columns QSortFilterProxyModel: MixedFilterModel
#include "MixedFilterModel.h"
bool MixedFilterModel::fit(QString s, QVariant rule)
{
int metaTypeId = rule.metaType().id();
if (metaTypeId == QMetaType::QString) {
QString r = rule.toString();
if (r == "*")
return true;
if (r.contains("*")) {
@mindon
mindon / get$.js
Created May 7, 2023 17:11
fetch large file with progress callback
async function get$(src, cb, opts = { method: "GET" }) {
let [current, received, total] = [0, 0, 0];
cb({ state: "nif" });
const resp = await fetch(src, {
mode: "cors",
headers: {
"Content-Type": "application/octet-stream",
},
@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.ts
Created January 4, 2023 08:36
try to get a fraction of a number
// fraction gets up/down from a number
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<mindon@live.com>
// 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: mindon@live.com
// created: 2022-07-14
@mindon
mindon / patch.sh
Last active December 7, 2022 08:56
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/std@0.144.0/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');