[upd] plankton
This commit is contained in:
parent
b02584b128
commit
4f6b4f0564
3 changed files with 914 additions and 8 deletions
209
lib/plankton/plankton.d.ts
vendored
209
lib/plankton/plankton.d.ts
vendored
|
@ -1,15 +1,15 @@
|
||||||
/**
|
/**
|
||||||
* @author fenris
|
* @author fenris
|
||||||
*/
|
*/
|
||||||
declare type int = number;
|
type int = number;
|
||||||
/**
|
/**
|
||||||
* @author fenris
|
* @author fenris
|
||||||
*/
|
*/
|
||||||
declare type float = number;
|
type float = number;
|
||||||
/**
|
/**
|
||||||
* @author fenris
|
* @author fenris
|
||||||
*/
|
*/
|
||||||
declare type type_date = {
|
type type_date = {
|
||||||
year: int;
|
year: int;
|
||||||
month: int;
|
month: int;
|
||||||
day: int;
|
day: int;
|
||||||
|
@ -17,7 +17,7 @@ declare type type_date = {
|
||||||
/**
|
/**
|
||||||
* @author fenris
|
* @author fenris
|
||||||
*/
|
*/
|
||||||
declare type type_time = {
|
type type_time = {
|
||||||
hour: int;
|
hour: int;
|
||||||
minute: int;
|
minute: int;
|
||||||
second: int;
|
second: int;
|
||||||
|
@ -25,7 +25,7 @@ declare type type_time = {
|
||||||
/**
|
/**
|
||||||
* @author fenris
|
* @author fenris
|
||||||
*/
|
*/
|
||||||
declare type type_datetimeobject = {
|
type type_datetimeobject = {
|
||||||
date: type_date;
|
date: type_date;
|
||||||
time: type_time;
|
time: type_time;
|
||||||
};
|
};
|
||||||
|
@ -42,7 +42,7 @@ declare namespace lib_plankton.base {
|
||||||
/**
|
/**
|
||||||
* @author fenris
|
* @author fenris
|
||||||
*/
|
*/
|
||||||
declare type type_pseudopointer<type_value> = {
|
type type_pseudopointer<type_value> = {
|
||||||
value: type_value;
|
value: type_value;
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
|
@ -1251,6 +1251,199 @@ declare var printf: typeof lib_plankton.string.printf;
|
||||||
declare var eml_log: any;
|
declare var eml_log: any;
|
||||||
declare var track_exports: any;
|
declare var track_exports: any;
|
||||||
declare var make_logger: (prefix: any, current_loglevel: any) => (obj: any, lvl: any) => void;
|
declare var make_logger: (prefix: any, current_loglevel: any) => (obj: any, lvl: any) => void;
|
||||||
|
declare namespace lib_plankton.object {
|
||||||
|
/**
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
function fetch<type_value>(object: Object, fieldname: string, fallback?: type_value, escalation?: int): type_value;
|
||||||
|
/**
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
function map<type_from, type_to>(object_from: {
|
||||||
|
[key: string]: type_from;
|
||||||
|
}, transformator: (value_from: type_from, key?: string) => type_to): {
|
||||||
|
[key: string]: type_to;
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* @desc gibt ein Objekt mit bestimmten Einträgen des Eingabe-Objekts zurück
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
function filter<type_value>(object_from: {
|
||||||
|
[key: string]: type_value;
|
||||||
|
}, predicate: (value_from: type_value, key?: string) => boolean): {
|
||||||
|
[key: string]: type_value;
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* @desc wandelt ein Array mit Einträgen der Form {key,value} in ein entsprechendes Objekt um
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
function from_array<type_value>(array: Array<{
|
||||||
|
key: string;
|
||||||
|
value: type_value;
|
||||||
|
}>): {
|
||||||
|
[key: string]: type_value;
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* @desc wandelt ein Objekt in ein entsprechendes Array mit Einträgen der Form {key,value} um
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
function to_array<type_value>(object: {
|
||||||
|
[key: string]: type_value;
|
||||||
|
}): Array<{
|
||||||
|
key: string;
|
||||||
|
value: type_value;
|
||||||
|
}>;
|
||||||
|
/**
|
||||||
|
* @desc gibt eine Liste von Schlüsseln eines Objekts zurück
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
function keys(object: {
|
||||||
|
[key: string]: any;
|
||||||
|
}): Array<string>;
|
||||||
|
/**
|
||||||
|
* @desc gibt eine Liste von Werten eines Objekts zurück
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
function values<type_value>(object: {
|
||||||
|
[key: string]: type_value;
|
||||||
|
}): Array<type_value>;
|
||||||
|
/**
|
||||||
|
* @desc liest ein Baum-artiges Objekt an einer bestimmten Stelle aus
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
function path_read<type_value>(object: Object, path: string, fallback?: type_value, escalation?: int): type_value;
|
||||||
|
/**
|
||||||
|
* @desc schreibt einen Wert an eine bestimmte Stelle in einem Baum-artigen Objekt
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
function path_write<type_value>(object: Object, path: string, value: type_value, construct?: boolean): void;
|
||||||
|
/**
|
||||||
|
* @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: Object, pattern: Object, collate?: typeof instance_collate): boolean;
|
||||||
|
/**
|
||||||
|
* @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: any, separator?: string, key_for_element?: (index: int) => string): Object;
|
||||||
|
/**
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
function clash(x: {
|
||||||
|
[key: string]: any;
|
||||||
|
}, y: {
|
||||||
|
[key: string]: any;
|
||||||
|
}, { "overwrite": overwrite, "hooks": { "existing": hook_existing, }, }?: {
|
||||||
|
overwrite?: boolean;
|
||||||
|
hooks?: {
|
||||||
|
existing?: (key?: string, value_old?: any, value_new?: any) => void;
|
||||||
|
};
|
||||||
|
}): {
|
||||||
|
[key: string]: any;
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
function patch(core: Object, mantle: Object, deep?: boolean, path?: string): void;
|
||||||
|
/**
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
function patched(core: Object, mantle: Object, deep?: boolean): Object;
|
||||||
|
/**
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
function attached(object: Object, key: string, value: any): Object;
|
||||||
|
/**
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
function copy(object: Object): Object;
|
||||||
|
}
|
||||||
|
declare namespace lib_plankton.translate {
|
||||||
|
/**
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
type type_package_meta = {
|
||||||
|
identifier: string;
|
||||||
|
name?: string;
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
type type_package = {
|
||||||
|
meta: type_package_meta;
|
||||||
|
tree: {
|
||||||
|
[id: string]: string;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* @desc the level of verbosity, specifiying how much output the system shall provide about its actions
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
var _verbosity: int;
|
||||||
|
/**
|
||||||
|
* @desc moves a language to the top of the order, making it the primary one
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
function promote(identifier: string): void;
|
||||||
|
/**
|
||||||
|
* @desc adds a package to the sytem
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
function add(package_: type_package): void;
|
||||||
|
/**
|
||||||
|
* @desc integrates a package to the system, i.e. creates a new one if none existed so far or merges with an existing one
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
function feed(package_: type_package): void;
|
||||||
|
/**
|
||||||
|
* @desc tries to retrieve a translation for a specific package identifier
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
function fetch(identifier: string, path: string, args?: {
|
||||||
|
[id: string]: string;
|
||||||
|
}): lib_plankton.pod.type_pod<string>;
|
||||||
|
/**
|
||||||
|
* @desc retrieves a string by going through the order and trying to fetch it for the current entry
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
function get(path: string, args?: {
|
||||||
|
[id: string]: string;
|
||||||
|
}, fallback?: string): string;
|
||||||
|
/**
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
function list(): Array<type_package_meta>;
|
||||||
|
/**
|
||||||
|
* @author fenris
|
||||||
|
* @todo get rid of this; it's currenly needed only for the cdh-internal lib_completion
|
||||||
|
*/
|
||||||
|
function paths(): Array<string>;
|
||||||
|
/**
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
function initialize({ "logprefix": logprefix, "verbosity": verbosity, "packages": packages, "order": order, "autopromote": autopromote, }?: {
|
||||||
|
logprefix?: string;
|
||||||
|
verbosity?: int;
|
||||||
|
packages?: Array<type_package>;
|
||||||
|
order?: Array<string>;
|
||||||
|
autopromote?: boolean;
|
||||||
|
}): Promise<void>;
|
||||||
|
}
|
||||||
|
declare namespace lib_plankton.translate {
|
||||||
|
/**
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
function iso_639_1_to_iso_639_2(iso6391: string): string;
|
||||||
|
/**
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
function stance(str: string): string;
|
||||||
|
}
|
||||||
declare namespace lib_plankton.database {
|
declare namespace lib_plankton.database {
|
||||||
/**
|
/**
|
||||||
*/
|
*/
|
||||||
|
@ -1513,7 +1706,7 @@ declare namespace lib_plankton.storage.memory {
|
||||||
clear(): Promise<void>;
|
clear(): Promise<void>;
|
||||||
write(key: any, value: any): Promise<boolean>;
|
write(key: any, value: any): Promise<boolean>;
|
||||||
delete(key: any): Promise<void>;
|
delete(key: any): Promise<void>;
|
||||||
read(key: any): Promise<type_item>;
|
read(key: any): Promise<Awaited<type_item>>;
|
||||||
search(term: any): Promise<{
|
search(term: any): Promise<{
|
||||||
key: string;
|
key: string;
|
||||||
preview: string;
|
preview: string;
|
||||||
|
@ -1820,7 +2013,7 @@ declare namespace lib_plankton.zoo_input {
|
||||||
private read_only;
|
private read_only;
|
||||||
/**
|
/**
|
||||||
*/
|
*/
|
||||||
dom_input: (null | HTMLInputElement);
|
private dom_input;
|
||||||
/**
|
/**
|
||||||
*/
|
*/
|
||||||
private hooks_change;
|
private hooks_change;
|
||||||
|
|
|
@ -3653,6 +3653,718 @@ var make_logger = (function () {
|
||||||
return make_logger;
|
return make_logger;
|
||||||
})();
|
})();
|
||||||
/*
|
/*
|
||||||
|
This file is part of »bacterio-plankton:object«.
|
||||||
|
|
||||||
|
Copyright 2016-2023 '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_plankton;
|
||||||
|
(function (lib_plankton) {
|
||||||
|
var object;
|
||||||
|
(function (object_1) {
|
||||||
|
/**
|
||||||
|
* @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 '".concat(fieldname, "' not in structure"));
|
||||||
|
message += ("; using fallback value '".concat(String(fallback), "'"));
|
||||||
|
// console.warn(message);
|
||||||
|
return fallback;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 2: {
|
||||||
|
var message = ("field '".concat(fieldname, "' not in structure"));
|
||||||
|
throw (new Error(message));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
throw (new Error("invalid escalation level ".concat(escalation)));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
object_1.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;
|
||||||
|
}
|
||||||
|
object_1.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;
|
||||||
|
}
|
||||||
|
object_1.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;
|
||||||
|
}
|
||||||
|
object_1.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;
|
||||||
|
}
|
||||||
|
object_1.to_array = to_array;
|
||||||
|
/**
|
||||||
|
* @desc gibt eine Liste von Schlüsseln eines Objekts zurück
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
function keys(object) {
|
||||||
|
return Object.keys(object);
|
||||||
|
}
|
||||||
|
object_1.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; });
|
||||||
|
}
|
||||||
|
object_1.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 = lib_plankton.object.fetch(position_1, step, null, 0);
|
||||||
|
return (position_1 != null);
|
||||||
|
});
|
||||||
|
if (reachable) {
|
||||||
|
return lib_plankton.object.fetch(position_1, steps[steps.length - 1], fallback, escalation);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return lib_plankton.object.fetch({}, "_dummy_", fallback, escalation);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
object_1.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_ = lib_plankton.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 '".concat(path, "' does not exist and may not be constructed"));
|
||||||
|
throw (new Error(message));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
object_1.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]); });
|
||||||
|
}
|
||||||
|
object_1.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_2 = (value);
|
||||||
|
Object.keys(object_2)
|
||||||
|
.forEach(function (key) {
|
||||||
|
integrate(result_2, key, object_2[key]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result_2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
object_1.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, _f = _e["existing"], hook_existing = _f === void 0 ? null : _f;
|
||||||
|
if (hook_existing == null) {
|
||||||
|
(function (key, value_old, value_new) { return console.warn("field ".concat(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;
|
||||||
|
}
|
||||||
|
object_1.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 : "".concat(path, ".").concat(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 '".concat(path_, "'; core has type '").concat(typeof (value_core), "' and mantle has type '").concat(typeof (value_mantle), "'");
|
||||||
|
console.warn(message);
|
||||||
|
}
|
||||||
|
core[key] = value_mantle;
|
||||||
|
// throw (new Error(message));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
object_1.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;
|
||||||
|
}
|
||||||
|
object_1.patched = patched;
|
||||||
|
/**
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
function attached(object, key, value) {
|
||||||
|
var mantle = {};
|
||||||
|
mantle[key] = value;
|
||||||
|
return patched(object, mantle, false);
|
||||||
|
}
|
||||||
|
object_1.attached = attached;
|
||||||
|
/**
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
function copy(object) {
|
||||||
|
return patched({}, object);
|
||||||
|
}
|
||||||
|
object_1.copy = copy;
|
||||||
|
})(object = lib_plankton.object || (lib_plankton.object = {}));
|
||||||
|
})(lib_plankton || (lib_plankton = {}));
|
||||||
|
/*
|
||||||
|
This file is part of »bacterio-plankton:translate«.
|
||||||
|
|
||||||
|
Copyright 2016-2023 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
|
||||||
|
<info@greenscale.de>
|
||||||
|
|
||||||
|
»bacterio-plankton:translate« 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:translate« 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:translate«. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
var lib_plankton;
|
||||||
|
(function (lib_plankton) {
|
||||||
|
var translate;
|
||||||
|
(function (translate) {
|
||||||
|
/**
|
||||||
|
* @desc contains the sets of strings
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
var _packages = {};
|
||||||
|
/**
|
||||||
|
* @desc specifies in which order the languages shall be queried; if getting a string from language #0 fails, the
|
||||||
|
* system tries to get it from language #1, and so on
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
var _order = [];
|
||||||
|
/**
|
||||||
|
* @desc whether to automatically promote the language of a newly added package
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
var _autopromote = false;
|
||||||
|
/**
|
||||||
|
* @desc the level of verbosity, specifiying how much output the system shall provide about its actions
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
translate._verbosity = 1;
|
||||||
|
/**
|
||||||
|
* @desc which initial string to use for log-outputs
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
var _logprefix = "[lib_translate]";
|
||||||
|
/**
|
||||||
|
* @desc moves a language to the top of the order, making it the primary one
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
function promote(identifier) {
|
||||||
|
if (Object.keys(_packages).indexOf(identifier) < 0) {
|
||||||
|
if (translate._verbosity >= 1) {
|
||||||
|
console.warn(`${_logprefix} package '${identifier}' doesn't exist yet`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let position = _order.indexOf(identifier);
|
||||||
|
if (position >= 0) {
|
||||||
|
if (translate._verbosity >= 2) {
|
||||||
|
console.info(`${_logprefix} '${identifier}' already in order; will promote it`);
|
||||||
|
}
|
||||||
|
_order.splice(position, 1);
|
||||||
|
}
|
||||||
|
_order.unshift(identifier);
|
||||||
|
if (translate._verbosity >= 2) {
|
||||||
|
console.info(`${_logprefix} order is now ${_order.toString()}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
translate.promote = promote;
|
||||||
|
/**
|
||||||
|
* @desc adds a package to the sytem
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
function add(package_) {
|
||||||
|
let identifier = package_.meta.identifier;
|
||||||
|
if (identifier in _packages) {
|
||||||
|
if (translate._verbosity >= 1) {
|
||||||
|
console.warn(`${_logprefix} package '${identifier}' has already been added; will overwrite`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (translate._verbosity >= 2) {
|
||||||
|
console.log(`${_logprefix} got package '${identifier}'`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_packages[identifier] = package_;
|
||||||
|
if (_autopromote) {
|
||||||
|
promote(identifier);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
translate.add = add;
|
||||||
|
/**
|
||||||
|
* @desc integrates a package to the system, i.e. creates a new one if none existed so far or merges with an existing one
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
function feed(package_) {
|
||||||
|
let identifier = package_.meta.identifier;
|
||||||
|
if (identifier in _packages) {
|
||||||
|
lib_plankton.object.patch(_packages[identifier].tree, package_.tree, true);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (translate._verbosity >= 2) {
|
||||||
|
console.info(`${_logprefix} package '${identifier}' didn't exist so far; will create it now`);
|
||||||
|
}
|
||||||
|
add(package_);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
translate.feed = feed;
|
||||||
|
/**
|
||||||
|
* @desc tries to retrieve a translation for a specific package identifier
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
function fetch(identifier, path, args = {}) {
|
||||||
|
if (!(identifier in _packages)) {
|
||||||
|
if (translate._verbosity >= 1) {
|
||||||
|
console.warn(`${_logprefix} no package '${identifier}'`);
|
||||||
|
}
|
||||||
|
return (lib_plankton.pod.make_empty());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// let str : string = lib_plankton.object.path_read<string>(_packages[identifier].tree, path);
|
||||||
|
let str = _packages[identifier].tree[path];
|
||||||
|
if (str == undefined) {
|
||||||
|
if (translate._verbosity >= 1) {
|
||||||
|
console.warn(`${_logprefix} string '${path}' missing in package '${identifier}'`);
|
||||||
|
}
|
||||||
|
return (lib_plankton.pod.make_empty());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// resolve references
|
||||||
|
{
|
||||||
|
let regexp_reference = new RegExp("#\\(([\\w\\.]*)(?:\\?(\\w+)=(\\w+)(?:&(\\w+)=(\\w+))*)?\\)");
|
||||||
|
while (true) {
|
||||||
|
let matching = regexp_reference.exec(str);
|
||||||
|
if (matching != null) {
|
||||||
|
let path_ = matching[1];
|
||||||
|
let args_ = {};
|
||||||
|
if (translate._verbosity >= 2) {
|
||||||
|
// console.info(`${_logprefix} found reference to '${path_}' with args ${JSON.stringify(args_)}`);
|
||||||
|
console.info(`${_logprefix} found reference to '${path_}'`);
|
||||||
|
}
|
||||||
|
// parse args
|
||||||
|
{
|
||||||
|
for (let index = 2; index <= matching.length - 1; index += 2) {
|
||||||
|
let id = matching[index + 0];
|
||||||
|
let value = matching[index + 1];
|
||||||
|
if (id != undefined) {
|
||||||
|
args_[id] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// fetch referenced string
|
||||||
|
{
|
||||||
|
let result_ = fetch(identifier, path_, args_);
|
||||||
|
if (lib_plankton.pod.is_filled(result_)) {
|
||||||
|
let front = str.slice(0, matching.index);
|
||||||
|
let back = str.slice(matching.index + matching[0].length);
|
||||||
|
str = (front + lib_plankton.pod.cull(result_) + back);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return (lib_plankton.pod.make_empty());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// insert arguments
|
||||||
|
{
|
||||||
|
str = lib_plankton.string.coin(str, args);
|
||||||
|
}
|
||||||
|
return (lib_plankton.pod.make_filled(str));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
translate.fetch = fetch;
|
||||||
|
/**
|
||||||
|
* @desc retrieves a string by going through the order and trying to fetch it for the current entry
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
function get(path, args = {}, fallback = null) {
|
||||||
|
if (fallback == null) {
|
||||||
|
fallback = `{${path}}`;
|
||||||
|
}
|
||||||
|
if (translate._verbosity >= 2) {
|
||||||
|
console.info(`${_logprefix} getting translation for string '${path}' with arguments ${JSON.stringify(args)} …`);
|
||||||
|
}
|
||||||
|
let result = lib_plankton.pod.make_empty();
|
||||||
|
let found = _order.some(identifier => {
|
||||||
|
if (translate._verbosity >= 2) {
|
||||||
|
console.info(`${_logprefix} trying package '${identifier}' …`);
|
||||||
|
}
|
||||||
|
let result_ = fetch(identifier, path, args);
|
||||||
|
if (lib_plankton.pod.is_filled(result_)) {
|
||||||
|
result = result_;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (found) {
|
||||||
|
let str = lib_plankton.pod.cull(result);
|
||||||
|
if (translate._verbosity >= 3) {
|
||||||
|
console.info(`${_logprefix} found translation: '${str}'`);
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
let str = fallback;
|
||||||
|
if (translate._verbosity >= 1) {
|
||||||
|
console.warn(`${_logprefix} no package provides a translation for string '${path}'; will use the fallback translation '${str}'`);
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
translate.get = get;
|
||||||
|
/**
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
function list() {
|
||||||
|
return lib_plankton.object.to_array(_packages).map(x => x.value.meta);
|
||||||
|
}
|
||||||
|
translate.list = list;
|
||||||
|
/**
|
||||||
|
* @author fenris
|
||||||
|
* @todo get rid of this; it's currenly needed only for the cdh-internal lib_completion
|
||||||
|
*/
|
||||||
|
function paths() {
|
||||||
|
return lib_plankton.object.keys(lib_plankton.object.flatten(_packages[_order[0]].tree));
|
||||||
|
}
|
||||||
|
translate.paths = paths;
|
||||||
|
/**
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
function initialize({ "logprefix": logprefix = undefined, "verbosity": verbosity = undefined, "packages": packages = [], "order": order = undefined, "autopromote": autopromote = undefined, } = {}) {
|
||||||
|
return (Promise.resolve(undefined)
|
||||||
|
// set variables
|
||||||
|
.then(_ => {
|
||||||
|
if (logprefix != undefined)
|
||||||
|
_logprefix = logprefix;
|
||||||
|
if (verbosity != undefined)
|
||||||
|
translate._verbosity = verbosity;
|
||||||
|
// _packages = {};
|
||||||
|
if (order != undefined)
|
||||||
|
_order = order;
|
||||||
|
if (autopromote != undefined)
|
||||||
|
_autopromote = autopromote;
|
||||||
|
return Promise.resolve(undefined);
|
||||||
|
})
|
||||||
|
// feed
|
||||||
|
.then(_ => {
|
||||||
|
packages.forEach(feed);
|
||||||
|
return Promise.resolve(undefined);
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
translate.initialize = initialize;
|
||||||
|
})(translate = lib_plankton.translate || (lib_plankton.translate = {}));
|
||||||
|
})(lib_plankton || (lib_plankton = {}));
|
||||||
|
/*
|
||||||
|
This file is part of »bacterio-plankton:translate«.
|
||||||
|
|
||||||
|
Copyright 2016-2023 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
|
||||||
|
<info@greenscale.de>
|
||||||
|
|
||||||
|
»bacterio-plankton:translate« 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:translate« 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:translate«. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
var lib_plankton;
|
||||||
|
(function (lib_plankton) {
|
||||||
|
var translate;
|
||||||
|
(function (translate) {
|
||||||
|
/**
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
function iso_639_1_to_iso_639_2(iso6391) {
|
||||||
|
let mapping = {
|
||||||
|
"af": "afr",
|
||||||
|
"ar": "ara",
|
||||||
|
"bg": "bul",
|
||||||
|
"cs": "ces",
|
||||||
|
"da": "dan",
|
||||||
|
"de": "deu",
|
||||||
|
"el": "ell",
|
||||||
|
"en": "eng",
|
||||||
|
"eo": "epo",
|
||||||
|
"es": "esp",
|
||||||
|
"fa": "fas",
|
||||||
|
"fi": "fin",
|
||||||
|
"fr": "fra",
|
||||||
|
"hi": "hin",
|
||||||
|
"hr": "hrv",
|
||||||
|
"hu": "hun",
|
||||||
|
"is": "isl",
|
||||||
|
"it": "ita",
|
||||||
|
"ja": "jpn",
|
||||||
|
"ko": "kor",
|
||||||
|
"nb": "nob",
|
||||||
|
"nl": "nld",
|
||||||
|
"nn": "nno",
|
||||||
|
"pt": "por",
|
||||||
|
"pl": "pol",
|
||||||
|
"ro": "ron",
|
||||||
|
"ru": "rus",
|
||||||
|
"sk": "slk",
|
||||||
|
"sv": "swe",
|
||||||
|
"zh": "zho",
|
||||||
|
};
|
||||||
|
return mapping[iso6391];
|
||||||
|
}
|
||||||
|
translate.iso_639_1_to_iso_639_2 = iso_639_1_to_iso_639_2;
|
||||||
|
/**
|
||||||
|
* @author fenris
|
||||||
|
*/
|
||||||
|
function stance(str) {
|
||||||
|
let regexp = new RegExp("^translate:(.*)$");
|
||||||
|
let matching = regexp.exec(str);
|
||||||
|
if (matching != null) {
|
||||||
|
return translate.get(matching[1]);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
translate.stance = stance;
|
||||||
|
})(translate = lib_plankton.translate || (lib_plankton.translate = {}));
|
||||||
|
})(lib_plankton || (lib_plankton = {}));
|
||||||
|
/*
|
||||||
This file is part of »bacterio-plankton:database«.
|
This file is part of »bacterio-plankton:database«.
|
||||||
|
|
||||||
Copyright 2016-2023 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
|
Copyright 2016-2023 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
|
||||||
|
|
|
@ -10,6 +10,7 @@ modules="${modules} file"
|
||||||
modules="${modules} json"
|
modules="${modules} json"
|
||||||
modules="${modules} base64"
|
modules="${modules} base64"
|
||||||
modules="${modules} string"
|
modules="${modules} string"
|
||||||
|
modules="${modules} translate"
|
||||||
modules="${modules} storage"
|
modules="${modules} storage"
|
||||||
modules="${modules} zoo-input"
|
modules="${modules} zoo-input"
|
||||||
modules="${modules} zoo-form"
|
modules="${modules} zoo-form"
|
||||||
|
|
Loading…
Add table
Reference in a new issue