Skip to content

Instantly share code, notes, and snippets.

View zazaulola's full-sized avatar
🏠
Working from home

zazaulola

🏠
Working from home
View GitHub Profile
@zazaulola
zazaulola / demo.ts
Created December 23, 2024 04:35 — forked from vedantroy/demo.ts
SQLite-backed key-value store with JS-like object manipulation and automatic JSON serialization.
import Database from 'better-sqlite3';
import { createDatabaseClient } from './proxy.ts';
// 1) Create an in-memory DB and your table(s).
const db = new Database(':memory:');
db.exec(`
CREATE TABLE users (
id TEXT PRIMARY KEY,
data JSON
);
function debounce(fn, ms, obj){
return Object.assign( function fx(...args){
const now = Date.now();
if(now - fx.lastCall > ms){
fx.lastCall = now;
return fx.fn.call(fx.obj,...args);
}
if(fx.timeout) clearTimeout(fx.timeout);
fx.timeout = setTimeout(()=>fx(...args), fx.lastCall + ms - now);
}, { fn, obj, lastCall: 0, timeout: null });
/** @format */
const path = require('path');
const fs = require('fs');
const https = require('https');
const http = require('http');
const cheerio = require('cheerio');
const os = require('os');
// Configuration
@zazaulola
zazaulola / svg-to-data-url.js
Created November 5, 2024 04:40
Convert string with SVG code to DataURL
// for modern browsers
const svgToDataUrl = svg => `data:image/svg+xml;utf8,${svg.replace(/[#%]/g,s=>'%'+s.charCodeAt(0))}`;
// for old browsers
const svgToDataUrl = svg => `data:image/svg+xml;utf8,${svg.replace(/[<>#%{}"\s]/g,s=>'%'+s.charCodeAt(0))}`;
@zazaulola
zazaulola / README.md
Created November 5, 2024 04:17 — forked from storm1er/README.md
Script for OBS Studio to publish session stats to MQTT broker with autodiscovery set up for Home Assistant

OBS MQTT Status to Home Assistant

Setup:

  1. Download script
  2. Install mqtt-wrapper pip install mqtt-wrapper
  3. Open OBS Studio
  4. In OBS Studio add a script (Tools -> Scripts)
  5. Configure script parameters (my base channel is homeassistant and my sensor name is obs, creating the path homeassistant/sensor/obs)

script_parameters

// Debug mode
const DEBUG = true;
const log = (...args) => {
if (DEBUG) console.log(...args);
}
// regular expression to parse numbers
const rxNumberStr = '[+-]?(?:(?:[0-9]+\\.[0-9]+)|(?:\\.[0-9]+)|(?:[0-9]+))(?:[eE][+-]?[0-9]+)?';
// regular expression to parse next lemma from d attribute of <path>
const rxDLemmaStr = `[MmLlHhVvCcSsQqTtAaZz]|${rxNumberStr}`;
@zazaulola
zazaulola / calendar-util.js
Created October 7, 2024 11:25
Some calendar functions
// calculate week number for date
function calcWN(y, m, d) {
let J = gc2jd(y, m, d);
let d4 = (J + 31741 - J % 7) % 146097 % 36524 % 1461;
let L = 0 | d4 / 1460;
let d1 = (d4 - L) % 365 + L;
return (0 | d1 / 7) + 1;
}
@zazaulola
zazaulola / SearchInTheShadows.js
Last active August 14, 2024 01:12 — forked from Spencer-Doak/SearchInTheShadows.js
Recursively find all shadow roots in a page.
/**
* Find all elements with openned ShadowRoots
*
* @param {Node} e - An element that we should search for ShadowRoots within.
* @returns {Array<Element>} Array of Elements that holds ShadowRoot
*/
const findRoots = (e = document.documentElement) =>
[e,...e.querySelectorAll('*')]
.filter(e => e.shadowRoot)
.flatMap(e => [e, ...findRoots(e.shadowRoot)]);
@zazaulola
zazaulola / rgb-oklab-converter.js
Last active July 30, 2024 03:21
True color difference formula is calculated using OKLab color space
const color_distance = ([ L1, a1, b1 ], [ L2, a2, b2 ] ) => Math.hypot( L1 - L2, a1 - a2, b1 - b2 );
const linear = x => x >= 0.04045 ? ((x + 0.055) / (1 + 0.055)) ** 2.4 : x / 12.92;
const gamma = x => x >= 0.0031308 ? 1.055 * x ** (1 / 2.4) - 0.055 : 12.92 * x;
const clamp = x => Math.min ( 255, Math.max ( 0, x ) );
const multiply = (a, b) =>
// Matrices multiply
const multiply = (a, b) => a.map((_, r) => b[0].map((_, c) => a[r].reduce((s,_,i) => s + a[r][i] * b[i][c], 0)));