Martin Karkowski
d911380511
- Fixes: - helpers.jsonSchemaMethods: -> renamed flatten to nested. - Added: - helpers.descriptors: -> parseFunctionToJsonSchema - helpers.jsonSchemaMethods: -> added `flattenSchema` and `reduceSchema`. This Function will create a nested JSON-Schema.
92 lines
2.6 KiB
TypeScript
92 lines
2.6 KiB
TypeScript
/**
|
|
* @author Martin Karkowski
|
|
* @email m.karkowski@zema.de
|
|
* @desc [description]
|
|
*/
|
|
|
|
/**
|
|
* Function to stringify an Object. This Function will stringify Functions as well.
|
|
* @param obj The Object.
|
|
*/
|
|
export function stringifyWithFunctions(obj, ...args) {
|
|
return JSON.stringify(
|
|
obj,
|
|
(key, value) => {
|
|
if (typeof value === "function") {
|
|
let str: string = value.toString();
|
|
|
|
// Todo Parse Arrow-Functions Correctly!
|
|
// Details here: https://zendev.com/2018/10/01/javascript-arrow-functions-how-why-when.html
|
|
// Difference Cases For:
|
|
// 1) (a, b) => a + b;
|
|
// 2) array => array[0];
|
|
// 3) (a, b) => (a + b);
|
|
// 4) (name, description) => ({name: name, description: description})
|
|
// ....
|
|
|
|
if (!str.startsWith("function") && !str.startsWith("(")) {
|
|
const name = str.slice(0, str.indexOf("=>"));
|
|
const func = str.slice(str.indexOf("=>(") + 3, str.length - 2);
|
|
const adaptedFunc = "function(" + name + "){ return " + func + "; }";
|
|
str = adaptedFunc;
|
|
}
|
|
|
|
return "/Function(" + str + ")/";
|
|
}
|
|
return value;
|
|
},
|
|
...args
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Function to parse a JSON String, in which methods should be available.
|
|
* @param json A String containing the json Object
|
|
* @param scope An Scope to use during parsing.
|
|
* @returns
|
|
*/
|
|
export function parseWithFunctions(
|
|
json: string,
|
|
scope: { [index: string]: any } = {}
|
|
) {
|
|
return JSON.parse(json, (key, value) => {
|
|
if (
|
|
typeof value === "string" &&
|
|
value.startsWith("/Function(") &&
|
|
value.endsWith(")/")
|
|
) {
|
|
const _value = value.substring(10, value.length - 2);
|
|
try {
|
|
return eval("(" + _value + ")").bind(scope);
|
|
} catch (e) {
|
|
console.log("FAILED PARSING", value, _value);
|
|
}
|
|
}
|
|
return value;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Function to stringify an Object. This Function is able to stringify Functions as well. Use the Flag withFunctions
|
|
* @param obj The Object.
|
|
* @param withFunctions Flag to Turn on / off the parsing of functions
|
|
*/
|
|
export function stringify(obj: any, withFunctions = false): string {
|
|
if (withFunctions) {
|
|
return stringifyWithFunctions(obj);
|
|
}
|
|
return JSON.stringify(obj);
|
|
}
|
|
|
|
/**
|
|
* Function to parse a JSON String. This Function is able to parse Functions as well. Use the Flag withFunctions
|
|
* @param json A String containing the json Object
|
|
* @param withFunctions Flag to Turn on / off the parsing of functions
|
|
*/
|
|
export function parse(json: string, withFunctions = false): any {
|
|
if (withFunctions) {
|
|
return parseWithFunctions(json);
|
|
}
|
|
return JSON.parse(json);
|
|
}
|