Make SJW modules out of the source files

This commit is contained in:
Tissevert 2020-01-08 22:58:02 +01:00
parent 56afea119a
commit 3862a866fd
8 changed files with 288 additions and 298 deletions

131
async.js
View file

@ -1,131 +0,0 @@
function Async() {
return {
apply: apply,
bind: bind,
fail: fail,
http: http,
map: map,
parallel: parallel,
run: run,
sequence: sequence,
wait: wait,
wrap: wrap
};
function apply(f, x) {
return function(g) {
g(f(x));
};
}
function bind() {
var m, steps, i;
if(arguments.length < 1) {
return wrap();
}
m = arguments[0];
steps = arguments;
i = 1;
return function(f) {
var step = function(x) {
if(i < steps.length) {
steps[i++](x)(step);
} else {
return f(x);
}
}
m(step);
};
}
function fail(message) {
return function(f) {
console.log(message);
}
}
function map(mapper) {
return function(x) {
return function(f) {
f(mapper(x));
};
};
}
function parallel() {
var threads = arguments;
var pending = threads.length;
var results = [];
var returned = [];
return function(f) {
var useResult = function(i) {
return function(x) {
if(!returned[i]) {
results[i] = x;
returned[i] = true;
pending--;
if(pending < 1) {
f(results);
}
}
};
};
for(var i = 0; i < threads.length; i++) {
threads[i](useResult(i));
}
};
}
function run() {
var m;
if(arguments.length == 1) {
m = arguments[0];
} else {
m = sequence.apply(null, arguments);
}
m(function() {});
}
function sequence() {
var steps = arguments;
var i = 0;
return function(f) {
var step = function(x) {
if(i < steps.length) {
steps[i++](step);
} else {
f(x);
}
}
step();
};
}
function wait(delay) {
return function(f) {
setTimeout(f, delay);
};
}
function wrap(x) {
return function(f) {
f(x);
};
}
function http(query) {
return function(f) {
var xhr = new XMLHttpRequest();
xhr.addEventListener('load', function() {
f(this);
});
xhr.open(query.method, query.url);
if(query.method == 'POST') {
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
}
xhr.send(query.body);
};
}
}

View file

@ -1,54 +0,0 @@
function Cache() {
function Cache(loader) {
this.loader = loader;
this.loaded = {};
this.loading = {};
}
Cache.prototype.get = function(key) {
return function(f) {
if(this.loaded[key] != undefined) {
f(this.loaded[key]);
} else {
this.startLoading(key);
this.loading[key].push(f);
this.loader(key)(this.store(key));
}
}.bind(this);
};
Cache.prototype.warmUp = function(key) {
if(this.loaded[key] == undefined) {
this.startLoading(key);
this.loader(key)(this.store(key));
}
};
Cache.prototype.startLoading = function(key) {
if(this.loading[key] == undefined) {
this.loading[key] = [];
}
};
Cache.prototype.store = function(key) {
return function(value) {
this.loaded[key] = value;
for(var i = 0; i < this.loading[key].length; i++) {
this.loading[key][i](value);
}
this.loading[key] = null;
}.bind(this);
};
return {
make: make
};
function make(loader) {
var cache = new Cache(loader);
return {
get: cache.get.bind(cache),
warmUp: cache.warmUp.bind(cache)
};
}
}

39
dom.js
View file

@ -1,39 +0,0 @@
function Dom() {
return {
clear: clear,
make: make
}
function clear(elem) {
while(elem.firstChild) {
elem.removeChild(elem.firstChild);
}
}
function make(tag, properties, children) {
var e = document.createElement(tag);
properties = properties || {};
children = children || [];
for(key in properties) {
var value = properties[key];
switch(key) {
case "class":
e.className = Array.isArray(value) ? value.join(' ') : value;
break;;
case "maxlength":
e.setAttribute("maxlength", value);
break;
case "onClick":
e.addEventListener("click", value);
break;;
default:
e[key] = value;
}
}
for(var i = 0; i < children.length; i++) {
e.appendChild(children[i]);
}
return e;
}
}

74
fun.js
View file

@ -1,74 +0,0 @@
function Fun() {
return {
compare: compare,
compose: compose,
defined: defined,
id: id,
insert: insert,
map: map,
mapFilter: mapFilter,
of: of,
proj: proj
};
function compare(on) {
on = on || id;
return function(a, b) {
var va = on(a), vb = on(b);
return va < vb ? -1 : (va > vb ? 1 : 0)
};
}
function compose(f, g) {
return function() {
return f(g.apply(null, arguments));
};
}
function defined(x) {
return x != undefined;
}
function id(x) {
return x;
}
function insert(obj, t, comparer, min, max) {
min = defined(min) ? min : 0;
max = defined(max) ? max : t.length;
comparer = comparer || compare();
if(max - min < 1) {
return min;
}
var avg = Math.floor((max + min) / 2);
if (compare(obj, t[avg]) < 0) {
return insert(obj, t, compare, min, avg);
} else {
return insert(obj, t, compare, avg+1, max);
}
}
function map(mapper, f) {
return function() {
var args = Array.prototype.map.call(arguments, mapper);
return f.apply(null, args);
}
}
function mapFilter(mapper, predicate) {
return function(array) {
return array.reduce(function(accumulator, elem) {
var v = mapper(elem);
return predicate(v) ? accumulator.concat(v) : accumulator;
}, []);
};
}
function of(o) {
return function(key) {return o[key];};
}
function proj(key) {
return function(o) {return o[key];};
}
}

