Skip to content

Instantly share code, notes, and snippets.

View rndme's full-sized avatar

dandavis rndme

View GitHub Profile
@rndme
rndme / cssformat.js
Created December 5, 2025 05:50
css parser formatter
// dan's naive css formatter for editors
// reliably parses and formats css using browser's CSS engine:
// sorts properties in rules, removes property repeats (and fallbacks), removes vendor-specific syntax, and removes comments.
// punctuationReplacements are executed while quoted strings are removed, making rule content predictable
////////////////////
// DOES NOT SUPPORT NESTED RULES - MAINLY TO FORMAT SNIPPETS IN A TEXT EDITOR - NOT PRODUCTION MACHINERY!!!!
////////////////////
@rndme
rndme / on.js
Last active November 20, 2025 06:29
put functions as event handlers in tagged template strings
// a function wrap that lets you put functions as event handlers in template strings, allowing closure w/ event code
// returns an html event=uid into the string template, then soon later swaps for the actual passed function
function on(string) { // tag template function to bind local variables in templated html
let values = [...arguments].slice(1);
values = values.map(function(expression) {
if(typeof expression != "function") return expression;
let id = "on_" + Math.random().toString(36).slice(-6),
evtName = expression.toString().split("=")[0].trim();
@rndme
rndme / on.js
Last active November 12, 2025 18:20
template string function embed for html event handlers
//a function wrap that lets you put functions as event handlers in template strings, allowing closure w/ event code
// returns an html event dec = uid into the string template, then soon later swaps for the actual passed function
(function(){ // non-global scope
function someLocal(){ alert(123); }
function on(evtName, f){
var id = Math.random().toString(36).slice(-6);
setTimeout(x=>{
function toSource(o){ // quick and dirty object literals w/ methods. good for refactoring.
var old = Function.prototype.toJSON;
Function.prototype.toJSON = function () {
return "#>"+this.toString()+"<#";
};
var s = JSON.stringify(o, null, "\t");
s=s.replace(/"#>([\w\W]+?)<#"/g, (a,b)=>eval('"'+b+'"'));
Function.prototype.toJSON = old;
return s;
}
@rndme
rndme / expr_parser.js
Created April 3, 2025 05:49
simple parser stack: tokenizer, ast builder, and evaluator for basic arithmetic expressions
function tokenize(s) { // OPS TERMS
return s.match(/([+*\-\/])|([\d\.]+)/g).map(function Token(t, i) {
return {
token: t,
type: isFinite(t) ? "num" : "op",
index: i,
value: isFinite(t) ? +t : "-+*\/".indexOf(t), // data value/op priority
};
});
}//end tokenize()
"☺☻♥♦♣♠•◘○◙♂♀♪♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼ !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~⌂ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒáíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■"
@rndme
rndme / ImmutableArray.js
Created April 17, 2024 07:35
creates real genuine Arrays that are immutable but "polyfill" mutating methods
// ccby@dandavis
function ImmutableArray(){
function odp(k,v){ Object.defineProperty(r,k,{value:v}); }
let r = [...arguments];
if(r.length===1 && Array.isArray(r[0])) r = r[0];
odp("push", function(){return ImmutableArray(r.concat(...arguments));});
@rndme
rndme / odp.js
Last active April 12, 2024 02:59
tiny getter/setter helper for observables, polyfilling, deprecation, interfacing, etc
function odp(obj, get, set, isHidden){
Object.defineProperty(obj, /^\w+/.exec(get), {get, set, enumerable: !isHidden});
return odp.bind(this, obj);
}
// example usage in a class
class Ohm {
constructor(V, I){
odp(this, _=>Ohm)
( v=>V, _=>V=_ )
function Ohm(v, i, _){
(_=(k, get, set) => Object.defineProperty(this, k, {get, set, enumerable: 1}) && _)
("v", _=>v, x=>v=x)
("i", _=>i, x=>i=x)
("w", _=>v*i, x=>i=x/v)
("r", _=>v/i, x=>i=v/x);
}
/*
* A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
* in FIPS 180-1
* Version 2.2 Copyright Paul Johnston 2000 - 2009.
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
* Distributed under the BSD License
* See http://pajhome.org.uk/crypt/md5 for details.
*/
// smooooshed by dandavis 2024. tested in jscript5.5