initial files
This commit is contained in:
72
public/helpers/comparisonHelpers.js
Normal file
72
public/helpers/comparisonHelpers.js
Normal file
@@ -0,0 +1,72 @@
|
||||
const Handlebars = require('handlebars');
|
||||
|
||||
module.exports = {
|
||||
toJSON: function(object) {
|
||||
if(typeof object === 'object') {
|
||||
return JSON.stringify(object);
|
||||
} else {
|
||||
throw 'no object type';
|
||||
}
|
||||
},
|
||||
isObject: function(value, options) {
|
||||
return typeof value === 'object' && value !== null && !Array.isArray(value) ? options.fn(this) : options.inverse(this);
|
||||
},
|
||||
isArray: function(value, options) {
|
||||
return Array.isArray(value) ? options.fn(this) : options.inverse(this);
|
||||
},
|
||||
isRGB: function(value) {
|
||||
return Array.isArray(value) && (value.length === 3 || value.length === 4);
|
||||
},
|
||||
rgbString: function(value) {
|
||||
if (Array.isArray(value)) {
|
||||
return value.length === 3
|
||||
? `rgb(${value.join(',')})`
|
||||
: value.length === 4
|
||||
? `rgba(${value.join(',')})`
|
||||
: '';
|
||||
}
|
||||
return '';
|
||||
},
|
||||
toArray: function(value) {
|
||||
if (Array.isArray(value)) return value;
|
||||
if (typeof value === "string") return [value];
|
||||
return [];
|
||||
},
|
||||
replaceAll: function(string, pattern, replacement) {
|
||||
return new Handlebars.SafeString(string.replaceAll(pattern, replacement) || '');
|
||||
},
|
||||
equaler: function(v1, operator, v2, options) {
|
||||
switch (operator) {
|
||||
case '==':
|
||||
return (v1 == v2) ? options.fn(this) : options.inverse(this);
|
||||
case '===':
|
||||
return (v1 === v2) ? options.fn(this) : options.inverse(this);
|
||||
case '!=':
|
||||
return (v1 != v2) ? options.fn(this) : options.inverse(this);
|
||||
case '!==':
|
||||
return (v1 !== v2) ? options.fn(this) : options.inverse(this);
|
||||
case '<':
|
||||
return (v1 < v2) ? options.fn(this) : options.inverse(this);
|
||||
case '<=':
|
||||
return (v1 <= v2) ? options.fn(this) : options.inverse(this);
|
||||
case '>':
|
||||
return (v1 > v2) ? options.fn(this) : options.inverse(this);
|
||||
case '>=':
|
||||
return (v1 >= v2) ? options.fn(this) : options.inverse(this);
|
||||
case '&&':
|
||||
return (v1 && v2) ? options.fn(this) : options.inverse(this);
|
||||
case '||':
|
||||
return (v1 || v2) ? options.fn(this) : options.inverse(this);
|
||||
case 'typeof':
|
||||
return (typeof v1 === v2) ? options.fn(this) : options.inverse(this);
|
||||
case 'like':
|
||||
return (v1.indexOf(v2)) > -1 ? options.fn(this) : options.inverse(this);
|
||||
case 'includes':
|
||||
return (v1.includes(v2)) <= 0 ? options.inverse(this) : options.fn(this);
|
||||
case '%':
|
||||
return (v1 % v2) == 0 ? options.fn(this) : options.inverse(this);
|
||||
default:
|
||||
return options.inverse(this);
|
||||
}
|
||||
},
|
||||
};
|
||||
6
public/helpers/dateHelpers.js
Normal file
6
public/helpers/dateHelpers.js
Normal file
@@ -0,0 +1,6 @@
|
||||
// const moment = require('moment'); // npm install moment
|
||||
// moment.locale('de');
|
||||
|
||||
module.exports = {
|
||||
dateFormat: (date, format) => dateFormat(date, format)
|
||||
};
|
||||
24
public/helpers/fileHelper.js
Normal file
24
public/helpers/fileHelper.js
Normal file
@@ -0,0 +1,24 @@
|
||||
module.exports = {
|
||||
// objectTree: function(context, options) {
|
||||
// let ret = '';
|
||||
|
||||
// if (context === null || context === undefined) return ret;
|
||||
|
||||
// if (Array.isArray(context)) {
|
||||
// context.forEach((item, index) => {
|
||||
// ret += options.fn({ key: index, value: item });
|
||||
// });
|
||||
// } else if (typeof context === 'object') {
|
||||
// for (const key in context) {
|
||||
// if (context.hasOwnProperty(key)) {
|
||||
// ret += options.fn({ key, value: context[key] });
|
||||
// }
|
||||
// }
|
||||
// } else {
|
||||
// // primitive Werte
|
||||
// ret += options.fn({ key: null, value: context });
|
||||
// }
|
||||
|
||||
// return ret;
|
||||
// }
|
||||
}
|
||||
145
public/helpers/iterationHelpers.js
Normal file
145
public/helpers/iterationHelpers.js
Normal file
@@ -0,0 +1,145 @@
|
||||
module.exports = {
|
||||
objectTree: function(obj) {
|
||||
function traverse(o) {
|
||||
if (o === null || typeof o !== 'object') return o;
|
||||
|
||||
if (Array.isArray(o)) {
|
||||
// RGB oder RGBA-Erkennung: Array aus 3 oder 4 Zahlen
|
||||
if ((o.length === 3 || o.length === 4) && o.every(v => typeof v === 'number')) {
|
||||
// const rgbString = o.length === 3
|
||||
// ? `rgb(${o.join(',')})`
|
||||
// : `rgba(${o.join(',')})`;
|
||||
const rgbString = o.join(',');
|
||||
return { key: null, value: rgbString, children: null, isRGB: true };
|
||||
}
|
||||
|
||||
// normales Array
|
||||
return o.map((v, i) => ({
|
||||
key: i,
|
||||
value: typeof v !== 'object' ? v : null,
|
||||
children: typeof v === 'object' ? traverse(v) : null
|
||||
}));
|
||||
}
|
||||
|
||||
const result = [];
|
||||
for (const key in o) {
|
||||
if (o.hasOwnProperty(key)) {
|
||||
const val = o[key];
|
||||
const children = (val !== null && typeof val === 'object') ? traverse(val) : null;
|
||||
result.push({
|
||||
key,
|
||||
value: children ? null : val,
|
||||
children
|
||||
});
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
return traverse(obj);
|
||||
},
|
||||
jsonEntriesRecursive: function(obj, options) {
|
||||
const entries = [];
|
||||
|
||||
function traverse(prefix, data) {
|
||||
if (Array.isArray(data)) {
|
||||
data.forEach((item, index) => {
|
||||
const arrayKey = `${prefix}[${index}]`;
|
||||
if (typeof item === "object" && item !== null) {
|
||||
traverse(arrayKey, item); // Rekursion für Array-Objekte
|
||||
} else {
|
||||
entries.push({ key: arrayKey, value: item });
|
||||
}
|
||||
});
|
||||
} else if (typeof data === "object" && data !== null) {
|
||||
for (const key in data) {
|
||||
if (!Object.prototype.hasOwnProperty.call(data, key)) continue;
|
||||
|
||||
const value = data[key];
|
||||
const fullKey = prefix ? `${prefix}.${key}` : key;
|
||||
|
||||
if (typeof value === "object" && value !== null) {
|
||||
traverse(fullKey, value); // Rekursion für verschachtelte Objekte
|
||||
} else {
|
||||
entries.push({ key: fullKey, value });
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Primitive Werte am obersten Level
|
||||
entries.push({ key: prefix, value: data });
|
||||
}
|
||||
|
||||
console.log({ key: prefix, value: data })
|
||||
}
|
||||
|
||||
traverse("", obj);
|
||||
|
||||
// Handlebars-Block Helper
|
||||
if (options.fn) {
|
||||
return options.fn(entries);
|
||||
}
|
||||
|
||||
return entries;
|
||||
},
|
||||
groupBy: function(items, keys, options) {
|
||||
// keys kann String oder Array sein
|
||||
if (!Array.isArray(keys)) keys = [keys];
|
||||
if (!keys.length) return '';
|
||||
|
||||
const key = keys[0]; // aktueller Gruppierungsschlüssel
|
||||
const remainingKeys = keys.slice(1); // restliche Schlüssel
|
||||
|
||||
// Gruppieren nach aktuellem Schlüssel
|
||||
const groups = {};
|
||||
items.forEach(item => {
|
||||
let groupKeys = item[key];
|
||||
if (!Array.isArray(groupKeys)) groupKeys = [groupKeys];
|
||||
|
||||
groupKeys.forEach(groupKey => {
|
||||
if (!groups[groupKey]) groups[groupKey] = [];
|
||||
groups[groupKey].push(item);
|
||||
});
|
||||
});
|
||||
|
||||
// Ergebnis zusammensetzen
|
||||
let result = '';
|
||||
for (const group in groups) {
|
||||
const groupItems = groups[group];
|
||||
|
||||
if (remainingKeys.length > 0) {
|
||||
// Rekursive Verschachtelung
|
||||
// Wir rufen die gleiche Helper-Funktion intern auf
|
||||
const nestedResult = Handlebars.helpers.groupBy(groupItems, remainingKeys, options);
|
||||
result += options.fn({ key: group, items: nestedResult });
|
||||
} else {
|
||||
// Basisfall: keine weiteren Keys, items direkt weitergeben
|
||||
result += options.fn({ key: group, items: groupItems });
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
},
|
||||
parseArray: function(str){
|
||||
try {
|
||||
return JSON.parse(str);
|
||||
} catch (e) {
|
||||
return [];
|
||||
}
|
||||
},
|
||||
ifSingle: function(array, options) {
|
||||
if (Array.isArray(array) && array.length === 1) {
|
||||
return options.fn(array[0]); // Item direkt übergeben
|
||||
}
|
||||
return options.inverse(this); // else-Block
|
||||
},
|
||||
findValue: function(array, searchKey, searchValue, returnKey) {
|
||||
if (!Array.isArray(array)) return "";
|
||||
for (let i = 0; i < array.length; i++) {
|
||||
const item = array[i];
|
||||
|
||||
if (item && item[searchKey] === searchValue) {
|
||||
return item[returnKey] ?? "";
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user