Skip to content

Instantly share code, notes, and snippets.

View rndme's full-sized avatar

dandavis rndme

View GitHub Profile
Object.defineProperty(window, "__", {set: Object.freeze});
// ex usage:
var state= __={
a: 1,
b: 3,
c: __=[1,2,3, __={ z: 7 } ]
};
// try to mutate:
<vcc-demo></vcc-demo>
<script src="http://danml.com/bundle/rndme.cia_rndme.vcc_.js"></script>
<script>
var store = CIA({
INC: function(state, n) {
state.index+= (n||1);
},
// uses arrow functions to define methods and creates an un-breakable bind to `this`
var x=new function(){
Object.assign(this, {
val : 530,
get: ( )=> this.val,
set: (v)=> this.val=v,
});
};
x.get(); // 530
//enough parens here to compile lisp apps...
var x= Object.freeze(((val)=>({
get: ( )=> val,
set: (v)=> val=v,
}))(123));
x.get(); // 123
x.set(321); // 321
x.get(); // 321
function cls(fn, state){
return Object.freeze(fn.call(state));
}
var x= cls(function(){return{
get: ( )=> this.val,
set: (v)=> this.val=v,
}}, {
val: 654
// uses arrow functions to define methods seperatly from data, like an alternative to .prototype.
var x=function(){
return { // public methods:
get: ( )=> this.val,
set: (v)=> this.val=v,
};
}.call({ // private state:
val: 902
});
function freeze(ob) {
for(var k in ob) {
if(!freeze.hasOwnProperty.call(ob, k)) continue;
if(typeof ob[k] === "object" && ob[k]) freeze(v);
}
return Object.freeze(ob);
}
@rndme
rndme / pget.js
Created March 27, 2016 01:25
simple promise-based ajax function for get and post
//a promise-based ajax io
function pGet(url, data) {
return new Promise(function(y, n) {
var e = new XMLHttpRequest();
e.responseType="document";
e.onload=x=>y(e.response);
if(n) e.onerror=x=>n(e);
e.open(data?"POST":"GET", url, true);
setTimeout(e.send.bind(e,data), 0);
});
// public domain, from wikipedia description: https://en.wikipedia.org/wiki/RC4#Spritz
function rc4(key, str) {
var s = [],
j = 0,
x, res = [],
i, j, y, l = key.length,
m = str.length;
for(i = 0; i < 256; i++) s[i] = i;
//public domain, based on https://www.schneier.com/blog/archives/2014/10/spritz_a_new_rc.html
function spritz(key, str) {
var s = [],
j = 0,
w = 17,
k = 0,
res = [],
l = key.length,
m = str.length,