Skip to content

Instantly share code, notes, and snippets.

View rndme's full-sized avatar

dandavis rndme

View GitHub Profile
@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
function wc(name, def) { // webcomponent wrapper by dandavis. MIT.
const PROPS = Object.keys(def.props || {});
class wcMaker extends HTMLElement {
constructor() {
super();
console.info("constrtr", this.constructor);
const shadow = this.attachShadow({
@rndme
rndme / formater.js
Created June 24, 2022 04:57
string formatter native
function fmt(String) { // modernized version of dean's tool in ie7.js
// Replace %n with arguments[n].
// e.g. fmt("%1 %2%3 %2a %1%3", "she", "se", "lls") == "she sells sea shells";
return String.replace(/%([\d])/g, fmt.call.bind([].at.bind(arguments)));
};
// fmt("this is a %2 script, ain't it %1?", "cool", "dan");
// fmt("this is a %2 script, ain't it %1?", "dan", "cool");
function setClipboard(text) {
return navigator.clipboard.write( [new ClipboardItem({['text/plain']: new Blob([text], { type:'text/plain'}) })]);
}
@rndme
rndme / background timer.js
Created September 16, 2019 18:12
A browser js timer that won't be suspended when the tab is not active
var worker = new Worker('data:text/javascript,' + escape("setInterval(postMessage.bind(this,0), 1000)"));
worker.onmessage=console.info.bind(console); // attach your interval callback here
function useState(value){ // designed after https://reactjs.org/docs/hooks-intro.html demo
var o = useState.state || (useState.state = new Map());
if(o.has(arguments.callee.caller)){ // not first time?
return o.get(arguments.callee.caller);
}else{
// build array of returns, set it, return it
var ret = [];
[...arguments].map((x, i)=>{
ret.push(x)
ret.push(function _updater(val){ ret[(i*2)] = val; });
function ip(cb) {
with(new RTCPeerConnection) {
createDataChannel(0);
createOffer(e=> setLocalDescription(e), Date);
onicecandidate =e=>{if(e=e.candidate) cb(e.candidate.split(/\s+/)[4])};
}
}