git.schokokeks.org
Repositories
Help
Report an Issue
fs-words.git
Code
Commits
Branches
Tags
Suche
Strukturansicht:
6ae1cb8
Branches
Tags
develop-client_server
master
typescript
fs-words.git
client
build
logic.js
[add] client
Christian Fraß
commited
6ae1cb8
at 2021-03-08 23:52:24
logic.js
Blame
History
Raw
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); /* This file is part of »bacterio-plankton:base«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:base« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:base« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:base«. If not, see <http://www.gnu.org/licenses/>. */ // } /* This file is part of »bacterio-plankton:base«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:base« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:base« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:base«. If not, see <http://www.gnu.org/licenses/>. */ /** * @author fenris */ /*export*/ function pseudopointer_null() { return { "value": null }; } /** * @author fenris */ /*export*/ function pseudopointer_make(value) { return { "value": value }; } /** * @author fenris */ /*export*/ function pseudopointer_isset(pseudopointer) { return (pseudopointer.value != null); } /** * @author fenris */ /*export*/ function pseudopointer_read(pseudopointer) { if (pseudopointer.value != null) { return pseudopointer.value; } else { var message = "nullpointer dereferencation"; throw (new Error(message)); } } /** * @author fenris */ /*export*/ function pseudopointer_write(pseudopointer, value) { pseudopointer.value = value; } /* This file is part of »bacterio-plankton:base«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:base« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:base« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:base«. If not, see <http://www.gnu.org/licenses/>. */ ; var lib_base; (function (lib_base) { /** * @author fenris */ function environment() { var entries = [ { "id": "web", "name": "Web", "predicate": function () { return (typeof (document) !== "undefined"); }, }, { "id": "node", "name": "Node.js", "predicate": function () { return (typeof (process) !== "undefined"); }, }, { "id": "rhino", "name": "Rhino", "predicate": function () { return (typeof (java) !== "undefined"); }, }, { "id": "webworker", "name": "WebWorker", "predicate": function () { return (typeof (self["WorkerNavigator"]) !== "undefined"); } } ]; var id; var found = entries.some(function (entry) { if (entry.predicate()) { id = entry.id; return true; } else { return false; } }); if (found) { return id; } else { throw (new Error("unknown environment")); } } lib_base.environment = environment; })(lib_base || (lib_base = {})); /* This file is part of »bacterio-plankton:base«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:base« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:base« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:base«. If not, see <http://www.gnu.org/licenses/>. */ /** * @author fenris */ var instance_verbosity = 0; /** * @author fenris */ function instance_collate(value1, value2) { if (typeof (value1) === "object") { if (value1 == null) { return (value2 == null); } else { if ("_collate" in value1) { return value1["_collate"](value2); } else { throw (new Error("[collate]" + " " + "object has no such method")); } } } else { if (instance_verbosity >= 1) { lib_log.warn("[collate]" + " " + "primitive value; using default implementation"); } return (value1 === value2); } } /** * @author fenris */ function instance_compare(value1, value2) { if (typeof (value1) === "object") { if ("_compare" in value1) { return value1["_compare"](value2); } else { throw (new Error("[compare]" + " " + "object has no such method")); } } else { if (instance_verbosity >= 1) { lib_log.warn("[compare]" + " " + "primitive value; using default implementation"); } return (value1 <= value2); } } /** * @author fenris */ function instance_clone(value) { if (typeof (value) === "object") { if ("_clone" in value) { return value["_clone"](); } else { throw (new Error("[clone]" + " " + "object has no such method")); } } else { if (instance_verbosity >= 1) { lib_log.warn("[clone]" + " " + "primitive value; using default implementation"); } return value; } } /** * @desc the ability to generate a string out of the element, which identifies it to a high degree * @author fenris */ function instance_hash(value) { if (typeof (value) === "object") { if ("_hash" in value) { return value["_hash"](); } else { throw (new Error("[hash]" + " " + "object has no such method")); } } else { if (instance_verbosity >= 1) { lib_log.warn("[hash]" + " " + "primitive value; using default implementation"); } return String(value); } } /** * @desc the ability to map the element to a textual representation (most likely not injective) * @author fenris */ function instance_show(value) { if (typeof (value) === "object") { if (value == null) { return "NULL"; } else { if ("_show" in value) { return value["_show"](); } else { // throw (new Error("[show]" + " " + "object has no such method")); return JSON.stringify(value); } } } else { if (instance_verbosity >= 1) { lib_log.warn("[show]" + " " + "primitive value; using default implementation"); } return String(value); } } /* This file is part of »bacterio-plankton:base«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:base« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:base« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:base«. If not, see <http://www.gnu.org/licenses/>. */ /** * @todo outsource to dedicated plankton-lib */ var lib_log; (function (lib_log) { /** * @author fenris */ function log() { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } /*window.*/ console.log.apply(console, args); } lib_log.log = log; /** * @author fenris */ function info() { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } /*window.*/ console.info.apply(console, args); } lib_log.info = info; /** * @author fenris */ function warn() { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } /*window.*/ console.warn.apply(console, args); } lib_log.warn = warn; /** * @author fenris */ function error() { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } /*window.*/ console.error.apply(console, args); } lib_log.error = error; })(lib_log || (lib_log = {})); /* This file is part of »bacterio-plankton:base«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:base« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:base« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:base«. If not, see <http://www.gnu.org/licenses/>. */ /** * @author frac */ var class_observer = /** @class */ (function () { /** * @author frac */ function class_observer() { this.counter = 0; this.actions = {}; this.buffer = []; } /** * @author frac */ class_observer.prototype.empty = function () { return (Object.keys(this.actions).length == 0); }; /** * @author frac */ class_observer.prototype.flush = function () { this.actions = {}; }; /** * @author frac */ class_observer.prototype.set = function (id, action) { this.actions[id] = action; }; /** * @author frac */ class_observer.prototype.del = function (id) { delete this.actions[id]; }; /** * @author frac */ class_observer.prototype.add = function (action) { this.set((this.counter++).toString(), action); }; /** * @author frac */ class_observer.prototype.notify = function (information, delayed) { var _this = this; if (information === void 0) { information = {}; } if (delayed === void 0) { delayed = false; } if (delayed) { this.buffer.push(information); } else { Object.keys(this.actions).forEach(function (id) { return _this.actions[id](information); }); } }; /** * @author frac */ class_observer.prototype.rollout = function () { var _this = this; this.buffer.forEach(function (information) { return _this.notify(information, false); }); this.buffer = []; }; return class_observer; }()); /** * @author frac */ /* export interface interface_readable<type_value> { |** * @author frac *| read() : type_executor<type_value, Error>; } */ /** * @author frac */ /* export interface interface_writeable<type_value> { |** * @author frac *| write(value : type_value) : type_executor<void, Error>; } */ /* This file is part of »bacterio-plankton:base«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:base« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:base« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:base«. If not, see <http://www.gnu.org/licenses/>. */ var lib_maybe; (function (lib_maybe) { /** * @author fenris */ function make_nothing() { return { "kind": "nothing", "parameters": {} }; } lib_maybe.make_nothing = make_nothing; /** * @author fenris */ function make_just(value) { return { "kind": "just", "parameters": { "value": value } }; } lib_maybe.make_just = make_just; /** * @author fenris */ function is_nothing(maybe) { return (maybe.kind === "nothing"); } lib_maybe.is_nothing = is_nothing; /** * @author fenris */ function is_just(maybe) { return (maybe.kind === "just"); } lib_maybe.is_just = is_just; /** * @author fenris */ function cull(maybe) { if (!is_just(maybe)) { var message = "cull from nothing"; throw (new Error(message)); } else { var value = maybe.parameters["value"]; return value; } } lib_maybe.cull = cull; /** * @author fenris */ function propagate(maybe, function_) { if (!is_just(maybe)) { } else { var value = maybe.parameters["value"]; var maybe_ = function_(value); return maybe_; } } lib_maybe.propagate = propagate; })(lib_maybe || (lib_maybe = {})); /** * @author fenris */ /*export*/ var class_maybe = /** @class */ (function () { function class_maybe() { } /** * @desc whether the wrapper is nothing * @author fenris */ class_maybe.prototype.is_nothing = function () { throw (new Error("not implemented: class_maybe.is_nothing")); }; /** * @desc whether the wrapper is just * @author fenris */ class_maybe.prototype.is_just = function () { throw (new Error("not implemented: class_maybe.is_just")); }; /** * @desc return the value, stored in the maybe-wrapper * @author fenris */ class_maybe.prototype.cull = function () { throw (new Error("not implemented: class_maybe.cull")); }; /** * @author fenris */ class_maybe.prototype.toString = function () { throw (new Error("not implemented: class_maybe.cull")); }; /** * @author fenris */ class_maybe.prototype.distinguish = function (action_just, action_nothing) { if (action_nothing === void 0) { action_nothing = function () { }; } throw (new Error("not implemented: class_maybe.distinguish")); }; /** * @author fenris */ class_maybe.prototype.propagate = function (action) { throw (new Error("not implemented: class_maybe.propagate")); }; /** * @desc [implementation] * @author fenris */ class_maybe.prototype._show = function () { return this.toString(); }; return class_maybe; }()); /** * @author fenris */ /*export*/ var class_nothing = /** @class */ (function (_super) { __extends(class_nothing, _super); /** * @author fenris */ function class_nothing(reason) { if (reason === void 0) { reason = null; } var _this = _super.call(this) || this; _this.reason = reason; return _this; } /** * @author fenris */ class_nothing.prototype.is_nothing = function () { return true; }; /** * @author fenris */ class_nothing.prototype.is_just = function () { return false; }; /** * @author fenris */ class_nothing.prototype.cull = function () { var message = "you shouldn't cull a nothing-value …"; lib_log.warn(message); return null; }; /** * @author fenris */ class_nothing.prototype.toString = function () { return "<\u00B7>"; }; /** * @author fenris */ class_nothing.prototype.reason_get = function () { var content = ((this.reason == null) ? "·" : this.reason); return "<- " + content + " ->"; }; /** * @author fenris */ class_nothing.prototype.distinguish = function (action_just, action_nothing) { if (action_nothing === void 0) { action_nothing = function () { }; } action_nothing(this.reason); }; /** * @author fenris */ class_nothing.prototype.propagate = function (action) { return (new class_nothing(this.reason)); }; return class_nothing; }(class_maybe)); /** * @author fenris */ /*export*/ var class_just = /** @class */ (function (_super) { __extends(class_just, _super); /** * @author fenris */ function class_just(value) { var _this = _super.call(this) || this; _this.value = value; return _this; } /** * @author fenris */ class_just.prototype.is_nothing = function () { return false; }; /** * @author fenris */ class_just.prototype.is_just = function () { return true; }; /** * @author fenris */ class_just.prototype.cull = function () { return this.value; }; /** * @author fenris */ class_just.prototype.toString = function () { var content = instance_show(this.value); return "<+ " + content + " +>"; }; /** * @author fenris */ class_just.prototype.distinguish = function (action_just, action_nothing) { if (action_nothing === void 0) { action_nothing = function () { }; } action_just(this.value); }; /** * @author fenris */ class_just.prototype.propagate = function (action) { return action(this.value); }; return class_just; }(class_maybe)); /* This file is part of »bacterio-plankton:base«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:base« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:base« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:base«. If not, see <http://www.gnu.org/licenses/>. */ /** * @author frac */ var class_error = /** @class */ (function (_super) { __extends(class_error, _super); /** * @author frac */ function class_error(message, suberrors) { if (suberrors === void 0) { suberrors = []; } var _this = _super.call(this, message) || this; _this.suberrors = suberrors; _this.mess = message; return _this; } /** * @override * @author frac */ class_error.prototype.toString = function () { return ( /*super.toString()*/this.mess + " " + ("[" + this.suberrors.map(function (x) { return x.toString(); }).join(",") + "]")); }; return class_error; }(Error)); /* This file is part of »bacterio-plankton:object«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:object« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:object« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:object«. If not, see <http://www.gnu.org/licenses/>. */ var lib_object; (function (lib_object) { /** * @author fenris */ function fetch(object, fieldname, fallback, escalation) { if (fallback === void 0) { fallback = null; } if (escalation === void 0) { escalation = 1; } if ((fieldname in object) && (object[fieldname] !== undefined)) { return object[fieldname]; } else { switch (escalation) { case 0: { return fallback; break; } case 1: { var message = ("field '" + fieldname + "' not in structure"); message += ("; using fallback value '" + String(fallback) + "'"); // console.warn(message); return fallback; break; } case 2: { var message = ("field '" + fieldname + "' not in structure"); throw (new Error(message)); break; } default: { throw (new Error("invalid escalation level " + escalation)); break; } } } } lib_object.fetch = fetch; /** * @author fenris */ function map(object_from, transformator) { var object_to = {}; Object.keys(object_from).forEach(function (key) { return (object_to[key] = transformator(object_from[key], key)); }); return object_to; } lib_object.map = map; /** * @desc gibt ein Objekt mit bestimmten Einträgen des Eingabe-Objekts zurück * @author fenris */ function filter(object_from, predicate) { var object_to = {}; Object.keys(object_from).forEach(function (key) { var value = object_from[key]; if (predicate(value, key)) { object_to[key] = value; } }); return object_to; } lib_object.filter = filter; /** * @desc wandelt ein Array mit Einträgen der Form {key,value} in ein entsprechendes Objekt um * @author fenris */ function from_array(array) { var object = {}; array.forEach(function (entry) { return (object[entry.key] = entry.value); }); return object; } lib_object.from_array = from_array; /** * @desc wandelt ein Objekt in ein entsprechendes Array mit Einträgen der Form {key,value} um * @author fenris */ function to_array(object) { var array = []; Object.keys(object).forEach(function (key) { return array.push({ "key": key, "value": object[key] }); }); return array; } lib_object.to_array = to_array; /** * @desc gibt eine Liste von Schlüsseln eines Objekts zurück * @author fenris */ function keys(object) { return Object.keys(object); } lib_object.keys = keys; /** * @desc gibt eine Liste von Werten eines Objekts zurück * @author fenris */ function values(object) { return to_array(object).map(function (entry) { return entry.value; }); } lib_object.values = values; /** * @desc liest ein Baum-artiges Objekt an einer bestimmten Stelle aus * @author fenris */ function path_read(object, path, fallback, escalation) { if (fallback === void 0) { fallback = null; } if (escalation === void 0) { escalation = 1; } var steps = ((path.length == 0) ? [] : path.split(".")); if (steps.length == 0) { throw (new Error("empty path")); } else { var position_1 = object; var reachable = (position_1 != null) && steps.slice(0, steps.length - 1).every(function (step) { position_1 = object_fetch(position_1, step, null, 0); return (position_1 != null); }); if (reachable) { return object_fetch(position_1, steps[steps.length - 1], fallback, escalation); } else { return object_fetch({}, "_dummy_", fallback, escalation); } } } lib_object.path_read = path_read; /** * @desc schreibt einen Wert an eine bestimmte Stelle in einem Baum-artigen Objekt * @author fenris */ function path_write(object, path, value, construct) { if (construct === void 0) { construct = true; } var steps = ((path.length == 0) ? [] : path.split(".")); if (steps.length == 0) { throw (new Error("empty path")); } else { var position_2 = object; var reachable = steps.slice(0, steps.length - 1).every(function (step) { var position_ = object_fetch(position_2, step, null, 0); if (position_ == null) { if (construct) { position_2[step] = {}; position_2 = position_2[step]; return true; } else { return false; } } else { position_2 = position_; return true; } }); if (reachable) { position_2[steps[steps.length - 1]] = value; } else { var message = ("path '" + path + "' does not exist and may not be constructed"); throw (new Error(message)); } } } lib_object.path_write = path_write; /** * @desc prüft ob ein Objekt einem bestimmten Muster entspricht * @param {Object} object das zu prüfende Objekt * @param {Object} pattern das einzuhaltende Muster * @param {Function} connlate eine Funktion zum Feststellen der Gleichheit von Einzelwerten * @author fenris */ function matches(object, pattern, collate) { if (collate === void 0) { collate = instance_collate; } return Object.keys(pattern).every(function (key) { return collate(pattern[key], object[key]); }); } lib_object.matches = matches; /** * @desc erzeugt eine Projektion eines Baum-artigen Objekts in ein Listen-artiges Objekt * @param {string} [separator] welches Zeichen als Trenner zwischen zwei Pfad-Schritten verwendet werden soll * @author fenris */ function flatten(value, separator, key_for_element) { if (separator === void 0) { separator = "."; } if (key_for_element === void 0) { key_for_element = (function (index) { return ("element_" + index.toFixed(0)); }); } var integrate = function (result, key_, value_) { if (value_ == null) { result[key_] = value_; } else { // primitive Werte direkt übernehmen if (typeof (value_) != "object") { result[key_] = value_; } // sonst durch rekursiven Aufruf die flache Variante des Wertes ermitteln und einarbeiten else { var result_1 = flatten(value_); Object.keys(result_1) .forEach(function (key__) { var value__ = result_1[key__]; var key_new = (key_ + separator + key__); result[key_new] = value__; }); } } }; if ((value === null) || (value === undefined)) { return null; } else { var result_2 = {}; if (typeof (value) != "object") { result_2["value"] = value; } else { if (value instanceof Array) { var array = (value); array .forEach(function (element, index) { integrate(result_2, key_for_element(index), element); }); } else { var object_1 = (value); Object.keys(object_1) .forEach(function (key) { integrate(result_2, key, object_1[key]); }); } } return result_2; } } lib_object.flatten = flatten; /** * @author fenris */ function clash(x, y, _a) { var _b = _a === void 0 ? {} : _a, _c = _b["overwrite"], overwrite = _c === void 0 ? true : _c, _d = _b["hooks"], _e = (_d === void 0 ? {} : _d)["existing"], hook_existing = _e === void 0 ? null : _e; if (hook_existing == null) { (function (key, value_old, value_new) { return console.warn("field " + key + " already defined"); }); } var z = {}; Object.keys(x).forEach(function (key) { z[key] = x[key]; }); Object.keys(y).forEach(function (key) { if (key in z) { if (hook_existing != null) { hook_existing(key, z[key], y[key]); } if (overwrite) { z[key] = y[key]; } } else { z[key] = y[key]; } }); return z; } lib_object.clash = clash; /** * @author fenris */ function patch(core, mantle, deep, path) { if (deep === void 0) { deep = true; } if (path === void 0) { path = null; } if (mantle == null) { console.warn("mantle is null; core was", core); } else { Object.keys(mantle).forEach(function (key) { var path_ = ((path == null) ? key : path + "." + key); var value_mantle = mantle[key]; if (!(key in core)) { if ((typeof (value_mantle) == "object") && (value_mantle != null) && deep) { if (value_mantle instanceof Array) { core[key] = []; value_mantle.forEach(function (element) { if ((typeof (element) == "object") && (element != null)) { var element_ = {}; patch(element_, element); core[key].push(element_); } else { core[key].push(element); } }); } else { core[key] = {}; patch(core[key], value_mantle, deep, path_); } } else { core[key] = value_mantle; } } else { var value_core = core[key]; if (typeof (value_core) == typeof (value_mantle)) { if ((typeof (value_mantle) == "object") && (value_mantle != null) && deep) { patch(core[key], value_mantle, deep, path_); } else { core[key] = value_mantle; } } else { if ((value_core != null) && (value_mantle != null)) { var message = "objects have different shapes at path '" + path_ + "'; core has type '" + typeof (value_core) + "' and mantle has type '" + typeof (value_mantle) + "'"; console.warn(message); } core[key] = value_mantle; // throw (new Error(message)); } } }); } } lib_object.patch = patch; /** * @author fenris */ function patched(core, mantle, deep) { if (deep === void 0) { deep = undefined; } var result = {}; patch(result, core, deep); patch(result, mantle, deep); return result; } lib_object.patched = patched; /** * @author fenris */ function attached(object, key, value) { var mantle = {}; mantle[key] = value; return patched(object, mantle, false); } lib_object.attached = attached; /** * @author fenris */ function copy(object) { return patched({}, object); } lib_object.copy = copy; })(lib_object || (lib_object = {})); /** * @desc adapters for old syntax * @author fenris */ var object_fetch = lib_object.fetch; var object_map = lib_object.map; var object_a2o = lib_object.from_array; var object_o2a = lib_object.to_array; var object_matches = lib_object.matches; var object_clash = lib_object.clash; /* This file is part of »bacterio-plankton:shape«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:shape« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:shape« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:shape«. If not, see <http://www.gnu.org/licenses/>. */ var lib_shape; (function (lib_shape) { /** * @author fenris */ var _pool = {}; /** * @author fenris */ var _aliases = {}; /** * @author fenris */ function list() { return [].concat(Object.keys(_pool)).concat(Object.keys(_aliases)); } lib_shape.list = list; /** * @author fenris */ function inspection_create() { return { "messages": [] }; } lib_shape.inspection_create = inspection_create; /** * @author fenris */ function inspection_add(main, message) { main.messages.push(message); } lib_shape.inspection_add = inspection_add; /** * @author fenris */ function inspection_extend(main, prefix, sub) { main.messages = main.messages.concat(sub.messages.map(message => `${prefix}: ${message}`)); } lib_shape.inspection_extend = inspection_extend; /** * @author fenris * @todo check for existing */ function register({ "name": name, "make": make, "inspect": _inspect, "stance": _stance, "show": _show, }) { const entry = { "name": name, "make": make, "inspect": _inspect, "stance": _stance, "show": _show, }; _pool[name] = entry; } lib_shape.register = register; /** * @author fenris * @todo check for existing */ function define_alias({ "name": name, "target": target, }) { const entry = { "name": "alias", "target": target, }; _aliases[name] = entry; } lib_shape.define_alias = define_alias; /** * @author fenris * @todo check for existing */ /* export function inspect_jstype( inspection, jstype_expected : string, value : any ) : void { let jstype_actual : string = typeof(value); if (jstype_actual === jstype_expected) { // all good } else { inspection_add(inspection, `expected JS-type '${jstype_expected}' but got '${jstype_actual}'`); } } */ /** * @author fenris */ function make(raw) { const name = raw["name"]; if (_aliases.hasOwnProperty(name)) { const alias = _aliases[name]; return alias.target; } else { if (_pool.hasOwnProperty(name)) { const entry = _pool[name]; const parameters = entry.make(raw["parameters"] || {}, make); return { "name": name, "parameters": parameters, }; } else { const message = `no shape registered with name '${name}'`; throw (new Error(message)); } } } lib_shape.make = make; /** * @author fenris */ function inspect(shape, value) { if (_pool.hasOwnProperty(shape.name)) { const entry = _pool[shape.name]; return entry.inspect(shape.parameters, value, inspect); } else { const message = `no shape registered with name '${shape.name}'`; throw (new Error(message)); } } lib_shape.inspect = inspect; /** * @author fenris */ function check(shape, value) { if (_pool.hasOwnProperty(shape.name)) { const entry = _pool[shape.name]; const inspection = inspect(shape, value); inspection.messages .forEach((message) => { console.warn(message); }); return (inspection.messages.length === 0); } else { const message = `no shape registered with name '${shape.name}'`; throw (new Error(message)); } } lib_shape.check = check; /** * @author fenris */ function stance(shape, bindings) { if (_pool.hasOwnProperty(shape.name)) { const entry = _pool[shape.name]; return entry.stance(shape.parameters, bindings, stance); } else { const message = `no shape registered with name '${shape.name}'`; throw (new Error(message)); } } lib_shape.stance = stance; /** * @author fenris */ function show(shape) { if (_pool.hasOwnProperty(shape.name)) { const entry = _pool[shape.name]; return entry.show(shape.parameters, show); } else { const message = `no shape registered with name '${shape.name}'`; throw (new Error(message)); } } lib_shape.show = show; })(lib_shape || (lib_shape = {})); /* This file is part of »bacterio-plankton:shape«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:shape« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:shape« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:shape«. If not, see <http://www.gnu.org/licenses/>. */ var lib_shape; (function (lib_shape) { /** * @author fenris */ function variable_make({ "name": name }) { if (name == undefined) { let message = `mandatory parameter 'name' missing`; throw (new Error(message)); } else { return { "name": name, }; } } /** * @author fenris */ function variable_inspect(parameters, value, _inspect) { let inspection = lib_shape.inspection_create(); let message = "cannot inspect a value against a type variable"; console.warn(message + "; will just pass ..."); // throw (new Error(message)); return inspection; } /** * @author fenris */ function variable_stance(parameters, bindings, _stance) { if (parameters.name in bindings) { return bindings[parameters.name]; } else { return { "name": "variable", "parameters": parameters }; } } /** * @author fenris */ function variable_show(parameters, _show) { let str; // core { str = ("$" + parameters.name); } return str; } /** * @author fenris */ lib_shape.register({ "name": "variable", "make": variable_make, "inspect": variable_inspect, "stance": variable_stance, "show": variable_show, }); })(lib_shape || (lib_shape = {})); /* This file is part of »bacterio-plankton:shape«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:shape« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:shape« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:shape«. If not, see <http://www.gnu.org/licenses/>. */ var lib_shape; (function (lib_shape) { /** * @author fenris */ function function_make({ "soft": soft = false, "shape_input": shape_input, "shape_output": shape_output, "defaultvalue": defaultvalue = undefined, }, _make) { if (shape_input === undefined) { let message = `mandatory parameter 'shape_input' missing`; throw (new Error(message)); } if (shape_output === undefined) { let message = `mandatory parameter 'shape_output' missing`; throw (new Error(message)); } if (defaultvalue === undefined) { defaultvalue = (soft ? null : (x => x)); } return { "soft": soft, "shape_input": _make(shape_input), "shape_output": _make(shape_output), "defaultvalue": defaultvalue, }; } /** * @author fenris */ function function_stance(parameters, bindings, _stance) { return { "name": "function", "parameters": { "soft": parameters.soft, "shape_input": _stance(parameters.shape_input, bindings), "shape_output": _stance(parameters.shape_output, bindings) } }; } /** * @author fenris * @todo closer look not possible? */ function function_inspect(parameters, value, _inspect) { let inspection = lib_shape.inspection_create(); if (value == undefined) { if (parameters.soft) { // all good } else { lib_shape.inspection_add(inspection, "null is not allowed"); } } else { let jstype_actual = typeof (value); let jstype_expected = "function"; if (jstype_actual === jstype_expected) { // all good? } else { lib_shape.inspection_add(inspection, `expected JS-type '${jstype_expected}' but got '${jstype_actual}'`); } } return inspection; } /** * @author fenris */ function function_show(parameters, _show) { let str; /* // core { str = "function"; } // in/out { str += ( "<" + [ _show(parameters.shape_input), _show(parameters.shape_output), ].join(",") + ">" ); } */ str = ""; str += ("(" + _show(parameters.shape_input) + " => " + _show(parameters.shape_output) + ")"); // soft { if (parameters.soft) { str = `~${str}`; } } return str; } /** * @author fenris */ lib_shape.register({ "name": "function", "make": function_make, "inspect": function_inspect, "stance": function_stance, "show": function_show, }); })(lib_shape || (lib_shape = {})); /* This file is part of »bacterio-plankton:shape«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:shape« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:shape« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:shape«. If not, see <http://www.gnu.org/licenses/>. */ var lib_shape; (function (lib_shape) { /** * @author fenris */ function void_make({ "soft": soft = true, }) { return { "soft": soft, }; } /** * @author fenris */ function void_inspect(parameters, value, _inspect) { let inspection = lib_shape.inspection_create(); if (value == undefined) { if (parameters.soft) { // all good } else { lib_shape.inspection_add(inspection, "null is not allowed"); } } else { // all good } return inspection; } /** * @author fenris */ function void_stance(parameters, bindings, _stance) { return { "name": "void", "parameters": parameters }; } /** * @author fenris */ function void_show(parameters, _show) { let str; // core { str = "void"; } // soft { if (parameters.soft) { str = `~${str}`; } } return str; } /** * @author fenris */ lib_shape.register({ "name": "void", "make": void_make, "inspect": void_inspect, "stance": void_stance, "show": void_show, }); })(lib_shape || (lib_shape = {})); /* This file is part of »bacterio-plankton:shape«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:shape« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:shape« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:shape«. If not, see <http://www.gnu.org/licenses/>. */ var lib_shape; (function (lib_shape) { /** * @author fenris */ function any_make({ "soft": soft = true, "mutable": mutable = true, "defaultvalue": defaultvalue = null, }) { return { "soft": soft, "mutable": mutable, "defaultvalue": defaultvalue, }; } /** * @author fenris */ function any_inspect(parameters, value, _inspect) { let inspection = lib_shape.inspection_create(); if (value == undefined) { if (parameters.soft) { // all good } else { lib_shape.inspection_add(inspection, "null is not allowed"); } } else { // all good } return inspection; } /** * @author fenris */ function any_stance(parameters, bindings, _stance) { return { "name": "any", "parameters": parameters }; } /** * @author fenris */ function any_show(parameters, _show) { let str; // core { str = "any"; } // soft { if (parameters.soft) { str = `~${str}`; } } return str; } /** * @author fenris */ lib_shape.register({ "name": "any", "make": any_make, "inspect": any_inspect, "stance": any_stance, "show": any_show, }); })(lib_shape || (lib_shape = {})); /* This file is part of »bacterio-plankton:shape«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:shape« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:shape« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:shape«. If not, see <http://www.gnu.org/licenses/>. */ var lib_shape; (function (lib_shape) { /** * @author fenris */ function boolean_make({ "soft": soft = false, "mutable": mutable = true, "defaultvalue": defaultvalue = undefined, }) { if (defaultvalue === undefined) { defaultvalue = (soft ? null : false); } return { "soft": soft, "mutable": mutable, "defaultvalue": defaultvalue, }; } /** * @author fenris */ function boolean_inspect(parameters, value, _inspect) { let inspection = lib_shape.inspection_create(); if (value == undefined) { if (parameters.soft) { // all good } else { lib_shape.inspection_add(inspection, "null is not allowed"); } } else { let jstype_actual = typeof (value); let jstype_expected = "boolean"; if (jstype_actual === jstype_expected) { // all good } else { lib_shape.inspection_add(inspection, `expected JS-type '${jstype_expected}' but got '${jstype_actual}'`); } } return inspection; } /** * @author fenris */ function boolean_stance(parameters, bindings, _stance) { return { "name": "boolean", "parameters": parameters }; } /** * @author fenris */ function boolean_show(parameters, _show) { let str; // core { str = "boolean"; } // soft { if (parameters.soft) { str = `~${str}`; } } return str; } /** * @author fenris */ lib_shape.register({ "name": "boolean", "make": boolean_make, "inspect": boolean_inspect, "stance": boolean_stance, "show": boolean_show, }); })(lib_shape || (lib_shape = {})); /* This file is part of »bacterio-plankton:shape«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:shape« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:shape« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:shape«. If not, see <http://www.gnu.org/licenses/>. */ var lib_shape; (function (lib_shape) { /** * @author fenris */ function int_make({ "soft": soft = false, "mutable": mutable = true, "defaultvalue": defaultvalue = undefined, "minimum": minimum = null, "maximum": maximum = null, }) { if (defaultvalue === undefined) { defaultvalue = (soft ? null : 0); } return { "soft": soft, "mutable": mutable, "defaultvalue": defaultvalue, "minimum": minimum, "maximum": maximum, }; } /** * @author fenris */ function int_inspect(parameters, value, _inspect) { let inspection = lib_shape.inspection_create(); if (value == undefined) { if (parameters.soft) { // all good } else { lib_shape.inspection_add(inspection, "null is not allowed"); } } else { let jstype_actual = typeof (value); let jstype_expected = "number"; if (jstype_actual === jstype_expected) { if (!isNaN(parseInt(value))) { if ((parameters.minimum === null) || (value >= parameters.minimum)) { if ((parameters.maximum === null) || (value <= parameters.maximum)) { // all good } else { lib_shape.inspection_add(inspection, `value is beyond maximum`); } } else { lib_shape.inspection_add(inspection, `value is below minimum`); } } else { lib_shape.inspection_add(inspection, `value is not parsable into a valid int`); } } else { lib_shape.inspection_add(inspection, `expected JS-type '${jstype_expected}' but got '${jstype_actual}'`); } } return inspection; } /** * @author fenris */ function int_stance(parameters, bindings, _stance) { return { "name": "int", "parameters": parameters }; } /** * @author fenris */ function int_show(parameters, _show) { let str; // core { str = "int"; } // soft { if (parameters.soft) { str = `~${str}`; } } return str; } /** * @author fenris */ lib_shape.register({ "name": "int", "make": int_make, "inspect": int_inspect, "stance": int_stance, "show": int_show, }); })(lib_shape || (lib_shape = {})); /* This file is part of »bacterio-plankton:shape«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:shape« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:shape« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:shape«. If not, see <http://www.gnu.org/licenses/>. */ var lib_shape; (function (lib_shape) { /** * @author fenris */ function float_make({ "soft": soft = false, "mutable": mutable = true, "defaultvalue": defaultvalue = undefined, }) { if (defaultvalue === undefined) { defaultvalue = (soft ? null : 0.0); } return { "soft": soft, "mutable": mutable, "defaultvalue": defaultvalue, }; } /** * @author fenris */ function float_inspect(parameters, value, _inspect) { let inspection = lib_shape.inspection_create(); if (value == undefined) { if (parameters.soft) { // all good } else { lib_shape.inspection_add(inspection, "null is not allowed"); } } else { let jstype_actual = typeof (value); let jstype_expected = "number"; if (jstype_actual === jstype_expected) { if (!isNaN(parseFloat(value))) { // all good } else { lib_shape.inspection_add(inspection, `value is not parsable into a valid float`); } } else { lib_shape.inspection_add(inspection, `expected JS-type '${jstype_expected}' but got '${jstype_actual}'`); } } return inspection; } /** * @author fenris */ function float_stance(parameters, bindings, _stance) { return { "name": "float", "parameters": parameters }; } /** * @author fenris */ function float_show(parameters, _show) { let str; // core { str = "float"; } // soft { if (parameters.soft) { str = `~${str}`; } } return str; } /** * @author fenris */ lib_shape.register({ "name": "float", "make": float_make, "inspect": float_inspect, "stance": float_stance, "show": float_show, }); })(lib_shape || (lib_shape = {})); /* This file is part of »bacterio-plankton:shape«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:shape« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:shape« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:shape«. If not, see <http://www.gnu.org/licenses/>. */ var lib_shape; (function (lib_shape) { /** * @author fenris */ function string_make({ "soft": soft = false, "mutable": mutable = true, "defaultvalue": defaultvalue = undefined, "long": long = false, }) { if (defaultvalue === undefined) { defaultvalue = (soft ? null : ""); } return { "soft": soft, "mutable": mutable, "defaultvalue": defaultvalue, "long": long, }; } /** * @author fenris */ function string_inspect(parameters, value, _inspect) { let inspection = lib_shape.inspection_create(); if (value == undefined) { if (parameters.soft) { // all good } else { lib_shape.inspection_add(inspection, "null is not allowed"); } } else { let jstype_actual = typeof (value); let jstype_expected = "string"; if (jstype_actual === jstype_expected) { // all good } else { lib_shape.inspection_add(inspection, `expected JS-type '${jstype_expected}' but got '${jstype_actual}'`); } } return inspection; } /** * @author fenris */ function string_stance(parameters, bindings, _stance) { return { "name": "string", "parameters": parameters }; } /** * @author fenris */ function string_show(parameters, _show) { let str; // core { str = "string"; } // soft { if (parameters.soft) { str = `~${str}`; } } return str; } /** * @author fenris */ lib_shape.register({ "name": "string", "make": string_make, "inspect": string_inspect, "stance": string_stance, "show": string_show, }); })(lib_shape || (lib_shape = {})); /* This file is part of »bacterio-plankton:shape«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:shape« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:shape« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:shape«. If not, see <http://www.gnu.org/licenses/>. */ var lib_shape; (function (lib_shape) { /** * @author fenris */ function email_make({ "soft": soft = false, "mutable": mutable = true, "defaultvalue": defaultvalue = undefined, }) { if (defaultvalue === undefined) { defaultvalue = (soft ? null : ""); } return { "soft": soft, "mutable": mutable, "defaultvalue": defaultvalue, }; } /** * @author fenris */ function email_inspect(parameters, value, _inspect) { let inspection = lib_shape.inspection_create(); if (value == undefined) { if (parameters.soft) { // all good } else { lib_shape.inspection_add(inspection, "null is not allowed"); } } else { let jstype_actual = typeof (value); let jstype_expected = "string"; if (jstype_actual === jstype_expected) { // all good } else { lib_shape.inspection_add(inspection, `expected JS-type '${jstype_expected}' but got '${jstype_actual}'`); } } return inspection; } /** * @author fenris */ function email_stance(parameters, bindings, _stance) { return { "name": "email", "parameters": parameters }; } /** * @author fenris */ function email_show(parameters, _show) { let str; // core { str = "email"; } // soft { if (parameters.soft) { str = `~${str}`; } } return str; } /** * @author fenris */ lib_shape.register({ "name": "email", "make": email_make, "inspect": email_inspect, "stance": email_stance, "show": email_show, }); })(lib_shape || (lib_shape = {})); /* This file is part of »bacterio-plankton:shape«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:shape« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:shape« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:shape«. If not, see <http://www.gnu.org/licenses/>. */ var lib_shape; (function (lib_shape) { /** * @author fenris */ function url_make({ "soft": soft = false, "mutable": mutable = true, "defaultvalue": defaultvalue = undefined, "type": type = "link", }) { if (defaultvalue === undefined) { defaultvalue = (soft ? null : ""); } return { "soft": soft, "mutable": mutable, "defaultvalue": defaultvalue, "type": type, }; } /** * @author fenris */ function url_inspect(parameters, value, _inspect) { let inspection = lib_shape.inspection_create(); if (value == undefined) { if (parameters.soft) { // all good } else { lib_shape.inspection_add(inspection, "null is not allowed"); } } else { let jstype_actual = typeof (value); let jstype_expected = "string"; if (jstype_actual === jstype_expected) { // all good } else { lib_shape.inspection_add(inspection, `expected JS-type '${jstype_expected}' but got '${jstype_actual}'`); } } return inspection; } /** * @author fenris */ function url_stance(parameters, bindings, _stance) { return { "name": "url", "parameters": parameters }; } /** * @author fenris */ function url_show(parameters, _show) { let str; // core { str = "url"; } // soft { if (parameters.soft) { str = `~${str}`; } } return str; } /** * @author fenris */ lib_shape.register({ "name": "url", "make": url_make, "inspect": url_inspect, "stance": url_stance, "show": url_show, }); })(lib_shape || (lib_shape = {})); /* This file is part of »bacterio-plankton:shape«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:shape« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:shape« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:shape«. If not, see <http://www.gnu.org/licenses/>. */ var lib_shape; (function (lib_shape) { /** * @author fenris */ function markdown_make({ "soft": soft = false, "mutable": mutable = true, "defaultvalue": defaultvalue = undefined, "type": type = "link", }) { if (defaultvalue === undefined) { defaultvalue = (soft ? null : ""); } return { "soft": soft, "mutable": mutable, "defaultvalue": defaultvalue, "type": type, }; } /** * @author fenris */ function markdown_inspect(parameters, value, _inspect) { let inspection = lib_shape.inspection_create(); if (value == undefined) { if (parameters.soft) { // all good } else { lib_shape.inspection_add(inspection, "null is not allowed"); } } else { let jstype_actual = typeof (value); let jstype_expected = "string"; if (jstype_actual === jstype_expected) { // all good } else { lib_shape.inspection_add(inspection, `expected JS-type '${jstype_expected}' but got '${jstype_actual}'`); } } return inspection; } /** * @author fenris */ function markdown_stance(parameters, bindings, _stance) { return { "name": "markdown", "parameters": parameters }; } /** * @author fenris */ function markdown_show(parameters, _show) { let str; // core { str = "markdown"; } // soft { if (parameters.soft) { str = `~${str}`; } } return str; } /** * @author fenris */ lib_shape.register({ "name": "markdown", "make": markdown_make, "inspect": markdown_inspect, "stance": markdown_stance, "show": markdown_show, }); })(lib_shape || (lib_shape = {})); /* This file is part of »bacterio-plankton:shape«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:shape« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:shape« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:shape«. If not, see <http://www.gnu.org/licenses/>. */ var lib_shape; (function (lib_shape) { /** * @author fenris */ function time_make({ "soft": soft = false, "mutable": mutable = true, "defaultvalue": defaultvalue = undefined, }) { if (defaultvalue === undefined) { defaultvalue = (soft ? null : ""); } return { "soft": soft, "mutable": mutable, "defaultvalue": defaultvalue, }; } /** * @author fenris */ function time_inspect(parameters, value, _inspect) { let inspection = lib_shape.inspection_create(); if (value == undefined) { if (parameters.soft) { // all good } else { lib_shape.inspection_add(inspection, "null is not allowed"); } } else { let jstype_actual = typeof (value); let jstype_expected = "object"; if (jstype_actual === jstype_expected) { // all good? } else { lib_shape.inspection_add(inspection, `expected JS-type '${jstype_expected}' but got '${jstype_actual}'`); } } return inspection; } /** * @author fenris */ function time_stance(parameters, bindings, _stance) { return { "name": "time", "parameters": parameters }; } /** * @author fenris */ function time_show(parameters, _show) { let str; // core { str = "time"; } // soft { if (parameters.soft) { str = `~${str}`; } } return str; } /** * @author fenris */ lib_shape.register({ "name": "time", "make": time_make, "inspect": time_inspect, "stance": time_stance, "show": time_show, }); })(lib_shape || (lib_shape = {})); /* This file is part of »bacterio-plankton:shape«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:shape« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:shape« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:shape«. If not, see <http://www.gnu.org/licenses/>. */ var lib_shape; (function (lib_shape) { /** * @author fenris */ function array_make({ "soft": soft = false, "mutable": mutable = true, "shape_element": shape_element, "defaultvalue": defaultvalue = undefined, }, _make) { if (shape_element === undefined) { let message = `mandatory parameter 'shape_element' missing`; throw (new Error(message)); } if (defaultvalue === undefined) { defaultvalue = (soft ? null : []); } return { "soft": soft, "mutable": mutable, "shape_element": _make(shape_element), "defaultvalue": defaultvalue, }; } /** * @author fenris */ function array_inspect(parameters, value, _inspect) { let inspection = lib_shape.inspection_create(); if (value == undefined) { if (parameters.soft) { // all good } else { lib_shape.inspection_add(inspection, "null is not allowed"); } } else { let jstype_actual = typeof (value); let jstype_expected = "object"; if (jstype_actual === jstype_expected) { if (value instanceof Array) { value.forEach((element, index) => { lib_shape.inspection_extend(inspection, `element #${index.toFixed(0)}`, _inspect(parameters.shape_element, element)); }); } else { lib_shape.inspection_add(inspection, `value does not seem to be an array-instance`); } } else { lib_shape.inspection_add(inspection, `expected JS-type '${jstype_expected}' but got '${jstype_actual}'`); } } return inspection; } /** * @author fenris */ function array_stance(parameters, bindings, _stance) { return { "name": "array", "parameters": { "soft": parameters.soft, "shape_element": _stance(parameters.shape_element, bindings) } }; } /** * @author fenris */ function array_show(parameters, _show) { let str; // core { str = "array"; } // shape_element { str += ("<" + _show(parameters.shape_element) + ">"); } // soft { if (parameters.soft) { str = `~${str}`; } } return str; } /** * @author fenris */ lib_shape.register({ "name": "array", "make": array_make, "inspect": array_inspect, "stance": array_stance, "show": array_show, }); })(lib_shape || (lib_shape = {})); /* This file is part of »bacterio-plankton:shape«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:shape« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:shape« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:shape«. If not, see <http://www.gnu.org/licenses/>. */ var lib_shape; (function (lib_shape) { /** * @author fenris */ function enumeration_make({ "soft": soft = false, "mutable": mutable = true, "shape_option": shape_option, "options": options, "defaultvalue": defaultvalue = undefined, }, _make) { if (shape_option === undefined) { let message = `mandatory parameter 'shape_option' missing`; throw (new Error(message)); } if (options === undefined) { let message = `mandatory parameter 'options' missing`; throw (new Error(message)); } if (defaultvalue === undefined) { defaultvalue = (soft ? null : options[0].value); } return { "soft": soft, "mutable": mutable, "shape_option": _make(shape_option), "options": options, "defaultvalue": defaultvalue, }; } /** * @author fenris */ function enumeration_inspect(parameters, value, _inspect) { let inspection = lib_shape.inspection_create(); if (value == undefined) { if (parameters.soft) { // all good } else { lib_shape.inspection_add(inspection, "null is not allowed"); } } else { lib_shape.inspection_extend(inspection, `given value for enumeration`, _inspect(parameters.shape_option, value)); // TODO: check if corresponding option exists } return inspection; } /** * @author fenris */ function enumeration_stance(parameters, bindings, _stance) { return { "name": "enumeration", "parameters": { "soft": parameters.soft, "shape_option": _stance(parameters.shape_option, bindings), "options": parameters.options, } }; } /** * @author fenris */ function enumeration_show(parameters, _show) { let str; // core { str = "enumeration"; } // shape_option { str += ("<" + parameters.options.map(option => String(option)).join(",") + ":" + _show(parameters.shape_option) + ">"); } // soft { if (parameters.soft) { str = `~${str}`; } } return str; } /** * @author fenris */ lib_shape.register({ "name": "enumeration", "make": enumeration_make, "inspect": enumeration_inspect, "stance": enumeration_stance, "show": enumeration_show, }); })(lib_shape || (lib_shape = {})); /* This file is part of »bacterio-plankton:shape«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:shape« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:shape« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:shape«. If not, see <http://www.gnu.org/licenses/>. */ var lib_shape; (function (lib_shape) { /** * @author fenris */ function date_make({ "soft": soft = false, "mutable": mutable = true, "defaultvalue": defaultvalue = undefined, }) { if (defaultvalue === undefined) { defaultvalue = (soft ? null : (new Date(Date.now()))); } return { "soft": soft, "mutable": mutable, "defaultvalue": defaultvalue, }; } /** * @author fenris */ function date_inspect(parameters, value, _inspect) { let inspection = lib_shape.inspection_create(); if (value == undefined) { if (parameters.soft) { // all good } else { lib_shape.inspection_add(inspection, "null is not allowed"); } } else { let jstype_actual = typeof (value); let jstype_expected = "object"; if (jstype_actual === jstype_expected) { if (value instanceof Date) { // all gode } else { lib_shape.inspection_add(inspection, `value does not seem to be a date-instance`); } } else { lib_shape.inspection_add(inspection, `expected JS-type '${jstype_expected}' but got '${jstype_actual}'`); } } return inspection; } /** * @author fenris */ function date_stance(parameters, bindings, _stance) { return { "name": "date", "parameters": parameters }; } /** * @author fenris */ function date_show(parameters, _show) { let str; // core { str = "date"; } // soft { if (parameters.soft) { str = `~${str}`; } } return str; } /** * @author fenris */ lib_shape.register({ "name": "date", "make": date_make, "inspect": date_inspect, "stance": date_stance, "show": date_show, }); })(lib_shape || (lib_shape = {})); /* This file is part of »bacterio-plankton:shape«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:shape« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:shape« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:shape«. If not, see <http://www.gnu.org/licenses/>. */ var lib_shape; (function (lib_shape) { /** * @author fenris */ function record_make({ "soft": soft = false, "mutable": mutable = true, "fields": fields = [], "defaultvalue": defaultvalue = undefined, }, _make) { let fields_ = (fields .map((field) => ({ "name": field.name, "shape": _make(field.shape) }))); if (defaultvalue === undefined) { if (soft) { defaultvalue = null; } else { defaultvalue = {}; fields_.forEach(field => { defaultvalue[field.name] = field.shape.parameters["defaultvalue"]; }); } } return { "soft": soft, "mutable": mutable, "fields": fields_, "defaultvalue": defaultvalue, }; } /** * @author fenris */ function record_inspect(parameters, value, _inspect) { let inspection = lib_shape.inspection_create(); if (value == undefined) { if (parameters.soft) { // all good } else { lib_shape.inspection_add(inspection, "null is not allowed"); } } else { let jstype_actual = typeof (value); let jstype_expected = "object"; if (jstype_actual === jstype_expected) { parameters.fields.forEach(field => { let value_ = value[field.name]; lib_shape.inspection_extend(inspection, `field '${field.name}'`, _inspect(field.shape, value_)); }); } else { lib_shape.inspection_add(inspection, `expected JS-type '${jstype_expected}' but got '${jstype_actual}'`); } } return inspection; } /** * @author fenris */ function record_stance(parameters, bindings, _stance) { return { "name": "record", "parameters": { "soft": parameters.soft, "fields": parameters.fields.map(field => { return { "name": field.name, "shape": _stance(field.shape, bindings) }; }) } }; } /** * @author fenris */ function record_show(parameters, _show) { let str; // core { str = "record"; } // fields { str += ("<" + parameters.fields.map(field => (field.name + ":" + _show(field.shape))).join(",") + ">"); } // soft { if (parameters.soft) { str = `~${str}`; } } return str; } /** * @author fenris */ lib_shape.register({ "name": "record", "make": record_make, "inspect": record_inspect, "stance": record_stance, "show": record_show, }); })(lib_shape || (lib_shape = {})); /* This file is part of »bacterio-plankton:shape«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:shape« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:shape« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:shape«. If not, see <http://www.gnu.org/licenses/>. */ var lib_shape; (function (lib_shape) { /** * @author fenris */ function map_make({ "soft": soft = false, "mutable": mutable = true, "shape_key": shape_key, "shape_value": shape_value, "defaultvalue": defaultvalue = undefined, }, _make) { if (shape_key === undefined) { let message = `mandatory parameter 'shape_key' missing`; throw (new Error(message)); } if (shape_value === undefined) { let message = `mandatory parameter 'shape_value' missing`; throw (new Error(message)); } if (defaultvalue === undefined) { defaultvalue = (soft ? null : {}); } return { "soft": soft, "mutable": mutable, "shape_key": _make(shape_key), "shape_value": _make(shape_value), "defaultvalue": defaultvalue, }; } /** * @author fenris */ function map_stance(parameters, bindings, _stance) { return { "name": "map", "parameters": { "soft": parameters.soft, "shape_key": _stance(parameters.shape_key, bindings), "shape_value": _stance(parameters.shape_value, bindings) } }; } /** * @author fenris * @todo closer look not possible? */ function map_inspect(parameters, value, _inspect) { let inspection = lib_shape.inspection_create(); if (value == undefined) { if (parameters.soft) { // all good } else { lib_shape.inspection_add(inspection, "null is not allowed"); } } else { let jstype_actual = typeof (value); let jstype_expected = "object"; if (jstype_actual === jstype_expected) { Object.keys(value).forEach(key => { lib_shape.inspection_extend(inspection, `key '${key}'`, _inspect(parameters.shape_key, key)); let value_ = value[key]; lib_shape.inspection_extend(inspection, `value for key '${key}'`, _inspect(parameters.shape_value, value_)); }); } else { lib_shape.inspection_add(inspection, `expected JS-type '${jstype_expected}' but got '${jstype_actual}'`); } } return inspection; } /** * @author fenris */ function map_show(parameters, _show) { let str; // core { str = "map"; } // in/out { str += ("<" + [ _show(parameters.shape_key), _show(parameters.shape_value), ].join(",") + ">"); } // soft { if (parameters.soft) { str = `~${str}`; } } return str; } /** * @author fenris */ lib_shape.register({ "name": "map", "make": map_make, "inspect": map_inspect, "stance": map_stance, "show": map_show, }); })(lib_shape || (lib_shape = {})); /* This file is part of »bacterio-plankton:shape«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:shape« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:shape« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:shape«. If not, see <http://www.gnu.org/licenses/>. */ var lib_shape; (function (lib_shape) { /** * @author fenris */ function maybe_make({ "soft": soft = false, "shape_value": shape_value, "defaultvalue": defaultvalue = undefined, }, _make) { if (shape_value === undefined) { let message = `mandatory parameter 'shape_value' missing`; throw (new Error(message)); } if (defaultvalue === undefined) { defaultvalue = (soft ? null : { "kind": "nothing" }); } return { "soft": soft, "shape_value": _make(shape_value), "defaultvalue": defaultvalue, }; } /** * @author fenris */ function maybe_inspect(parameters, value, _inspect) { let inspection = lib_shape.inspection_create(); if (value == undefined) { if (parameters.soft) { // all good } else { lib_shape.inspection_add(inspection, "null is not allowed"); } } else { let jstype_actual = typeof (value); let jstype_expected = "object"; if (jstype_actual === jstype_expected) { if ("kind" in value) { // all good? } else { lib_shape.inspection_add(inspection, `field 'kind' missing`); } } else { lib_shape.inspection_add(inspection, `expected JS-type '${jstype_expected}' but got '${jstype_actual}'`); } } return inspection; } /** * @author fenris */ function maybe_stance(parameters, bindings, _stance) { return { "name": "maybe", "parameters": { "soft": parameters.soft, "shape_value": _stance(parameters.shape_value, bindings) } }; } /** * @author fenris */ function maybe_show(parameters, _show) { let str; // core { str = "maybe"; } // shape_value { str += ("<" + _show(parameters.shape_value) + ">"); } // soft { if (parameters.soft) { str = `~${str}`; } } return str; } /** * @author fenris */ lib_shape.register({ "name": "maybe", "make": maybe_make, "inspect": maybe_inspect, "stance": maybe_stance, "show": maybe_show, }); })(lib_shape || (lib_shape = {})); /* This file is part of »bacterio-plankton:shape«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:shape« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:shape« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:shape«. If not, see <http://www.gnu.org/licenses/>. */ var lib_shape; (function (lib_shape) { /** * @author fenris */ function promise_make({ "soft": soft = false, "shape_result": shape_result, "shape_reason": shape_reason, "defaultvalue": defaultvalue = undefined, }, _make) { if (shape_result === undefined) { let message = `mandatory parameter 'shape_result' missing`; throw (new Error(message)); } if (shape_reason === undefined) { let message = `mandatory parameter 'shape_reason' missing`; throw (new Error(message)); } if (defaultvalue === undefined) { defaultvalue = (soft ? null : Promise.resolve(null)); } return { "soft": soft, "shape_result": _make(shape_result), "shape_reason": _make(shape_reason), "defaultvalue": defaultvalue, }; } /** * @author fenris */ function promise_stance(parameters, bindings, _stance) { return { "name": "promise", "parameters": { "soft": parameters.soft, "shape_result": _stance(parameters.shape_result, bindings), "shape_reason": _stance(parameters.shape_reason, bindings) } }; } /** * @author fenris * @todo closer look not possible? */ function promise_inspect(parameters, value, _inspect) { let inspection = lib_shape.inspection_create(); if (value == undefined) { if (parameters.soft) { // all good } else { lib_shape.inspection_add(inspection, "null is not allowed"); } } else { let jstype_actual = typeof (value); let jstype_expected = "object"; if (jstype_actual === jstype_expected) { if (value instanceof Promise /*<any, any>*/) { // all good? } else { lib_shape.inspection_add(inspection, `values is not an Promise-instance`); } } else { lib_shape.inspection_add(inspection, `expected JS-type '${jstype_expected}' but got '${jstype_actual}'`); } } return inspection; } /** * @author fenris */ function promise_show(parameters, _show) { let str; // core { str = "promise"; } // in/out { str += ("<" + [ _show(parameters.shape_result), _show(parameters.shape_reason), ].join(",") + ">"); } // soft { if (parameters.soft) { str = `~${str}`; } } return str; } /** * @author fenris */ lib_shape.register({ "name": "promise", "make": promise_make, "inspect": promise_inspect, "stance": promise_stance, "show": promise_show, }); })(lib_shape || (lib_shape = {})); /* This file is part of »bacterio-plankton:shape«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:shape« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:shape« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:shape«. If not, see <http://www.gnu.org/licenses/>. */ var lib_shape; (function (lib_shape) { /** * @author fenris * @todo use a treemap-function (but first make one :P) */ function adjust_labels(shape, transformator) { if (false) { } else if (shape.name === "enumeration") { return { "name": "enumeration", "parameters": { "soft": shape.parameters["soft"], "shape_option": adjust_labels(shape.parameters["shape_option"], transformator), "options": shape.parameters["options"].map(option => { return { "value": option.value, "label": transformator(option.label), }; }), "defaultvalue": shape.parameters["defaultvalue"], } }; } else if (shape.name === "array") { return { "name": "array", "parameters": { "soft": shape.parameters["soft"], "shape_element": adjust_labels(shape.parameters["shape_element"], transformator), "defaultvalue": shape.parameters["defaultvalue"], } }; } else if (shape.name === "record") { return { "name": "record", "parameters": { "soft": shape.parameters["soft"], "fields": shape.parameters["fields"].map(field => { return { "name": field.name, "shape": adjust_labels(field.shape, transformator), "label": transformator(field.label), }; }), "defaultvalue": shape.parameters["defaultvalue"], } }; } // lil' hacky … else { // shape["label"] = ((shape["label"] == null) ? null : transformator(shape["label"])); return shape; } } lib_shape.adjust_labels = adjust_labels; /** * @author fenris */ function register_(id, factory) { const message = "not implemented; dummy impl"; console.warn(message); } lib_shape.register_ = register_; /** * @author fenris */ function retrieve_(id) { const message = "not implemented; returning dummy"; console.warn(message); return { "name": "any" }; } lib_shape.retrieve_ = retrieve_; })(lib_shape || (lib_shape = {})); /* This file is part of »bacterio-plankton:trait«. Copyright 2016-2018 greenscale <info@greenscale.de> »bacterio-plankton:trait« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:trait« is distributed in the hope that it will be callful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:trait«. If not, see <http://www.gnu.org/licenses/>. */ var lib_trait; (function (lib_trait) { /** * @author fenris */ function domain_make(_a, _make) { var _b = _a["soft"], soft = _b === void 0 ? false : _b, kind = _a["kind"]; return { "soft": soft, "kind": kind }; } /** * @author fenris */ function domain_inspect(parameters, value, _inspect) { var inspection = lib_shape.inspection_create(); if (value == undefined) { if (parameters.soft) { // all good } else { lib_shape.inspection_add(inspection, "null is not allowed"); } } else { var jstype_actual = typeof (value); var jstype_expected = "object"; if (jstype_actual === jstype_expected) { if ("domain" in value) { var domain_actual = value["domain"]["kind"]; var domain_expected = parameters.kind; if (domain_actual == domain_expected) { // all good? } else { lib_shape.inspection_add(inspection, "expected domain-kind '" + domain_expected + "' but got '" + domain_actual + "'"); } } else { lib_shape.inspection_add(inspection, "field 'domain' missing"); } } else { lib_shape.inspection_add(inspection, "expected JS-type '" + jstype_expected + "' but got '" + jstype_actual + "'"); } } return inspection; } /** * @author fenris */ function domain_stance(parameters, bindings, _stance) { return { "name": "domain", "parameters": { "soft": parameters.soft, "kind": parameters.kind } }; } /** * @author fenris */ function domain_show(parameters, _show) { var str; // core { str = ("#" + parameters.kind); } /* // parameters { str += (":" + JSON.stringify(parameters.parameters)) } */ // soft { if (parameters.soft) { str = "~" + str; } } return str; } /** * @author fenris */ lib_shape.register({ "name": "domain", "make": domain_make, "inspect": domain_inspect, "stance": domain_stance, "show": domain_show }); })(lib_trait || (lib_trait = {})); /* This file is part of »bacterio-plankton:trait«. Copyright 2016-2018 greenscale <info@greenscale.de> »bacterio-plankton:trait« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:trait« is distributed in the hope that it will be callful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:trait«. If not, see <http://www.gnu.org/licenses/>. */ /** * @author fenris */ var lib_trait; (function (lib_trait) { /** * @author fenris */ lib_trait._verbosity = 1; /** * @author fenris */ lib_trait._logprefix = "[lib_trait] "; /** * @desc holds all defined traits * @author fenris */ var _pool = {}; /** * @author fenris */ var _typechecker = function (value, shape, logger) { var messages = lib_shape.inspect(shape, value).messages; messages.forEach(logger); return (messages.length === 0); }; /** * @author fenris */ function log(_a) { var message = _a["message"], _b = _a["kind"], kind = _b === void 0 ? "log" : _b, _c = _a["level"], level = _c === void 0 ? 0 : _c; if (level <= lib_trait._verbosity) { var message_ = "" + lib_trait._logprefix + message; switch (kind) { default: case "log": { console.log(message_); break; } case "information": case "info": { console.info(message_); break; } case "warning": case "warn": { console.warn(message_); break; } case "error": case "err": { console.error(message_); break; } } } } /** * @author fenris */ function set_typechecker(typechecker) { _typechecker = typechecker; } lib_trait.set_typechecker = set_typechecker; /** * @author fenris */ function domain_instance(domain, value) { return { "domain": domain, "value": value }; } lib_trait.domain_instance = domain_instance; /** * @desc adds a trait * @author fenris */ function define(trait_id, variable_conditions, facets_raw) { if (trait_id in _pool) { var message = "trait '" + trait_id + "' already registered"; throw (new Error(message)); } else { var trait_1 = { "variables": {}, "facets": {}, "attendants": {} }; Object.keys(variable_conditions).forEach(function (variable_name) { var variable = { "conditions": variable_conditions[variable_name], "bindings": {} }; trait_1.variables[variable_name] = variable; }); Object.keys(facets_raw).forEach(function (facet_name) { var facet_raw = facets_raw[facet_name]; var facet = { "shape": lib_shape.make(facet_raw.shape || { "name": "any" }), "description": facet_raw.description, "implementation": facet_raw.implementation, "handlers": {} }; trait_1.facets[facet_name] = facet; }); _pool[trait_id] = trait_1; } } lib_trait.define = define; /** * @desc adhoc binding * @author fenris */ function attend(trait_id, domain_kind, bindings, handlers, condition) { if (condition === void 0) { condition = null; } var strict_binding = true; var strict_handling = true; if (!_pool.hasOwnProperty(trait_id)) { var message = "no trait '" + trait_id + "'"; throw (new Error(message)); } else { var trait_2 = _pool[trait_id]; // assignments { // variables { Object.keys(bindings).forEach(function (variable_name) { if (!trait_2.variables.hasOwnProperty(variable_name)) { var message = ""; message += "no variable '" + variable_name + "'"; message += " to bind in trait '" + trait_id + "'"; log({ "message": message, "kind": "warning" }); // throw (new Error(message)); } else { var variable = trait_2.variables[variable_name]; if (variable.bindings.hasOwnProperty(domain_kind)) { var message = ""; message += "variable '" + variable_name + "'"; message += " already bound for domain '" + domain_kind + "'"; message += " in trait '" + trait_id + "'"; throw (new Error(message)); } else { variable.bindings[domain_kind] = lib_shape.make(bindings[variable_name]); } } }); } // facets { Object.keys(handlers).forEach(function (facet_name) { if (!trait_2.facets.hasOwnProperty(facet_name)) { var message = ""; message += "no facet '" + facet_name + "'"; message += " to implement at trait '" + trait_id + "'"; log({ "kind": "warning", "message": message }); // throw (new Error(message)); } else { var facet = trait_2.facets[facet_name]; var intrinsic = (facet.implementation != undefined); if (intrinsic) { var message = "shadowing intrinsic facet '" + facet_name + "'"; console.warn(message); } else { // do nothing } if (facet.handlers.hasOwnProperty(domain_kind)) { var message = ""; message += "facet '" + facet_name + "'"; message += " already implemented in domain '" + domain_kind + "'"; message += " at trait '" + trait_id + "'"; throw (new Error(message)); } else { facet.handlers[domain_kind] = handlers[facet_name]; } } }); } // attendants { trait_2.attendants[domain_kind] = { "condition": condition }; } } // checks { var messages_1 = []; // variables { if (strict_binding) { var variable_names_given_1 = Object.keys(bindings); var variable_names_present = Object.keys(trait_2.variables); var variable_names_missing = variable_names_present.filter(function (variable_name) { return (!variable_names_given_1.some(function (variable_name_) { return (variable_name == variable_name_); })); }); variable_names_missing.forEach(function (variable_name) { var message = "binding missing for variable '" + variable_name + "'"; messages_1.push(message); }); } else { // do nothing } } // facets { if (strict_handling) { var facet_names_given_1 = Object.keys(handlers); var facet_names_needed = (Object.keys(trait_2.facets).filter(function (facet_name) { return (trait_2.facets[facet_name].implementation == undefined); })); var facet_names_missing = (facet_names_needed.filter(function (facet_name) { return (!facet_names_given_1.some(function (facet_name_) { return (facet_name === facet_name_); })); })); facet_names_missing.forEach(function (facet_name) { var message = "implementation missing for the facet '" + facet_name + "'"; messages_1.push(message); }); } else { // do nothing } } if (messages_1.length > 0) { var message = ""; message += "assignment of domain '" + domain_kind + "'"; message += " to trait '" + trait_id + "'"; message += " incomplete: " + messages_1.join(", "); throw (new Error(message)); } } } } lib_trait.attend = attend; /** * @desc calls a facet from a trait according to a given domain * @author fenris */ function call(trait_id, facet_name, domain, check) { if (check === void 0) { check = true; } if (!_pool.hasOwnProperty(trait_id)) { var message = "no trait '" + trait_id + "'"; throw (new Error(message)); } else { var trait_3 = _pool[trait_id]; if (!trait_3.facets.hasOwnProperty(facet_name)) { var message = "no facet '" + facet_name + "' in trait '" + trait_id + "'"; throw (new Error(message)); } else { var facet = trait_3.facets[facet_name]; var intrinsic = (facet.implementation !== undefined); var result = void 0; if (intrinsic) { result = facet.implementation(function (facet_name_) { return call(trait_id, facet_name_, domain, check); }); } else { if (!facet.handlers.hasOwnProperty(domain.kind)) { var message = ""; message += "implementation missing"; message += " at trait '" + trait_id + "'"; message += " for facet '" + facet_name + "'"; message += " in domain '" + domain.kind + "'"; throw (new Error(message)); } else { var show_shape = (function (shape) { return lib_shape.show(shape); }); var handler = facet.handlers[domain.kind]; var bindings_1 = {}; Object.keys(trait_3.variables).forEach(function (variable_name) { bindings_1[variable_name] = trait_3.variables[variable_name].bindings[domain.kind]; }); result = handler(domain.parameters); if (check && (_typechecker !== null) && (facet.shape !== undefined)) { var shape = lib_shape.stance(facet.shape, bindings_1); // console.info("expected shape of result: " + lib_shape.show(shape)); console.info("result: ", result); var valid = _typechecker(result, shape, function (message) { return log({ "kind": "warning", "message": "[typechecker] [result] " + message }); }); if (!valid) { var message = ""; message += "result '" + instance_show(result) + "'"; message += " doesn't match the expected type '" + show_shape(shape) + "'"; message += " at trait '" + trait_id + "'"; message += " for facet '" + facet_name + "'"; message += " in domain '" + domain.kind + "'"; log({ "kind": "warning", "message": message }); } { var condition = trait_3.attendants[domain.kind].condition; if (condition != null) { var attends = function (trait_id_, domain_) { return (_pool.hasOwnProperty(trait_id_) && _pool[trait_id_].attendants.hasOwnProperty(domain_.kind)); }; if (!condition(attends)(domain.parameters)) { var message = ""; message += "condition not fulfilled"; message += " at trait '" + trait_id + "'"; message += " for facet '" + facet_name + "'"; message += " in domain '" + domain.kind + "'"; log({ "kind": "warning", "message": message }); } else { // do nothing } } else { // do nothing } } } } } return result; } } } lib_trait.call = call; /** * @author fenris */ function explain(trait_id) { if (!_pool.hasOwnProperty(trait_id)) { var message = "no trait '" + trait_id + "'"; throw (new Error(message)); } else { var trait_4 = _pool[trait_id]; var str_1 = ""; // head { str_1 += "<<" + trait_id + ">>"; str_1 += (" " + "(" + (Object.keys(trait_4.variables) .map(function (variable_name) { var shape = lib_shape.make({ "name": "variable", "parameters": { "name": variable_name } }); return lib_shape.show(shape); }) .join(",")) + ")"); str_1 += "\n"; } // facets { Object.keys(trait_4.facets) .forEach(function (facet_name) { var facet = trait_4.facets[facet_name]; // str += "\t"; str_1 += "* "; str_1 += ((facet.implementation != undefined) ? ("[" + facet_name + "]") : facet_name); str_1 += (" : " + lib_shape.show(facet.shape)); if (facet.description !== undefined) { str_1 += (" ~ " + "" + facet.description + ""); } str_1 += "\n"; }); } return str_1; } } lib_trait.explain = explain; /** * @author fenris */ function explain_all() { return Object.keys(_pool).map(function (trait_id) { return explain(trait_id); }).join("\n"); } lib_trait.explain_all = explain_all; })(lib_trait || (lib_trait = {})); /* This file is part of »bacterio-plankton:trait«. Copyright 2016-2018 greenscale <info@greenscale.de> »bacterio-plankton:trait« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:trait« is distributed in the hope that it will be callful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:trait«. If not, see <http://www.gnu.org/licenses/>. */ /** * @author fenris */ var lib_trait; (function (lib_trait) { /** * @desc the ability to check for equality with another element of the same domain * @author fenris */ function define_collatable() { lib_trait.define("collatable", { "value": [] }, { "collate": { "shape": { "name": "function", "parameters": { "shape_input": { "name": "variable", "parameters": { "name": "value" } }, "shape_output": { "name": "function", "parameters": { "shape_input": { "name": "variable", "parameters": { "name": "value" } }, "shape_output": { "name": "boolean" } } } } } } }); lib_trait.attend("collatable", "crude", { "value": { "name": "any" } }, { "collate": function () { return function (instance) { return function (other) { if (typeof (instance) === "object") { if (instance == null) { return (other == null); } else { if ("_collate" in instance) { return instance["_collate"](other); } else { var message = ("[collate]" + " " + "object has no such method"); throw (new Error(message)); } } } else { if (lib_trait._verbosity >= 1) { var message = ("[collate]" + " " + "primitive value; using default implementation"); console.warn(message); } return (instance === other); } }; }; } }); } lib_trait.define_collatable = define_collatable; /** * @author fenris */ function _collate(instance, other, domain) { if (domain === void 0) { domain = { "kind": "crude" }; } return lib_trait.call("collatable", "collate", domain)(instance)(other); } lib_trait._collate = _collate; define_collatable(); })(lib_trait || (lib_trait = {})); /* This file is part of »bacterio-plankton:trait«. Copyright 2016-2018 greenscale <info@greenscale.de> »bacterio-plankton:trait« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:trait« is distributed in the hope that it will be callful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:trait«. If not, see <http://www.gnu.org/licenses/>. */ /** * @author fenris */ var lib_trait; (function (lib_trait) { /** * @desc the ability to compare with another element of the same domain for determining if the first is "smaller than or equal to" the latter * @author fenris */ function define_comparable() { lib_trait.define("comparable", { "value": [] }, { "compare": { "shape": { "name": "function", "parameters": { "shape_input": { "name": "variable", "parameters": { "name": "value" } }, "shape_output": { "name": "function", "parameters": { "shape_input": { "name": "variable", "parameters": { "name": "value" } }, "shape_output": { "name": "boolean" } } } } } } }); lib_trait.attend("comparable", "crude", { "value": { "name": "any" } }, { "compare": function () { return function (instance) { return function (other) { if (typeof (instance) === "object") { if ("_compare" in instance) { return instance["_compare"](other); } else { throw (new Error("[compare]" + " " + "object has no such method")); } } else { if (lib_trait._verbosity >= 1) { console.warn("[compare]" + " " + "primitive value; using default implementation"); } return (instance <= other); } }; }; } }); } lib_trait.define_comparable = define_comparable; /** * @author fenris */ function _compare(instance, other, domain) { if (domain === void 0) { domain = { "kind": "crude" }; } return lib_trait.call("comparable", "compare", domain)(instance)(other); } lib_trait._compare = _compare; define_comparable(); })(lib_trait || (lib_trait = {})); /* This file is part of »bacterio-plankton:trait«. Copyright 2016-2018 greenscale <info@greenscale.de> »bacterio-plankton:trait« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:trait« is distributed in the hope that it will be callful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:trait«. If not, see <http://www.gnu.org/licenses/>. */ /** * @author fenris */ var lib_trait; (function (lib_trait) { /** * @desc the ability to create an exact copy * @author fenris */ function define_clonable() { lib_trait.define("clonable", { "value": [] }, { "clone": { "shape": { "name": "function", "parameters": { "shape_input": { "name": "variable", "parameters": { "name": "value" } }, "shape_output": { "name": "variable", "parameters": { "name": "value" } } } } } }); lib_trait.attend("clonable", "crude", { "value": { "name": "any" } }, { "clone": function () { return function (instance) { if (typeof (instance) === "object") { if ("_clone" in instance) { return instance["_clone"](); } else { throw (new Error("[clone]" + " " + "object has no such method")); } } else { if (lib_trait._verbosity >= 1) { console.warn("[clone]" + " " + "primitive value; using default implementation"); } return instance; } }; } }); } lib_trait.define_clonable = define_clonable; /** * @author fenris */ function _clone(instance, domain) { if (domain === void 0) { domain = { "kind": "crude" }; } return lib_trait.call("clonable", "clone", domain)(instance); } lib_trait._clone = _clone; define_clonable(); })(lib_trait || (lib_trait = {})); /* This file is part of »bacterio-plankton:trait«. Copyright 2016-2018 greenscale <info@greenscale.de> »bacterio-plankton:trait« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:trait« is distributed in the hope that it will be callful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:trait«. If not, see <http://www.gnu.org/licenses/>. */ /** * @author fenris */ var lib_trait; (function (lib_trait) { /** * @desc the ability to generate a string out of the element, which identifies it to a high degree * @author fenris */ function define_hashable() { lib_trait.define("hashable", { "value": [] }, { "hash": { "shape": { "name": "function", "parameters": { "shape_input": { "name": "variable", "parameters": { "name": "value" } }, "shape_output": { "name": "string" } } } } }); lib_trait.attend("hashable", "crude", { "value": { "name": "any" } }, { "hash": function () { return function (thing) { if (typeof (thing) === "object") { if ("_hash" in thing) { return thing["_hash"](); } else { var message = ("[hash]" + " " + "object has no such method"); throw (new Error(message)); } } else { if (lib_trait._verbosity >= 1) { var message = ("[hash]" + " " + "primitive value; using default implementation"); console.warn(message); } return String(thing); } }; } }); } lib_trait.define_hashable = define_hashable; /** * @author fenris */ function _hash(instance, domain) { if (domain === void 0) { domain = { "kind": "crude" }; } return lib_trait.call("hashable", "hash", domain)(instance); } lib_trait._hash = _hash; define_hashable(); })(lib_trait || (lib_trait = {})); /* This file is part of »bacterio-plankton:trait«. Copyright 2016-2018 greenscale <info@greenscale.de> »bacterio-plankton:trait« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:trait« is distributed in the hope that it will be callful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:trait«. If not, see <http://www.gnu.org/licenses/>. */ /** * @author fenris */ var lib_trait; (function (lib_trait) { /** * @desc the ability to map the element to a textual representation (most likely not injective) * @author fenris */ function define_showable() { lib_trait.define("showable", { "value": [] }, { "show": { "shape": { "name": "function", "parameters": { "shape_input": { "name": "variable", "parameters": { "name": "value" } }, "shape_output": { "name": "string", "parameters": { "soft": false } } } } } }); lib_trait.attend("showable", "crude", { "value": { "name": "any" } }, { "show": function () { return function (instance) { if (typeof (instance) === "object") { if (instance == null) { return "NULL"; } else { if ("_show" in instance) { return instance["_show"](); } else { // throw (new Error("[show]" + " " + "object has no such method")); return JSON.stringify(instance); } } } else { if (lib_trait._verbosity >= 1) { var message = ("[show]" + " " + "primitive value; using default implementation"); // console.warn(message); } return String(instance); } }; } }); } lib_trait.define_showable = define_showable; /** * @author fenris */ function _show(instance, domain) { if (domain === void 0) { domain = { "kind": "crude" }; } return lib_trait.call("showable", "show", domain)(instance); } lib_trait._show = _show; define_showable(); })(lib_trait || (lib_trait = {})); /* This file is part of »bacterio-plankton:code«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:code« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:code« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:code«. If not, see <http://www.gnu.org/licenses/>. */ /* This file is part of »bacterio-plankton:code«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:code« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:code« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:code«. If not, see <http://www.gnu.org/licenses/>. */ var lib_code; (function (lib_code) { /** * @author fenris */ lib_trait.define("code", { "from": null, "to": null }, { "encode": { "shape": { "name": "function", "parameters": { "shape_input": { "name": "variable", "parameters": { "name": "from" } }, "shape_output": { "name": "variable", "parameters": { "name": "to" } } } } }, "decode": { "shape": { "name": "function", "parameters": { "shape_input": { "name": "variable", "parameters": { "name": "to" } }, "shape_output": { "name": "variable", "parameters": { "name": "from" } } } } } }); })(lib_code || (lib_code = {})); /* This file is part of »bacterio-plankton:code«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:code« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:code« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:code«. If not, see <http://www.gnu.org/licenses/>. */ var lib_code; (function (lib_code) { /** * @author Christian Fraß <frass@greenscale.de> */ function custom_encode(function_, from) { return function_(from); } lib_code.custom_encode = custom_encode; /** * @author Christian Fraß <frass@greenscale.de> */ function custom_decode(function_, to) { return function_(to); } lib_code.custom_decode = custom_decode; })(lib_code || (lib_code = {})); /* This file is part of »bacterio-plankton:code«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:code« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:code« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:code«. If not, see <http://www.gnu.org/licenses/>. */ var lib_code; (function (lib_code) { /** * @author fenris */ class class_code_custom { /** * @author fenris */ constructor(encodefunc, decodefunc) { this.encodefunc = encodefunc; this.decodefunc = decodefunc; } /** * @implementation * @author fenris */ encode(x) { return lib_code.custom_encode(this.encodefunc, x); } /** * @implementation * @author fenris */ decode(x) { return lib_code.custom_decode(this.decodefunc, x); } } lib_code.class_code_custom = class_code_custom; })(lib_code || (lib_code = {})); /* This file is part of »bacterio-plankton:code«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:code« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:code« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:code«. If not, see <http://www.gnu.org/licenses/>. */ var lib_code; (function (lib_code) { /** * @author fenris */ function inverse_encode(decode, to) { return decode(to); } lib_code.inverse_encode = inverse_encode; /** * @author fenris */ function inverse_decode(encode, from) { return encode(from); } lib_code.inverse_decode = inverse_decode; })(lib_code || (lib_code = {})); /* This file is part of »bacterio-plankton:code«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:code« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:code« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:code«. If not, see <http://www.gnu.org/licenses/>. */ var lib_code; (function (lib_code) { /** * @author fenris */ lib_trait.attend("code", "inverse", { "from": { "name": "any" }, "to": { "name": "any" } }, { "encode": (domain_parameters) => (x) => { return (lib_code.inverse_encode(y_ => lib_trait.call("code", "decode", domain_parameters.domain_subject)(y_), x)); }, "decode": (domain_parameters) => (y) => { return (lib_code.inverse_decode(x_ => lib_trait.call("code", "encode", domain_parameters.domain_subject)(x_), y)); } }); })(lib_code || (lib_code = {})); /* This file is part of »bacterio-plankton:code«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:code« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:code« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:code«. If not, see <http://www.gnu.org/licenses/>. */ var lib_code; (function (lib_code) { /** * @author fenris */ class class_code_inverse { /** * @author fenris */ constructor(subject) { this.subject = subject; } /** * @implementation * @author fenris */ encode(to) { return lib_code.inverse_encode(x => this.subject.decode(x), to); } /** * @implementation * @author fenris */ decode(from) { return lib_code.inverse_decode(x => this.subject.encode(x), from); } } lib_code.class_code_inverse = class_code_inverse; })(lib_code || (lib_code = {})); /* This file is part of »bacterio-plankton:code«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:code« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:code« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:code«. If not, see <http://www.gnu.org/licenses/>. */ var lib_code; (function (lib_code) { /** * @author fenris */ function pair_encode(encode_first, encode_second, from) { let between = encode_first(from); let to = encode_second(between); return to; } lib_code.pair_encode = pair_encode; /** * @author fenris */ function pair_decode(decode_first, decode_second, to) { let between = decode_second(to); let from = decode_first(between); return from; } lib_code.pair_decode = pair_decode; })(lib_code || (lib_code = {})); /* This file is part of »bacterio-plankton:code«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:code« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:code« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:code«. If not, see <http://www.gnu.org/licenses/>. */ var lib_code; (function (lib_code) { /** * @author fenris */ lib_trait.attend("code", "pair", { "from": { "name": "any" }, "to": { "name": "any" } }, { "encode": (domain_parameters) => (x) => { return (lib_code.pair_encode(x_ => lib_trait.call("code", "encode", domain_parameters.domain_first)(x_), x_ => lib_trait.call("code", "encode", domain_parameters.domain_second)(x_), x)); }, "decode": (domain_parameters) => (y) => { return (lib_code.pair_decode(y_ => lib_trait.call("code", "decode", domain_parameters.domain_first)(y_), y_ => lib_trait.call("code", "decode", domain_parameters.domain_second)(y_), y)); } }); })(lib_code || (lib_code = {})); /* This file is part of »bacterio-plankton:code«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:code« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:code« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:code«. If not, see <http://www.gnu.org/licenses/>. */ var lib_code; (function (lib_code) { /** * @author fenris */ class class_code_pair { /** * @author fenris */ constructor(first, second) { this.first = first; this.second = second; } /** * @implementation * @author fenris */ encode(from) { return lib_code.pair_encode(x => this.first.encode(x), x => this.second.encode(x), from); } /** * @implementation * @author fenris */ decode(to) { return lib_code.pair_decode(x => this.first.decode(x), x => this.second.decode(x), to); } } lib_code.class_code_pair = class_code_pair; })(lib_code || (lib_code = {})); /* This file is part of »bacterio-plankton:code«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:code« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:code« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:code«. If not, see <http://www.gnu.org/licenses/>. */ var lib_code; (function (lib_code) { /** * @author fenris */ function chain_encode(encode_links, from) { let value = from; encode_links .forEach((link) => { value = link(value); }); return value; } lib_code.chain_encode = chain_encode; /** * @author fenris */ function chain_decode(decode_links, to) { let value = to; decode_links .reverse() .forEach((link) => { value = link(value); }); return value; } lib_code.chain_decode = chain_decode; })(lib_code || (lib_code = {})); /* This file is part of »bacterio-plankton:code«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:code« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:code« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:code«. If not, see <http://www.gnu.org/licenses/>. */ var lib_code; (function (lib_code) { /** * @author fenris */ class class_code_chain { /** * @author fenris */ constructor(links) { this.links = links; } /** * @implementation * @author fenris */ encode(from) { return lib_code.chain_encode(this.links.map((link) => (x => link.encode(x))), from); } /** * @implementation * @author fenris */ decode(to) { return lib_code.chain_decode(this.links.map((link) => (x => link.decode(x))), to); } } lib_code.class_code_chain = class_code_chain; })(lib_code || (lib_code = {})); /* This file is part of »bacterio-plankton:code«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:code« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:code« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:code«. If not, see <http://www.gnu.org/licenses/>. */ var lib_code; (function (lib_code) { /** * @author Christian Fraß <frass@greenscale.de> */ function flatten_encode(from, keys = null) { if (keys === null) { if (from.length > 0) { keys = Object.keys(from[0]); } else { throw (new Error("encoding impossible")); } } return { "keys": keys, "data": from.map((line) => keys.map((name) => line[name])), }; } lib_code.flatten_encode = flatten_encode; /** * @author Christian Fraß <frass@greenscale.de> */ function flatten_decode(to) { return (to.data .map((dataset) => { let dataset_ = {}; dataset .forEach((value, index) => { const name = to.keys[index]; dataset_[name] = value; }); return dataset_; })); } lib_code.flatten_decode = flatten_decode; })(lib_code || (lib_code = {})); /* This file is part of »bacterio-plankton:code«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:code« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:code« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:code«. If not, see <http://www.gnu.org/licenses/>. */ var lib_code; (function (lib_code) { /** * @author fenris */ class class_code_flatten { /** * @author fenris */ constructor() { } /** * @implementation * @author fenris */ encode(x) { return lib_code.flatten_encode(x); } /** * @implementation * @author fenris */ decode(x) { return lib_code.flatten_decode(x); } } lib_code.class_code_flatten = class_code_flatten; })(lib_code || (lib_code = {})); /* This file is part of »bacterio-plankton:code«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:code« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:code« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:code«. If not, see <http://www.gnu.org/licenses/>. */ var lib_code; (function (lib_code) { /** * @author fenris */ const _prefix = "DATE:"; /** * @author fenris */ function jsdate_encode(date) { let isostring = date.toISOString(); let date_ = (_prefix + isostring); return date_; } lib_code.jsdate_encode = jsdate_encode; /** * @author fenris */ function jsdate_decode(date_) { if ((typeof (date_) === "string") && (date_.startsWith(_prefix))) { let isostring = date_.slice(_prefix.length); let timestamp = Date.parse(isostring); if (!isNaN(timestamp)) { let date = (new Date(timestamp)); return date; } else { let message = ("'" + isostring + "' does not seem to be an ISO-string of a date"); throw (new Error(message)); } } else { let message = ("expected an encoded date object to be a string starting with '" + _prefix + "'"); throw (new Error(message)); } } lib_code.jsdate_decode = jsdate_decode; })(lib_code || (lib_code = {})); /* This file is part of »bacterio-plankton:code«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:code« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:code« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:code«. If not, see <http://www.gnu.org/licenses/>. */ var lib_code; (function (lib_code) { /** * @author fenris */ lib_trait.attend("code", "jsdate", { "from": { "name": "date" }, "to": { "name": "string" } }, { "encode": () => (x) => { return lib_code.jsdate_encode(x); }, "decode": () => (y) => { return lib_code.jsdate_decode(y); } }); })(lib_code || (lib_code = {})); /* This file is part of »bacterio-plankton:code«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:code« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:code« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:code«. If not, see <http://www.gnu.org/licenses/>. */ var lib_code; (function (lib_code) { /** * @author fenris */ class class_code_jsdate { /** * @author fenris */ constructor() { } /** * @implementation * @author fenris */ encode(x) { return lib_code.jsdate_encode(x); } /** * @implementation * @author fenris */ decode(x) { return lib_code.jsdate_decode(x); } } lib_code.class_code_jsdate = class_code_jsdate; })(lib_code || (lib_code = {})); /* This file is part of »bacterio-plankton:code«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:code« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:code« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:code«. If not, see <http://www.gnu.org/licenses/>. */ var lib_code; (function (lib_code) { /** * @author fenris */ function json_encode(x, formatted = false) { return JSON.stringify(x, undefined, formatted ? "\t" : undefined); } lib_code.json_encode = json_encode; /** * @author fenris */ function json_decode(x) { return JSON.parse(x); } lib_code.json_decode = json_decode; })(lib_code || (lib_code = {})); /* This file is part of »bacterio-plankton:code«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:code« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:code« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:code«. If not, see <http://www.gnu.org/licenses/>. */ var lib_code; (function (lib_code) { /** * @author fenris */ lib_trait.attend("code", "json", { "from": { "name": "any" }, "to": { "name": "string" } }, { "encode": () => (x) => { return lib_code.json_encode(x); }, "decode": () => (y) => { return lib_code.json_decode(y); } }); })(lib_code || (lib_code = {})); /* This file is part of »bacterio-plankton:code«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:code« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:code« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:code«. If not, see <http://www.gnu.org/licenses/>. */ var lib_code; (function (lib_code) { /** * @author fenris */ class class_code_json { /** * @author fenris */ constructor() { } /** * @implementation * @author fenris */ encode(x) { return lib_code.json_encode(x); } /** * @implementation * @author fenris */ decode(x) { return lib_code.json_decode(x); } } lib_code.class_code_json = class_code_json; })(lib_code || (lib_code = {})); /* This file is part of »bacterio-plankton:code«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:code« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:code« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:code«. If not, see <http://www.gnu.org/licenses/>. */ var lib_code; (function (lib_code) { /** * @author Christian Fraß <frass@greenscale.de> * @todo escaping */ function csv_encode(from, { "delimiter": delimiter = ",", "linebreak": linebreak = "\n", } = {}) { return (([] .concat((from.head !== null) ? [from.head] : []) .concat(from.data)) .map((dataset) => dataset.join(delimiter)) .join(linebreak)); } lib_code.csv_encode = csv_encode; /** * @author Christian Fraß <frass@greenscale.de> */ function csv_decode(to, { "delimiter": delimiter = ",", "linebreak": linebreak = "\n", "with_head": with_head = true, } = {}) { const array = (to.split(linebreak) .map((line) => line.split(delimiter))); return (with_head ? ({ "head": array[0], "data": array.slice(1), }) : ({ "head": null, "data": array, })); } lib_code.csv_decode = csv_decode; })(lib_code || (lib_code = {})); /* This file is part of »bacterio-plankton:code«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:code« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:code« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:code«. If not, see <http://www.gnu.org/licenses/>. */ var lib_code; (function (lib_code) { /** * @author fenris */ class class_code_csv { /** * @author fenris */ constructor() { } /** * @implementation * @author fenris */ encode(x) { return lib_code.csv_encode(x); } /** * @implementation * @author fenris */ decode(x) { return lib_code.csv_decode(x); } } lib_code.class_code_csv = class_code_csv; })(lib_code || (lib_code = {})); /* This file is part of »bacterio-plankton:code«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:code« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:code« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:code«. If not, see <http://www.gnu.org/licenses/>. */ var lib_code; (function (lib_code) { /** * @author fenris */ function uri_encode(x) { return encodeURIComponent(x); } lib_code.uri_encode = uri_encode; /** * @author fenris */ function uri_decode(x) { return decodeURIComponent(x); } lib_code.uri_decode = uri_decode; })(lib_code || (lib_code = {})); /* This file is part of »bacterio-plankton:code«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:code« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:code« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:code«. If not, see <http://www.gnu.org/licenses/>. */ var lib_code; (function (lib_code) { /** * @author fenris */ lib_trait.attend("code", "uri", { "from": { "name": "string" }, "to": { "name": "string" } }, { "encode": () => (x) => { return lib_code.uri_encode(x); }, "decode": () => (y) => { return lib_code.uri_decode(y); } }); })(lib_code || (lib_code = {})); /* This file is part of »bacterio-plankton:code«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:code« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:code« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:code«. If not, see <http://www.gnu.org/licenses/>. */ var lib_code; (function (lib_code) { /** * @author fenris */ class class_code_uri { /** * @author fenris */ constructor() { } /** * @implementation * @author fenris */ encode(x) { return lib_code.uri_encode(x); } /** * @implementation * @author fenris */ decode(x) { return lib_code.uri_decode(x); } } lib_code.class_code_uri = class_code_uri; })(lib_code || (lib_code = {})); /* This file is part of »bacterio-plankton:code«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:code« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:code« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:code«. If not, see <http://www.gnu.org/licenses/>. */ var lib_code; (function (lib_code) { /** * @author fenris */ function base64_encode(x) { return btoa(x); } lib_code.base64_encode = base64_encode; /** * @author fenris */ function base64_decode(x) { return atob(x); } lib_code.base64_decode = base64_decode; })(lib_code || (lib_code = {})); /* This file is part of »bacterio-plankton:code«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:code« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:code« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:code«. If not, see <http://www.gnu.org/licenses/>. */ var lib_code; (function (lib_code) { /** * @author fenris */ lib_trait.attend("code", "base64", { "from": { "name": "string" }, "to": { "name": "string" } }, { "encode": () => (x) => { return lib_code.base64_encode(x); }, "decode": () => (y) => { return lib_code.base64_decode(y); } }); })(lib_code || (lib_code = {})); /* This file is part of »bacterio-plankton:code«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:code« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:code« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:code«. If not, see <http://www.gnu.org/licenses/>. */ var lib_code; (function (lib_code) { /** * @author fenris */ class class_code_base64 { /** * @author fenris */ constructor() { } /** * @implementation * @author fenris */ encode(x) { return lib_code.base64_encode(x); } /** * @implementation * @author fenris */ decode(x) { return lib_code.base64_decode(x); } } lib_code.class_code_base64 = class_code_base64; })(lib_code || (lib_code = {})); /* This file is part of »bacterio-plankton:string«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:string« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:string« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:string«. If not, see <http://www.gnu.org/licenses/>. */ var plain_text_to_html = function (text) { var ret = text; ret = ret.replace(/ /g, " "); // convert multiple whitespace to forced ones ret = ret.split("\n").join("<br/>"); return ret; }; /** * @desc makes a valid */ var format_sentence = function (str, rtl, caseSense) { if (rtl === void 0) { rtl = false; } if (caseSense === void 0) { caseSense = true; } if (str === "") { return str; } else { var marks = { ".": true, "?": true, "!": true }; var default_mark = "."; var ret = str.split(""); if (!rtl) { ret[0] = ret[0].toLocaleUpperCase(); if (!(ret[ret.length - 1] in marks)) { ret.push(default_mark); } } else { ret[ret.length - 1] = ret[ret.length - 1].toLocaleUpperCase(); if (!(ret[0] in marks)) { ret.unshift(default_mark); } } return ret.join(""); } }; var fill_string_template = function (template_string, object, fabric, delimiter, default_string, sloppy) { if (fabric === void 0) { fabric = function (object, key) { return object[key]; }; } if (delimiter === void 0) { delimiter = "%"; } if (default_string === void 0) { default_string = null; } function get_tags(str) { var r = new RegExp(delimiter + "[^\\s^" + delimiter + "]+" + delimiter, "gi"); return ((str.match(r) || []).map(function (e) { return e.slice(delimiter.length, e.length - delimiter.length); })); } function replace_tag(str, tag, value) { var r = new RegExp(delimiter + tag + delimiter, "gi"); return str.replace(r, value); } function replace_tags(str, obj) { return (get_tags(str).reduce(function (ret, key) { var value = ""; try { value = fabric(obj, key); if ((!sloppy && (value === void 0)) || (sloppy && (value == void 0))) { value = default_string; } } catch (e) { console.warn("invalid placeholder " + key); value = default_string; } return replace_tag(ret, key, value); }, str)); } return replace_tags(template_string, object); }; var make_string_template = function (_template, _fabrics) { if (_fabrics === void 0) { _fabrics = {}; } function replace_tag(str, tag, value) { var r = new RegExp("%" + tag + "%", "gi"); return str.replace(r, value); } function replace_tags(str, obj) { return (Object.keys(obj).reduce(function (ret, key) { return replace_tag(ret, key, _fabrics[key] || obj[key]); }, str)); } return (function (tags) { return replace_tags(_template, tags); }); }; var make_eml_header = (function () { var _template = ""; _template += "From: %from%\n"; _template += "To: %recipient%\n"; _template += "Subject: %subject%\n"; _template += "X-Mailer: greenscale-plankton.emlgen\n"; return make_string_template(_template); })(); var make_eml_body = (function () { var exports = {}; exports["simple_body"] = make_string_template("Content-Type: %contenttype%\n\n%body%\n\n"); // very basic implementation // parts = [{contenttype:"text/html; charset=UTF-8", body: "<h1>foo</h1>" }, {...}] exports["body_boundrary"] = function (parts, boundrary) { var _template = ""; _template += "--%boundrary%\n"; _template += "Content-Type: %contenttype%\n\n%body%\n\n"; //_template += "--%boundrary%--\n\n"; var maker = make_string_template(_template); return (parts.reduce(function (prev, curr) { curr.boundrary = boundrary; return [prev, maker(curr)].join(""); }, "")); }; // body must be base64 encoded! exports["attachment_boundrary"] = function (parts, boundrary) { var _template = ""; _template += "--%boundrary%\n"; _template += "Content-Type: %contenttype%\n"; _template += "Content-Transfer-Encoding: base64\n"; _template += "Content-Disposition: %disposition%; filename=\"%name%\"\n\n"; _template += "%body%\n\n"; //_template += "--%boundrary%--\n\n"; var maker = make_string_template(_template); return (parts.reduce(function (prev, curr) { curr.boundrary = boundrary; if (curr.disposition === void 0) curr.disposition = "inline"; return [prev, maker(curr)].join(""); }, "")); }; exports["gen_boundrary"] = function () { return ("xxxxxxxxxxxxxxxxxxxxxx".replace(/[xy]/g, function (c) { var r = crypto.getRandomValues(new Uint8Array(1))[0] % 16 | 0, v = c == "x" ? r : (r & 0x3 | 0x8); return v.toString(16); })); }; // simple implementation without alternatives (old rfc) exports["complete_boundrary"] = function (bodyparts, attachments) { var ret = ""; var boundrary = exports["gen_boundrary"](); ret += exports["body_boundrary"](bodyparts, boundrary); ret += exports["attachment_boundrary"](attachments, boundrary); ret += "--" + boundrary + "--\n\nINVISIBLE!!!!"; return (exports["simple_body"]({ "contenttype": sprintf("multipart/mixed; boundary=%s", [boundrary]), "body": ret })); }; return exports; })(); /* This file is part of »bacterio-plankton:string«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:string« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:string« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:string«. If not, see <http://www.gnu.org/licenses/>. */ var lib_string; (function (lib_string) { /** * @author fenris */ var hexdigits = 4; /** * @author fenris */ var index_max = (1 << (4 * hexdigits)); /** * @author fenris */ var index_is = 0; /** * @author neuc,frac */ function empty(str) { return (str.trim() === ""); } lib_string.empty = empty; /** * @desc returns a unique string * @param {string} prefix an optional prefix for the generated string * @return {string} * @author fenris */ function generate(prefix) { if (prefix === void 0) { prefix = "string_"; } if (index_is > index_max) { throw (new Error("[string_generate] out of valid indices")); } else { return lib_string.sprintf(prefix + "%0" + hexdigits.toString() + "X", [index_is++]); } } lib_string.generate = generate; /** * @desc splits a string, but returns an empty list, if the string is empty * @param {string} chain * @param {string} separator * @return {Array<string>} * @author fenris */ function split(chain, separator) { if (separator === void 0) { separator = " "; } if (chain.length == 0) { return []; } else { return chain.split(separator); } } lib_string.split = split; /** * @author neu3no */ function explode(str, needle, max) { var temp = str.split(needle); var right = temp.splice(max - 1); temp.push(right.join(needle)); return temp; } lib_string.explode = explode; /** * @desc concats a given word with itself n times * @param {string} word * @param {int} * @return {string} * @author fenris */ function repeat(word, count) { // return ((count == 0) ? "" : (word + repeat(word, count-1))); var result = ""; for (var n = 0; n < count; n += 1) { result += word; } return result; } lib_string.repeat = repeat; /** * @desc lengthens a string by repeatedly appending or prepending another string * @param {string} word the string to pad * @param {int} length the length, which the result shall have * @param {string} symbol the string, which will be added (multiple times) * @param {boolean} [prepend]; whether to prepend (~true) or append (~false); default: false * @return {string} the padded string * @author fenris */ function pad(word, length, symbol, mode) { if (symbol === void 0) { symbol = " "; } if (mode === void 0) { mode = "append"; } switch (mode) { case "prepend": { // insert symbols only at the beginning while (word.length < length) word = symbol + word; return word.substring(word.length - length); break; } case "append": { // insert symbols only at the end while (word.length < length) word = word + symbol; return word.substring(0, length); break; } case "widen": { // insert symbols at both sides var left = (((length - word.length) & 1) === 0); while (word.length < length) { word = (left ? (symbol + word) : (word + symbol)); left = (!left); } return word.substring(0, length); break; } default: { var message = ("unhandled mode '" + mode + "'"); console.warn(message); return word; break; } } } lib_string.pad = pad; /** * @desc checks if a given string conttains a certain substring * @param {string} string * @param {string} part * @return {boolean} * @author fenris */ function contains(chain, part) { if (typeof (chain) !== "string") { return false; } return (chain.indexOf(part) >= 0); } lib_string.contains = contains; /** * @desc checks if a given string starts with a certain substring * @param {string} string * @param {string} part * @return {boolean} * @author fenris */ function startsWith(chain, part) { if (typeof (chain) !== "string") { return false; } // return (string.indexOf(part) === 0); return ((function (m, n) { if (n === 0) { return true; } else { if (m === 0) { return false; } else { return ((chain[0] == part[0]) && startsWith(chain.substring(1), part.substring(1))); } } })(chain.length, part.length)); } lib_string.startsWith = startsWith; /** * @desc checks if a given string ends with a certain substring * @param {string} string * @param {string} part * @return {boolean} * @author fenris */ function endsWith(chain, part) { if (typeof (chain) !== "string") { return false; } // return (string.lastIndexOf(part) === string.length-part.length); return ((function (m, n) { if (n === 0) { return true; } else { if (m === 0) { return false; } else { // console.info(("(" + string[m-1] + " == " + part[n-1] + ")") + " = " + String(string[m-1] == part[n-1])); return ((chain[m - 1] === part[n - 1]) && endsWith(chain.substring(0, m - 1), part.substring(0, n - 1))); } } })(chain.length, part.length)); } lib_string.endsWith = endsWith; /** * @desc count the occourrences of a string in a string * @param string haystack_string the string wich should be examined * @param string needle_string the string which should be counted * @author neuc */ function count_occourrences(haystack_string, needle_string, check_escape) { var cnt = 0; var pos = -1; do { pos = haystack_string.indexOf(needle_string, pos + 1); if ((!check_escape) || (haystack_string[pos - 1] != "\\")) { cnt++; } } while (pos >= 0); return (cnt - 1); } lib_string.count_occourrences = count_occourrences; /** * @desc replaces occurences of "${name}" in a string by the corresponding values of an argument object * @author fenris */ function coin(str, args) { Object.keys(args).forEach(function (key) { // old syntax { var value = args[key]; var regexp_argument = new RegExp("\\${" + key + "}"); str = str.replace(regexp_argument, value); } // new syntax { var value = args[key]; var regexp_argument = new RegExp("{{" + key + "}}"); str = str.replace(regexp_argument, value); } }); return str; } lib_string.coin = coin; /** * @author fenris */ lib_string.stance = coin; /** * @author fenris */ function url_encode(_a) { var _b = _a === void 0 ? {} : _a, _c = _b["protocol"], protocol = _c === void 0 ? null : _c, _d = _b["host"], host = _d === void 0 ? null : _d, _e = _b["port"], port = _e === void 0 ? null : _e, _f = _b["path"], path = _f === void 0 ? null : _f, _g = _b["arguments"], arguments_ = _g === void 0 ? null : _g; var url = ""; // protocol { if (protocol != null) { url = protocol + ":" + url; } } // host { if (host != null) { url = url + "//" + host; } } // port { if (port != null) { url = url + ":" + port.toString(); } } // path { if (path != null) { var path_ = encodeURI(path); url = "" + url + path_; } } // arguments { if (arguments_ != null) { var suffix = Object.keys(arguments_).map(function (key) { return key + "=" + arguments_[key]; }).join("&"); var suffix_ = encodeURI(suffix); url = url + "?" + suffix_; } } return url; } lib_string.url_encode = url_encode; /** * @author fenris */ lib_string.make_url = url_encode; /** * @author fenris * @todo arguments */ function url_decode(url) { var regexp = new RegExp("^([^:]*)://([^:]*):([^/]*)/(.*)$"); var matching = regexp.exec(url); if (matching === null) { return null; } else { var components = { "protocol": matching[1], "host": matching[2], "port": parseInt(matching[3]), "path": ("/" + matching[4]) }; return components; } } lib_string.url_decode = url_decode; /** * @author fenris */ function cut(str, length, delimiter) { if (delimiter === void 0) { delimiter = "…"; } if (str.length <= length) { return str; } else { return (str.slice(0, length - delimiter.length) + delimiter); } } lib_string.cut = cut; })(lib_string || (lib_string = {})); /* This file is part of »bacterio-plankton:string«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:string« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:string« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:string«. If not, see <http://www.gnu.org/licenses/>. */ var lib_string; (function (lib_string) { var pattern = /%([-+#0 ]*)([0-9]*)[\.]{0,1}([0-9]*)([\w]{1})/; var gpattern = /%([-+#0 ]*)([0-9]*)[\.]{0,1}([0-9]*)([\w]{1})/g; function split_format(format) { var tmp = format.match(pattern); if (tmp === null) return null; return { 'flags': tmp[1].split(""), 'width': Number(tmp[2]), 'precision': tmp[3] === '' ? null : Number(tmp[3]), 'specifier': tmp[4], 'string': format }; } function make_err(format, arg, should) { return ("[sprintf]" + " " + "argument for '" + format.string + "' has to be '" + should + "' but '" + arg + "' is '" + typeof arg + "'!"); } function test_arg(format, arg, should) { if (typeof arg !== should) { console.warn(make_err(format, arg, should)); return false; } return true; } function string_fill(str, char, len, left) { while (str.length < len) { if (left) { str += char; } else { str = char + str; } } return str; } /** * the known_parameters are used to parse the different identifiers for the welln known syntax: * flag width precision identifier * %{[0#+- ]}{[0-9]*}.{[0-9]*}[fFdiueEgGsoxXaAsn] * flags: * 0 - fill with '0' instead of ' ' if the string length < width * # - not implemented * - - left-justified -> fill on the right side to reach width * + - force using '+' on positive numbers * ' ' - add a single space before positive numbers * * identifiers * %f, %F - interpret given number as float, width: the minimal total width (fill with ' ' or '0' if the * resulting string is too short, precision: cut more then given decimal places * %d, %i, %u - interpret number as integer, decimal places will be cut. width: like float, precision: * fill with '0' on right side until length given in precision is reached * %e - interpret as float and write as scientifical number, width & precision like in float * %E - same es %e but uppercase 'E' * %g - use the shortest string of %f or %e * %G - use the shortest string of %E or %E * %s - simply print a string * %o - print the given number in octal notation * %x - print the given number in hex notation * %X - same as %x but with uppercase characters * %a - alias to %x * %A - alias to %X * %n - just print nothing * @type {{}} */ var known_params = {}; known_params["f"] = function (format, arg) { if (!test_arg(format, arg, "number")) return "Ø"; var tmp = Math.abs(arg); var sign = (arg < 0) ? -1 : 1; var tmp_result = null; if (format.precision !== null) { tmp = Math.floor(Math.pow(10, format.precision) * tmp) / Math.pow(10, format.precision); var tmp_ = (tmp * sign).toString().split("."); if (tmp_.length === 1) tmp_.push(""); tmp_[1] = string_fill(tmp_[1], "0", format.precision, true); tmp_result = tmp_.join("."); } else { tmp_result = (sign * tmp).toString(); } if ((format.flags.indexOf(" ") >= 0) && (arg >= 0)) { tmp_result = " " + tmp; } else if ((format.flags.indexOf("+") >= 0) && (arg >= 0)) { tmp_result = "+" + tmp; } tmp_result = string_fill(tmp, (format.flags.indexOf("0") >= 0) ? "0" : " ", format.width, (format.flags.indexOf("-") >= 0)); return tmp_result; }; known_params["F"] = known_params["f"]; known_params["d"] = function (format, arg) { if (!test_arg(format, arg, 'number')) return 'Ø'; var tmp = (((arg < 0 && format.specifier !== 'u') ? -1 : 1) * Math.floor(Math.abs(arg))).toString(); if ((format.specifier === 'd' || format.specifier === 'i') && format.flags.indexOf(' ') >= 0 && arg >= 0) { tmp = ' ' + tmp; } else if ((format.specifier === 'd' || format.specifier === 'i') && format.flags.indexOf('+') >= 0 && arg >= 0) { tmp = '+' + tmp; } tmp = string_fill(tmp, format.flags.indexOf('0') >= 0 ? '0' : ' ', format.width, format.flags.indexOf('-') >= 0); tmp = string_fill(tmp, '0', format.precision === null ? 0 : format.precision, false); return tmp; }; known_params["i"] = known_params["d"]; known_params["u"] = known_params["d"]; known_params["e"] = function (format, arg) { if (!test_arg(format, arg, 'number')) return 'Ø'; var tmp = arg.toExponential(format.precision === null ? undefined : format.precision).toString(); if (format.flags.indexOf(' ') >= 0 && arg >= 0) { tmp = ' ' + tmp; } else if (format.flags.indexOf('+') >= 0 && arg >= 0) { tmp = '+' + tmp; } tmp = string_fill(tmp, format.flags.indexOf('0') >= 0 ? '0' : ' ', format.width, format.flags.indexOf('-') >= 0); return tmp; }; known_params["E"] = function (format, arg) { return known_params["e"](format, arg).toUpperCase(); }; known_params["g"] = function (format, arg) { if (!test_arg(format, arg, 'number')) return 'Ø'; var tmpf = known_params["f"](format, arg); var tmpe = known_params["e"](format, arg); if (tmpf.length < tmpe.length) { return tmpf; } else { return tmpe; } }; known_params["G"] = function (format, arg) { return known_params["g"](format, arg).toUpperCase(); }; known_params["s"] = function (format, arg) { if (!test_arg(format, arg, 'string')) return 'o.O'; var tmp = format.precision !== null ? arg.substr(0, format.precision) : arg; tmp = string_fill(tmp, format.flags.indexOf('0') >= 0 ? '0' : ' ', format.width, format.flags.indexOf('-') >= 0); return tmp; }; known_params["o"] = function (format, arg) { if (!test_arg(format, arg, 'number')) return 'Ø'; var tmp = Math.floor(Math.round(Math.abs(arg))) * ((arg < 0) ? -1 : 1); return known_params["s"](format, tmp.toString(8)); }; known_params["x"] = function (format, arg) { if (!test_arg(format, arg, 'number')) return 'Ø'; var tmp = Math.floor(Math.round(Math.abs(arg))) * ((arg < 0) ? -1 : 1); return known_params["s"](format, tmp.toString(16)); }; known_params["a"] = known_params["x"]; known_params["X"] = function (format, arg) { if (!test_arg(format, arg, 'number')) return 'Ø'; return known_params["x"](format, arg).toUpperCase(); }; known_params["A"] = known_params["X"]; known_params["c"] = function (format, arg) { var tmp = ""; if (typeof arg === "number") { tmp = String.fromCharCode(arg); } else if ((typeof arg === "string") && (arg.length === 1)) { tmp = arg[0]; } else { console.warn(make_err(format, arg, "number|string") + " and if string it needs to have the length of 1!"); } return known_params["s"](format, tmp); }; known_params["n"] = function () { return ""; }; var decompose = function (chain, regexp) { var result = regexp.exec(chain); if (result == null) { return null; } else { var front = chain.substring(0, result.index); var back = chain.substring(result.index + result[0].length); return { "front": front, "match": result[0], "back": back }; } }; /** * an implementation of c sprintf * @param {string} string format string * @param {array} args arguments which should be filled into * @returns {string} */ lib_string.sprintf = function (input, args, original) { if (args === void 0) { args = []; } if (original === void 0) { original = null; } if (original == null) original = input; var components = decompose(input, pattern); if (components == null) { if (args.length > 0) { console.warn("[sprintf] superfluous arguments while formatting '" + original + "': ", args); } return input; } else { var arg; var rest; if (args.length > 0) { arg = args[0]; rest = args.slice(1); } else { console.warn("[sprintf] out of arguments while formatting '" + original + "'"); arg = null; rest = []; return input; } var fmt = split_format(components["match"]); return (components["front"] + known_params[fmt.specifier](fmt, arg) + lib_string.sprintf(components["back"], rest, original)); } }; /** * an implementation of c printf * @param {string} string format string * @param {array} args arguments which should be filled into * @returns {string} */ function printf(format, args) { console.log(lib_string.sprintf(format, args)); } lib_string.printf = printf; })(lib_string || (lib_string = {})); var sprintf = lib_string.sprintf; var printf = lib_string.printf; /* This file is part of »bacterio-plankton:string«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:string« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:string« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:string«. If not, see <http://www.gnu.org/licenses/>. */ var make_logger = (function () { var _loggers = {}; var make_logger = function (prefix, current_loglevel) { var log = []; var level = [ "LOG", "INFO", "WARNING", "DEBUG" ]; var logger = function (obj, lvl) { var txt = obj.txt || obj; if (lvl == void 0) lvl = 0; var date = new Date(); log.push({ "message": sprintf("%s [%s:%s] %s", [date.toString(), level[lvl], prefix, txt]), "timeStamp": +(date) }); if (lvl <= current_loglevel) { var msg = ["[" + prefix + "]", txt]; if (obj.arg) msg = ["[" + prefix + "]"].concat(Array.prototype.slice.call(obj.arg)); if (lvl === 0) console["_log"].apply(console, msg); else if (lvl === 1) console["_info"].apply(console, msg); else if (lvl === 2) console["_warn"].apply(console, msg); else if (lvl >= 3) console["_log"].apply(console, msg); } }; _loggers[prefix] = { "logger": logger, "log": log }; return logger; }; make_logger["loggers"] = _loggers; make_logger["complete_log"] = function () { var logs = Object.keys(_loggers) .reduce(function (p, c) { return [].concat(p, _loggers[c].log); }, []); logs.sort(function (x, y) { return ((x.timeStamp > y.timeStamp) ? -1 : +1); }); return logs.map(function (x, i, a) { return x.message; }); }; if ( /*!track_exports*/true) { var _log_all = function (log, lvl, next) { if (next === void 0) { next = function () { }; } return function () { var msg = []; for (var i = 0; i < arguments.length; i++) { if (typeof arguments[i] === "string") { msg.push(arguments[i]); } else { msg.push(JSON.stringify(arguments[i])); } } var obj = { txt: msg.join("\t"), arg: arguments }; log(obj, lvl); next(); }; }; { var __warn = make_logger("deprecated console.warn", 99); var __error = make_logger("deprecated console.error", 99); var __log = make_logger("deprecated console.log", 99); var __info = make_logger("deprecated console.info", 99); // bad ass console["_log"] = console.log; console["_error"] = console.error; console["_warn"] = console.warn; console["_info"] = console.info; /* console["log"] = _log_all(__log, 0); console["error"] = _log_all(__error, 2); console["warn"] = _log_all(__warn, 2); console["info"] = _log_all(__info, 0); */ } /* { make_logger["send_log"] = function(){ eml_log( function () { alert("fehlerbericht wurde gesendet!"); } ); }; var error_log = make_logger("global.error", 99); window.onerror = _log_all( error_log, 1, function(){ if (global_config == undefined) { return false; } if (global_config.report_error) { make_logger["send_log"](); } } ); } */ } return make_logger; })(); /* This file is part of »bacterio-plankton:call«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:call« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:call« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:call«. If not, see <http://www.gnu.org/licenses/>. */ "use strict"; /* This file is part of »bacterio-plankton:call«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:call« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:call« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:call«. If not, see <http://www.gnu.org/licenses/>. */ var lib_call; (function (lib_call) { /** * @desc hacked class for postfix function application * @author fenris */ class class_valuewrapper { /** * @desc [constructor] * @author fenris */ constructor(value) { this.value = value; } /** * @desc [accessor] applies a function and returns a new valuewrapper * @author fenris */ pass(function_) { return (new class_valuewrapper(function_(this.value))); } /** * @desc [accessor] gives the wrapped value * @author fenris */ extract() { return this.value; } } lib_call.class_valuewrapper = class_valuewrapper; /** * @desc shortcut for constructing a valuewrapper-object * @author fenris */ function vw(value) { return (new class_valuewrapper(value)); } lib_call.vw = vw; /** * @author fenris */ function use(input, function_) { return function_(input); } lib_call.use = use; /** * @desc just the identity; useful for some callbacks etc. * @author fenris */ function id(x) { return x; } lib_call.id = id; /** * @desc composes two functions (i.e. returns a function that return the result of the successive execution of both input-functions) * @param {function} function_f * @param {function} function_g * @author fenris */ function compose(function_f, function_g) { return (function (x) { // return function_g(function_f(x)); return function_g(function_f.apply(function_f, lib_call.args2list(arguments))); }); } lib_call.compose = compose; /** * @desc transforms a function with sequential input into a function with leveled input; example: add(2,3) = curryfy(add)(2)(3) * @param {function} f * @param {int} n (don't set manually) * @return {function} the currified version of the in put function * @author fenris */ function curryfy(f, n = f.length) { switch (n) { case 0: { throw (new Error("[curryfy] impossible")); // break; } case 1: { return f; // break; } default: { return (function (x) { return (curryfy(function () { return f.apply(f, [x].concat(lib_call.args2list(arguments))); }, n - 1)); }); // break; } } } lib_call.curryfy = curryfy; })(lib_call || (lib_call = {})); var lib_call; (function (lib_call) { /** * @author fenris */ function executor_resolve(result) { return ((resolve, reject) => resolve(result)); } lib_call.executor_resolve = executor_resolve; /** * @author fenris */ function executor_reject(reason) { return ((resolve, reject) => reject(reason)); } lib_call.executor_reject = executor_reject; /** * @author fenris */ function executor_transform(executor, transform_result, transform_reason) { return ((resolve, reject) => { executor(result => resolve(transform_result(result)), reason => reject(transform_reason(reason))); }); } lib_call.executor_transform = executor_transform; /** * @author fenris */ function executor_transform_default(executor, transform_result, wrap_string = null) { let transform_reason = (error => ((wrap_string == null) ? error : new class_error(wrap_string, [error]))); return (executor_transform(executor, transform_result, transform_reason)); } lib_call.executor_transform_default = executor_transform_default; /** * @author fenris */ function executor_compose_sequential(first, second) { return ((resolve, reject) => { first(result => { second(result)(resolve, reject); }, reason => { reject(reason); }); }); } lib_call.executor_compose_sequential = executor_compose_sequential; /** * @author fenris */ function executor_chain(state, executors) { return ((resolve, reject) => { if (executors.length == 0) { return resolve(state); } else { return executors[0](state)(result => { executor_chain(result, executors.slice(1))(resolve, reject); }, reject); } }); /* */ /* if (executors.length == 0) { return executor_resolve<type_state, type_error>(state); } else if (executors.length == 1) { return executors[0](state); } else { return ( executor_chain<type_state, type_error>( state, [ state => (resolve, reject) => executors[0](state)(result => executors[1](result)(resolve, reject), reject) ].concat(executors.slice(2)) ) ); } */ /* return ( executors.reduce( (chain, current) => executor_compose_sequential<type_state, type_state, type_error>(chain, current, deferred), executor_resolve<type_state, type_error>(state) ) ); */ } lib_call.executor_chain = executor_chain; /** * @author fenris */ function executor_first(executors) { /* return ( (resolve, reject) => { if (executors.length == 0) { reject(new Error("all failed")); } else { executors[0]( result => { resolve(result); }, reason => { executor_first<type_result, type_reason>(executors.slice(1))(resolve, reject); } ) } } ); */ return ((resolve, reject) => { executor_chain([], executors.map(executor => reasons => (resolve_, reject_) => { executor(result => reject_(result), reason => resolve_(reasons.concat([reason]))); }))(errors => reject(errors), result => resolve(result)); }); } lib_call.executor_first = executor_first; /** * @author fenris */ function executor_condense(executors) { return (executor_chain([], executors.map(executor => result => (resolve, reject) => { executor(element => resolve(result.concat([element])), reject); }))); } lib_call.executor_condense = executor_condense; /** * @author fenris * @deprecated use condense */ function executor_filter(executors, predicate) { return (executor_chain([], executors.map(executor => result => (resolve, reject) => { executor(element => resolve(predicate(element) ? result.concat([element]) : result), reject); }))); } lib_call.executor_filter = executor_filter; /** * @author fenris * @deprecated use condense */ function executor_map(executors, transformator) { return (executor_chain([], executors.map(executor => result => (resolve, reject) => { executor(element1 => resolve(result.concat([transformator(element1)])), reject); }))); } lib_call.executor_map = executor_map; /** * @author fenris * @deprecated use condense */ function executor_reduce(executors, initial, accumulator) { return (executor_chain(initial, executors.map(executor => result => (resolve, reject) => { executor(element => resolve(accumulator(result, element)), reject); }))); } lib_call.executor_reduce = executor_reduce; })(lib_call || (lib_call = {})); /* This file is part of »bacterio-plankton:call«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:call« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:call« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:call«. If not, see <http://www.gnu.org/licenses/>. */ var lib_call; (function (lib_call) { /** * @author fenris */ function promise_reject(reason) { return Promise.reject(reason); } lib_call.promise_reject = promise_reject; /** * @author fenris */ function promise_resolve(result) { return Promise.resolve(result); } lib_call.promise_resolve = promise_resolve; /** * @author fenris */ function promise_make(executor) { return (new Promise(executor)); } lib_call.promise_make = promise_make; /** * @author fenris */ function promise_then_close(promise, resolver, rejector) { promise.then(resolver, rejector); } lib_call.promise_then_close = promise_then_close; /** * @author fenris */ function promise_then_append(promise, resolver, rejector = null) { if (rejector == null) { rejector = (reason) => promise_reject(reason); } return (promise.then(resolver, rejector)); } lib_call.promise_then_append = promise_then_append; /** * @author fenris */ function promise_all(promises) { return Promise.all(promises); } lib_call.promise_all = promise_all; /** * @author fenris */ function promise_chain(promises, start = undefined) { return (promises.reduce((chain, promise) => promise_then_append(chain, promise), promise_resolve(start))); } lib_call.promise_chain = promise_chain; /** * @author fenris */ function promise_condense(promises) { return (promise_chain(promises.map(promise => result => promise_then_append(promise(), element => promise_resolve(result.concat([element])))), [])); } lib_call.promise_condense = promise_condense; /** * @author fenris */ function promise_group(promises, serial = false) { const decorate = function (promise, name) { return (() => promise_then_append(promise(), value => promise_resolve({ "key": name, "value": value }))); }; const convert = function (array) { let object = {}; array.forEach(({ "key": key, "value": value }) => { object[key] = value; }); return object; }; if (serial) { return (promise_then_append(promise_condense(Object.keys(promises) .map(name => decorate(promises[name], name))), list => promise_resolve(convert(list)))); } else { return (promise_then_append(promise_all(Object.keys(promises) .map(name => decorate(promises[name], name)) .map(promise => promise())), list => promise_resolve(convert(list)))); } } lib_call.promise_group = promise_group; /** * @author fenris */ function promise_wrap(promise, transformator_result, transformator_reason = lib_call.id) { return (promise_make((resolve, reject) => { promise_then_close(promise, result => resolve(transformator_result(result)), reason => reject(transformator_reason(reason))); })); } lib_call.promise_wrap = promise_wrap; /** * @author fenris */ function promise_show(label) { return (result => promise_make((resolve, reject) => { lib_log.info(label + ": " + instance_show(result)); resolve(result); })); } lib_call.promise_show = promise_show; /** * @author fenris */ function promise_log(result) { return promise_show("log"); } lib_call.promise_log = promise_log; /** * @author fenris */ function promise_attach(state, promise, name) { return (promise_wrap(promise, result => { state[name] = result; return state; })); } lib_call.promise_attach = promise_attach; /** * @author fenris */ function promise_delay(promise, delay) { return promise_make((resolve, reject) => { lib_call.timeout(() => { promise_then_close(promise, resolve, reject); return null; }, delay); }); } lib_call.promise_delay = promise_delay; /** * @author fenris */ function promise_to_executor(promise) { return ((resolve, reject) => promise.then(resolve, reject)); } lib_call.promise_to_executor = promise_to_executor; })(lib_call || (lib_call = {})); /* This file is part of »bacterio-plankton:call«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:call« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:call« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:call«. If not, see <http://www.gnu.org/licenses/>. */ var lib_call; (function (lib_call) { lib_call.initializer_state_initial = 0; lib_call.initializer_state_waiting = 1; lib_call.initializer_state_successful = 2; lib_call.initializer_state_failed = 3; /** * @author fenris */ function initializer_make(fetcher) { let subject = { "fetcher": fetcher, "state": lib_call.initializer_state_initial, "queue": [], "result": undefined, "reason": undefined, }; return subject; } lib_call.initializer_make = initializer_make; /** * @author fenris */ function initializer_actuate(subject) { switch (subject.state) { case lib_call.initializer_state_successful: { subject.queue.forEach(entry => entry.resolve(subject.result)); break; } case lib_call.initializer_state_failed: { subject.queue.forEach(entry => entry.reject(subject.reason)); break; } default: { let message = `unhandled state ${subject.state}`; throw (new Error(message)); break; } } } /** * @author fenris */ function initializer_reset(subject) { subject.state = lib_call.initializer_state_initial; subject.queue = []; } lib_call.initializer_reset = initializer_reset; /** * @author fenris */ function initializer_state(subject) { return subject.state; } lib_call.initializer_state = initializer_state; /** * @author fenris */ function initializer_get(subject) { switch (subject.state) { case lib_call.initializer_state_initial: { subject.state = lib_call.initializer_state_waiting; return (lib_call.promise_make((resolve, reject) => { subject.queue.push({ "resolve": resolve, "reject": reject }); subject.fetcher().then(result => { subject.state = lib_call.initializer_state_successful; subject.result = result; initializer_actuate(subject); }, reason => { subject.state = lib_call.initializer_state_failed; subject.reason = reason; initializer_actuate(subject); }); })); break; } case lib_call.initializer_state_waiting: { return (lib_call.promise_make((resolve, reject) => { subject.queue.push({ "resolve": resolve, "reject": reject }); })); break; } case lib_call.initializer_state_successful: { return (lib_call.promise_resolve(subject.result)); break; } case lib_call.initializer_state_failed: { return (lib_call.promise_reject(subject.reason)); break; } default: { let message = `unhandled state ${subject.state}`; throw (new Error(message)); break; } } } lib_call.initializer_get = initializer_get; /** * @author fenris */ function initializer_get_sync(subject) { switch (subject.state) { case lib_call.initializer_state_successful: { return subject.result; break; } case lib_call.initializer_state_failed: { throw subject.reason; break; } default: { let message = `unhandled state ${subject.state}`; throw (new Error(message)); break; } } } /** * @author fenris */ function initializer_set_sync(subject, result) { switch (subject.state) { case lib_call.initializer_state_successful: { subject.result = result; break; } case lib_call.initializer_state_failed: { subject.state = lib_call.initializer_state_successful; subject.result = result; break; } default: { let message = `unhandled state ${subject.state}`; throw (new Error(message)); break; } } } })(lib_call || (lib_call = {})); /* This file is part of »bacterio-plankton:call«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:call« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:call« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:call«. If not, see <http://www.gnu.org/licenses/>. */ var lib_call; (function (lib_call) { /* The core idea of this library is to provide means for asynchronous program flow. The old-school way to do is, is to use callbacks. While this approach is simple and easy to understand, it has some disadvantages. As an attempt to relief and improve this, the promise-system was introduced. In principle it solves most of the problems found in the callback-approach; however it has some downsides as well: - Convolution of multiple principles Promises unite the ideas of asynchronous program flow and error handling. - Instant execution Creating a promise results in the instant execution of the given executor prodecure. While this might be convenient in some cases, it can be quite disturbing and counter-intuitive in others. - Broken typing The Promise system doesn't distinguish between an appending "then" (i.e. passing a function, which returns a new promise) and a closing "then" (i.e. passing a function, which has no return value). On top of that it allows returning simple values in an appending "then", which results in an implicit call of the executors "resolve"-function. The price for these "pragmatic" features is that the whole system can't be typed well. And even though JavaScript is not a strictly typed language, it was a quite questionable decision to design the promise system in a way, which breaks typing from the start. The deferral-system forseeks to solve these issues while retaining the advantages of the promise-system. */ /** * @author fenris * @desc activates the deferral and handles its output according to a given procedure * @param {(value : type_value)=>void} procedure a function which receives the output of the deferral as argument */ function deferral_use(deferral, input, procedure) { deferral.representation(input).then(value => { procedure(value); }, reason => { throw reason; }); } lib_call.deferral_use = deferral_use; /** * @author fenris * @desc creates a deferral-subject (similar to "new Promise", where "convey" reflects "resolve"/"reject") */ function deferral_make(handler) { return ({ "representation": ((input) => (new Promise((resolve, reject) => { handler(input, resolve); }))) }); } lib_call.deferral_make = deferral_make; /** * @author fenris * @desc wraps a simple function into a deferral (similar to "Promise.resolve"/"Promise.reject") */ function deferral_wrap(function_) { return (deferral_make((input, convey) => convey(function_(input)))); } lib_call.deferral_wrap = deferral_wrap; /** * @author fenris */ function deferral_id() { return (deferral_make((input, convey) => convey(input))); } lib_call.deferral_id = deferral_id; /** * @author fenris */ function deferral_const(value) { return (deferral_make((input, convey) => convey(value))); } lib_call.deferral_const = deferral_const; /** * @author fenris */ function deferral_delay(output, delay) { return (deferral_make((input, convey) => { setTimeout(() => convey(output), delay); })); } lib_call.deferral_delay = deferral_delay; /** * @author fenris * @desc connects two deferrals to form a new one; the output of the first is taken as input for the second * (similar to "Promise.then" when passing a function which returns a new promise) * @param {type_deferral<type_value1>} first a simple deferral * @param {(value1 : type_value1)=>type_deferral<type_value2>} second a function depending from a value returning a deferral */ function deferral_compose_serial(first, second) { return { "representation": ((input) => first.representation(input).then((between) => second.representation(between))) }; } lib_call.deferral_compose_serial = deferral_compose_serial; /** * @author fenris */ function deferral_compose_parallel({ "left": deferral_left, "right": deferral_right, }) { return (deferral_make((input, convey) => { let object = { "left": lib_maybe.make_nothing(), "right": lib_maybe.make_nothing(), }; let finish = function () { if (lib_maybe.is_just(object.left) && lib_maybe.is_just(object.right)) { let result = { "left": lib_maybe.cull(object.left), "right": lib_maybe.cull(object.right), }; convey(result); } else { // do nothing } }; deferral_use(deferral_left, input, output_left => { object.left = lib_maybe.make_just(output_left); finish(); }); deferral_use(deferral_right, input, output_right => { object.right = lib_maybe.make_just(output_right); finish(); }); })); } lib_call.deferral_compose_parallel = deferral_compose_parallel; /** * @author fenris * @desc repeatedly applied serial composition */ function deferral_chain(members) { return (members.reduce( // (result, current) => deferral_compose_serial<type_value, type_value, type_value>(result, current), deferral_compose_serial, deferral_id())); } lib_call.deferral_chain = deferral_chain; /** * @author fenris */ /* export function deferral_bunch<type_input, type_output>( members : {[name : string] : type_deferral<type_input, type_output>} ) : type_deferral<type_input, {[name : string] : type_output}> { } */ })(lib_call || (lib_call = {})); /* This file is part of »bacterio-plankton:call«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:call« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:call« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:call«. If not, see <http://www.gnu.org/licenses/>. */ var lib_call; (function (lib_call) { /** * @author fenris */ class class_deferral { /** * @author fenris */ constructor(subject) { this.subject = subject; } /** * @author fenris */ static _cram(subject) { return (new class_deferral(subject)); } /** * @author fenris */ static _tear(instance) { return instance.subject; } /** * @author fenris */ static make(handler) { return (class_deferral._cram(lib_call.deferral_make(handler))); } /** * @author fenris */ use(input, procedure) { return (lib_call.deferral_use(class_deferral._tear(this), input, procedure)); } /** * @author fenris */ compose_serial(second) { return (class_deferral._cram(lib_call.deferral_compose_serial(class_deferral._tear(this), class_deferral._tear(second)))); } /** * @author fenris */ static chain(members) { return (class_deferral._cram(lib_call.deferral_chain(members.map(member => class_deferral._tear(member))))); } /** * @author fenris */ static wrap(function_) { return (class_deferral._cram(lib_call.deferral_wrap(function_))); } /** * @author fenris */ static const_(value) { return (class_deferral._cram(lib_call.deferral_const(value))); } /** * @author fenris */ static delay(output, delay) { return (class_deferral._cram(lib_call.deferral_delay(output, delay))); } } lib_call.class_deferral = class_deferral; })(lib_call || (lib_call = {})); /* This file is part of »bacterio-plankton:call«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:call« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:call« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:call«. If not, see <http://www.gnu.org/licenses/>. */ var lib_call; (function (lib_call) { /** * @author fenris */ function timeout(function_, delay) { return ( /*window.*/ setTimeout(function_, delay)); } lib_call.timeout = timeout; /** * @desc a definition for a value being "defined" * @author neuc */ function is_def(obj, null_is_valid = false) { return (!((typeof (obj) === "undefined") || (!null_is_valid && (obj === null)))); } lib_call.is_def = is_def; /** * @desc returns the value if set and, when a type is specified, if the type is correct, if not return default_value * @author neuc */ function def_val(value, default_value, type = null, null_is_valid = false) { if (is_def(value, null_is_valid) && (is_def(type) ? ((typeof value === type) || ((value === null) && null_is_valid)) : true)) { return value; } else { return default_value; } } lib_call.def_val = def_val; ; /** * @desc just the empty function; useful for some callbacks etc. * @author fenris */ function nothing() { } lib_call.nothing = nothing; /** * @desc outputs * @author fenris */ function output(...args) { lib_log.info.apply(lib_log, args); } lib_call.output = output; /** * @desc converts the "arguments"-map into an array * @param {Object} args * @author fenris */ function args2list(args) { return Object.keys(args).map(key => args[key]); } lib_call.args2list = args2list; /** * @desc provides the call for an attribute of a class as a regular function * @param {string} name the name of the attribute * @return {*} * @author fenris */ function attribute(name) { return ((object) => object[name]); } lib_call.attribute = attribute; /** * @desc provides a method of a class as a regular function * @param {string} name the name of the method * @return {function} * @author fenris */ function method(name) { return (function (object) { return object[name].apply(object, args2list(arguments).slice(1)); }); } lib_call.method = method; /** * @author fenris */ function distinguish(unival, handlers, fallback = null) { if (unival.kind in handlers) { let handler = handlers[unival.kind]; return handler(unival.data); } else { let message = ("unhandled kind '" + unival.kind + "'"); if (fallback !== null) { console.warn(message); return fallback(unival); } else { throw (new Error(message)); } } } lib_call.distinguish = distinguish; })(lib_call || (lib_call = {})); /* This file is part of »bacterio-plankton:comm-client«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:comm-client« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:comm-client« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:comm-client«. If not, see <http://www.gnu.org/licenses/>. */ var lib_comm; (function (lib_comm) { /** * @author fenris */ lib_trait.define("comm-client", { "state": null, "message_in": null, "message_out": null }, { "setup": { "shape": { "name": "function", "parameters": { "shape_input": { "name": "variable", "parameters": { "name": "state" } }, "shape_output": { "name": "promise", "parameters": { "shape_result": { "name": "variable", "parameters": { "name": "state" } }, "shape_reason": { "name": "any" } } } } } }, "send": { "shape": { "name": "function", "parameters": { "shape_input": { "name": "variable", "parameters": { "name": "state" } }, "shape_output": { "name": "function", "parameters": { "shape_input": { "name": "variable", "parameters": { "name": "message_out" } }, "shape_output": { "name": "promise", "parameters": { "shape_result": { "name": "variable", "parameters": { "name": "message_in" } }, "shape_reason": { "name": "any" } } } } } } } } }); })(lib_comm || (lib_comm = {})); /* This file is part of »bacterio-plankton:comm-client«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:comm-client« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:comm-client« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:comm-client«. If not, see <http://www.gnu.org/licenses/>. */ /* This file is part of »bacterio-plankton:comm-client«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:comm-client« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:comm-client« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:comm-client«. If not, see <http://www.gnu.org/licenses/>. */ var lib_comm; (function (lib_comm) { /** * @author fenris */ lib_comm.default_parameters_client_http = { "protocol": "http", "host": null, "port": null, "path": null, "omit_protocol": false, "content_type": "plain/text; charset=utf-8", "with_credentials": false, }; })(lib_comm || (lib_comm = {})); /* This file is part of »bacterio-plankton:comm-client«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:comm-client« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:comm-client« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:comm-client«. If not, see <http://www.gnu.org/licenses/>. */ var lib_comm; (function (lib_comm) { /** * @author fenris */ function client_http_construct(parameters = {}, method = "POST", headers = {}) { return { "parameters": lib_object.patched(lib_comm.default_parameters_client_http, parameters), "method": method.toLowerCase(), "headers": headers, }; } lib_comm.client_http_construct = client_http_construct; /** * @author fenris */ function client_http_setup(subject) { return (lib_call.promise_resolve(undefined)); } lib_comm.client_http_setup = client_http_setup; /** * @author fenris */ function client_http_send(subject, message) { return (lib_call.promise_make((resolve, reject) => { if ((subject.method === "get") && (message !== null)) { let message_ = "can not send data via GET"; reject(new Error(message_)); } else { let request = new XMLHttpRequest(); let url = lib_string.make_url({ "protocol": (subject.parameters.omit_protocol ? null : subject.parameters.protocol), "host": subject.parameters.host, "port": subject.parameters.port, "path": subject.parameters.path, }); request.open(subject.method.toUpperCase(), url, true); lib_object.to_array(subject.headers).forEach(({ "key": key, "value": value }) => { request.setRequestHeader(key, value); }); request.setRequestHeader("Content-Type", subject.parameters.content_type); if (subject.parameters.with_credentials) { request.withCredentials = true; } /* request.onerror = () => { debugger; reject(new Error("unhandled error")); }; */ request.onreadystatechange = () => { switch (request.readyState) { case XMLHttpRequest.DONE: { switch (request.status) { case 0: { reject(new Error("XMLHttpRequest failed")); break; } default: { let answer = { "code": request.status, "text": request.responseText }; resolve(answer); break; } } break; } default: { // console.warn(`unhandled readyState ${request.readyState}`); break; } } }; request.send(message); } })); } lib_comm.client_http_send = client_http_send; /** * @author Martin Springwald <springwald@greenscale.de> * @author Christian Neubauer <neubauer@greenscale.de> * @author Christian Fraß <frass@greenscale.de> */ function http_get_blob({ "url": url, "mime": type }) { let request = new XMLHttpRequest(); return new Promise((resolve, reject) => { request.open("GET", url, true); request.responseType = "arraybuffer"; request.onload = function () { if (request.status === 200) { resolve(new Blob([request.response], { "type": type })); } else { reject(new Error("error on blob-load")); } }; request.onerror = function () { reject(undefined); }; request.send(); }); } lib_comm.http_get_blob = http_get_blob; })(lib_comm || (lib_comm = {})); /* This file is part of »bacterio-plankton:comm-client«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:comm-client« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:comm-client« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:comm-client«. If not, see <http://www.gnu.org/licenses/>. */ var lib_comm; (function (lib_comm) { /** * @author fenris */ lib_trait.attend("comm-client", "http", { "state": { "name": "any" }, "message_in": { "name": "string" }, "message_out": { "name": "any" // type_response_http } }, { "setup": () => (state) => { return lib_comm.client_http_setup(state).then(_ => lib_call.promise_resolve(state)); }, "send": () => (state) => (message) => { return lib_comm.client_http_send(state, message); } }); })(lib_comm || (lib_comm = {})); /* This file is part of »bacterio-plankton:comm-client«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:comm-client« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:comm-client« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:comm-client«. If not, see <http://www.gnu.org/licenses/>. */ var lib_comm; (function (lib_comm) { /** * @author fenris */ class class_client_http { /** * @author fenris */ constructor(parameters = lib_comm.default_parameters_client_http, method = "POST", headers = {}) { this.subject = lib_comm.client_http_construct(parameters, method, headers); } /** * @author fenris * @implementation */ setup() { return lib_comm.client_http_setup(this.subject); } /** * @author fenris * @implementation */ send(message) { return lib_comm.client_http_send(this.subject, message); } } lib_comm.class_client_http = class_client_http; })(lib_comm || (lib_comm = {})); /* This file is part of »bacterio-plankton:comm-client«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:comm-client« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:comm-client« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:comm-client«. If not, see <http://www.gnu.org/licenses/>. */ var lib_comm; (function (lib_comm) { /** * @author fenris */ class class_client_mhttp { /** * @author fenris */ constructor(parameters = lib_comm.default_parameters_client_http) { this.core = new lib_comm.class_client_http(parameters); } /** * @author fenris * @implementation */ setup() { return this.core.setup(); } /** * @author fenris * @implementation */ send(message) { return (this.core.send(message) .then((answer) => { switch (answer.code) { case 200: { return lib_call.promise_resolve(answer.text /*.toString()*/); break; } default: { return lib_call.promise_reject(new Error(`XMLHttpRequest failed; status was ${answer.code.toFixed(0)}`)); break; } } })); } } lib_comm.class_client_mhttp = class_client_mhttp; })(lib_comm || (lib_comm = {})); /* This file is part of »bacterio-plankton:comm-client«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:comm-client« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:comm-client« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:comm-client«. If not, see <http://www.gnu.org/licenses/>. */ var lib_comm; (function (lib_comm) { /** * @desc wrapper for string-based clients * @author fenris */ class class_client_complex { /** * @author fenris */ constructor(core, code = new lib_code.class_code_pair(new lib_code.class_code_json(), new lib_code.class_code_uri())) { this.core = core; this.code = code; } /** * @author fenris * @implementation */ setup() { return this.core.setup(); } /** * @author fenris * @implementation */ send(message) { const message_out_raw = this.code.encode(message); return (this.core.send(message_out_raw) .then((message_in_raw) => { const message_in = this.code.decode(message_in_raw); return lib_call.promise_resolve(message_in); })); } } lib_comm.class_client_complex = class_client_complex; })(lib_comm || (lib_comm = {})); async function api_query ( action_name, input ) { const client = lib_comm.client_http_construct ( { "protocol": "http", "host": "localhost", "port": 7777, "path": "/foo/bar", } ); const message = JSON.stringify ( { "action": action_name, "input": input, } ); const answer = await lib_comm.client_http_send(client, message); if (! (answer.code === 200)) { return Promise.reject("server side error"); } else { const output = JSON.parse(answer.text); return Promise.resolve(output); } } /** */ function main ( ) { document.querySelector("button").addEventListener ( "click", async function () { const language_from = document.querySelector("select[name=language_from]").value; const language_to = document.querySelector("select[name=language_to]").value; const token = document.querySelector("input[name=token]").value; const result = await api_query ( "translate", { "language_from": language_from, "language_to": language_to, "token": token, } ); const dom_ul = document.querySelector("#result"); dom_ul.textContent = ""; for (const entry of result) { let dom_li = document.createElement("li"); dom_li.textContent = (`[${entry.language_from}] ${entry.value_from} ~ [${entry.language_to}] ${entry.value_to}`); dom_ul.appendChild(dom_li); } } ); } document.addEventListener("DOMContentLoaded", function (event) {main();});