Friendiiverse/Source/Utils.js

79 lines
2.1 KiB
JavaScript
Raw Normal View History

function ForceList(Item) {
return (Array.isArray(Item) ? Item : [Item]);
};
function RndId() {
return `${Date.now()}${Math.random()}`;
};
function LogDebug(Data, Status) {
if (Debug) {
if (!Status) {
Status = 'l';
};
for (var i=0; i<Data.length; i++) {
try {
Data[i] = JSON.parse(Data[i]);
} catch(_){};
};
console[{l: "log", e: "error"}[Status.toLowerCase()]](Data);
};
};
// Transform JSON tree into a new using a template schema
2023-04-20 00:19:19 +02:00
// DEVNOTE: Unsafe, should check for colliding "__" keys from input tree and act accordingly
function JsonTransformA(TreesOld, SchemaCurr, SchemaRoot) {
if (Array.isArray(TreesOld)) {
var ListNew = [];
ForceList(TreesOld).forEach(function(TreeOld){
ListNew.push(JsonTransformCycleA(TreeOld, SchemaCurr, SchemaRoot));
});
return ListNew;
} else {
return JsonTransformCycleA(TreesOld, SchemaCurr, SchemaRoot);
};
};
function JsonTransformCycleA(TreeOld, SchemaCurr, SchemaRoot) {
var TreeNew = {};
Object.keys(TreeOld).forEach(function(KeyOld){
var Content = TreeOld[KeyOld];
var KeyNew = ((typeof(SchemaCurr) == 'object' && KeyOld in SchemaCurr) ? SchemaCurr[KeyOld] : KeyOld);
if (typeof(Content) == 'object' && Content !== null) {
if (Array.isArray(Content)) {
// Lists
2023-04-20 00:19:19 +02:00
/* var ListNew = [];
Content.forEach(function(Value){
ListNew.push(JsonTransform(Value, KeyNew));
});
2023-04-20 00:19:19 +02:00
TreeNew[KeyNew] = ListNew;*/
} else {
// Dicts
2023-04-20 00:19:19 +02:00
// Strange bug, in this context we can't assign new value to child of the object, we use a variable
NameKeyNew = KeyNew.__;
if (!NameKeyNew) {
NameKeyNew = KeyOld;
};
TreeNew[NameKeyNew] = JsonTransformA(Content, SchemaRoot[NameKeyNew], SchemaRoot);
if (NameKeyNew !== KeyOld) {
TreeNew[SchemaRoot[NameKeyNew].__] = TreeNew[NameKeyNew];
delete TreeNew[NameKeyNew];
};
};
} else {
// Values
TreeNew[KeyNew] = Content;
};
});
return TreeNew;
};
2023-04-20 00:19:19 +02:00
function JsonTransformB(TreesOld, Schema, Node, Source) {
};
function JsonTransformCycleB(TreeOld, Schema, Node, Source) {
var TreeNew = {};
Object.keys(Node).forEach(function(KeyOld){
console.log(KeyOld)
});
return TreeNew;
};