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
lib
plankton
object
logic-impl.js
[add] client
Christian Fraß
commited
6ae1cb8
at 2021-03-08 23:52:24
logic-impl.js
Blame
History
Raw
/* 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;