128
src/UnitJS/Async.js Normal file
View file

@ -0,0 +1,128 @@
return {
apply: apply,
bind: bind,
fail: fail,
http: http,
map: map,
parallel: parallel,
run: run,
sequence: sequence,
wait: wait,
wrap: wrap
};
function apply(f, x) {
return function(g) {
g(f(x));
};
}
function bind() {
var m, steps, i;
if(arguments.length < 1) {
return wrap();
}
m = arguments[0];
steps = arguments;
i = 1;
return function(f) {
var step = function(x) {
if(i < steps.length) {
steps[i++](x)(step);
} else {
return f(x);
}
}
m(step);
};
}
function fail(message) {
return function(f) {
console.log(message);
}
}
function map(mapper) {
return function(x) {
return function(f) {
f(mapper(x));
};
};
}
function parallel() {
var threads = arguments;
var pending = threads.length;
var results = [];
var returned = [];
return function(f) {
var useResult = function(i) {
return function(x) {
if(!returned[i]) {
results[i] = x;
returned[i] = true;
pending--;
if(pending < 1) {
f(results);
}
}
};
};
for(var i = 0; i < threads.length; i++) {
threads[i](useResult(i));
}
};
}
function run() {
var m;
if(arguments.length == 1) {
m = arguments[0];
} else {
m = sequence.apply(null, arguments);
}
m(function() {});
}
function sequence() {
var steps = arguments;
var i = 0;
return function(f) {
var step = function(x) {
if(i < steps.length) {
steps[i++](step);
} else {
f(x);
}
}
step();
};
}
function wait(delay) {
return function(f) {
setTimeout(f, delay);
};
}
function wrap(x) {
return function(f) {
f(x);
};
}
function http(query) {
return function(f) {
var xhr = new XMLHttpRequest();
xhr.addEventListener('load', function() {
f(this);
});
xhr.open(query.method, query.url);
if(query.method == 'POST') {
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
}
xhr.send(query.body);
};
}

52
src/UnitJS/Cache.js Normal file
View file

@ -0,0 +1,52 @@
function Cache(loader) {
this.loader = loader;
this.loaded = {};
this.loading = {};
}
Cache.prototype.get = function(key) {
return function(f) {
if(this.loaded[key] != undefined) {
f(this.loaded[key]);
} else {
this.startLoading(key);
this.loading[key].push(f);
this.loader(key)(this.store(key));
}
}.bind(this);
};
Cache.prototype.warmUp = function(key) {
if(this.loaded[key] == undefined) {
this.startLoading(key);
this.loader(key)(this.store(key));
}
};
Cache.prototype.startLoading = function(key) {
if(this.loading[key] == undefined) {
this.loading[key] = [];
}
};
Cache.prototype.store = function(key) {
return function(value) {
this.loaded[key] = value;
for(var i = 0; i < this.loading[key].length; i++) {
this.loading[key][i](value);
}
this.loading[key] = null;
}.bind(this);
};
return {
make: make
};
function make(loader) {
var cache = new Cache(loader);
return {
get: cache.get.bind(cache),
warmUp: cache.warmUp.bind(cache)
};
}

36
src/UnitJS/Dom.js Normal file
View file

@ -0,0 +1,36 @@
return {
clear: clear,
make: make
}
function clear(elem) {
while(elem.firstChild) {
elem.removeChild(elem.firstChild);
}
}
function make(tag, properties, children) {
var e = document.createElement(tag);
properties = properties || {};
children = children || [];
for(key in properties) {
var value = properties[key];
switch(key) {
case "class":
e.className = Array.isArray(value) ? value.join(' ') : value;
break;;
case "maxlength":
e.setAttribute("maxlength", value);
break;
case "onClick":
e.addEventListener("click", value);
break;;
default:
e[key] = value;
}
}
for(var i = 0; i < children.length; i++) {
e.appendChild(children[i]);
}
return e;
}

72
src/UnitJS/Fun.js Normal file
View file

@ -0,0 +1,72 @@
return {
compare: compare,
compose: compose,
defined: defined,
id: id,
insert: insert,
map: map,
mapFilter: mapFilter,
of: of,
proj: proj
};
function compare(on) {
on = on || id;
return function(a, b) {
var va = on(a), vb = on(b);
return va < vb ? -1 : (va > vb ? 1 : 0)
};
}
function compose(f, g) {
return function() {
return f(g.apply(null, arguments));
};
}
function defined(x) {
return x != undefined;
}
function id(x) {
return x;
}
function insert(obj, t, comparer, min, max) {
min = defined(min) ? min : 0;
max = defined(max) ? max : t.length;
comparer = comparer || compare();
if(max - min < 1) {
return min;
}
var avg = Math.floor((max + min) / 2);
if (compare(obj, t[avg]) < 0) {
return insert(obj, t, compare, min, avg);
} else {
return insert(obj, t, compare, avg+1, max);
}
}
function map(mapper, f) {
return function() {
var args = Array.prototype.map.call(arguments, mapper);
return f.apply(null, args);
}
}
function mapFilter(mapper, predicate) {
return function(array) {
return array.reduce(function(accumulator, elem) {
var v = mapper(elem);
return predicate(v) ? accumulator.concat(v) : accumulator;
}, []);
};
}
function of(o) {
return function(key) {return o[key];};
}
function proj(key) {
return function(o) {return o[key];};
}