nope/lib/helpers/jsonMethods.ts

98 lines
2.7 KiB
TypeScript
Raw Normal View History

2020-11-06 08:10:30 +00:00
/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @desc [description]
*/
# 1.2.0 - Added: - `lib/cli/nope` adding scan for ui service - `lib/decorators/container`: Main Container, holding all `methods` and `classes`. Use `getCentralDecoratedContainer()` to get this decorator. - `types/nope/nopePackage.interface` added `IClassDescription` which contains the class description used in the Package Description. - `logger/nopeLogger`: added methods: `enabledFor`, `enableFor`, `shouldLog` - `package.json`: installed types of `ace` text editor. - `ui/helpers.browser`: Created `convertInstanceRenderPage` and `IUiDefinition` - `ui/helpers.nodejs`: Added a Helper to write the Ui-File (`writeUiFile`) and parse its arguments (`readInwriteUiFileArgs`) - `ui/index.*`: Crated the corresponding exports. - Modified: - `lib/decorators/*` Adding the main `container` where every function, service method etc is added. All decorators now safe the decorated elements in the container. - `helpers/json`: Adding `BEGIN_STR` and `END_STR` for parsing functions as constants. - `logger/eventLogging`: simplify `useEventLogger` - `logger/index.browser`: Adating exports. - `loader/loadPackages`: Modifing `IPackageConfig` now extends Partial the `IPackageDescription` - `types/ui/editor/IEditPage`: adapting Type of `getData` to `T`->`any`. Adapting the return of `getPorts` (The Ports will be generated in the ui then) - `types/ui/editor/helpers.interface`: Adapting the `w2ui` and added `w2uiHelpers` and added `ace`. Rearanging `IRenderData` element. to compact the data. - `types/ui/editor/render.callbacks`: Rearange the Generic Type of `TRenderInstancePage` and Renaming `TCreatorPage` to `TInstanceManagerPage`. Adapting the `option` of `TInstanceManagerPage` regarding the `createInstance` and `instances` - `types/ui/editor/index`: Adapting the Exports. - `lib/index.browser`: Exporting `ui` elements - `lib/index.nodejs`: Exporting `ui` elements - `lib/types/index`: Exporting `ui` elements - Fixes: - `types/nope/nopeInstanceManager.interface`: Fixing Type of createInstance. Now the Type `I` extends `INopeModule` instead of being set to `IGenericNopeModule`
2022-07-02 09:30:10 +00:00
const BEGIN_STR = "____Function_beginn/(";
const END_STR = ")/__Function_end";
/**
* 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;
}
# 1.2.0 - Added: - `lib/cli/nope` adding scan for ui service - `lib/decorators/container`: Main Container, holding all `methods` and `classes`. Use `getCentralDecoratedContainer()` to get this decorator. - `types/nope/nopePackage.interface` added `IClassDescription` which contains the class description used in the Package Description. - `logger/nopeLogger`: added methods: `enabledFor`, `enableFor`, `shouldLog` - `package.json`: installed types of `ace` text editor. - `ui/helpers.browser`: Created `convertInstanceRenderPage` and `IUiDefinition` - `ui/helpers.nodejs`: Added a Helper to write the Ui-File (`writeUiFile`) and parse its arguments (`readInwriteUiFileArgs`) - `ui/index.*`: Crated the corresponding exports. - Modified: - `lib/decorators/*` Adding the main `container` where every function, service method etc is added. All decorators now safe the decorated elements in the container. - `helpers/json`: Adding `BEGIN_STR` and `END_STR` for parsing functions as constants. - `logger/eventLogging`: simplify `useEventLogger` - `logger/index.browser`: Adating exports. - `loader/loadPackages`: Modifing `IPackageConfig` now extends Partial the `IPackageDescription` - `types/ui/editor/IEditPage`: adapting Type of `getData` to `T`->`any`. Adapting the return of `getPorts` (The Ports will be generated in the ui then) - `types/ui/editor/helpers.interface`: Adapting the `w2ui` and added `w2uiHelpers` and added `ace`. Rearanging `IRenderData` element. to compact the data. - `types/ui/editor/render.callbacks`: Rearange the Generic Type of `TRenderInstancePage` and Renaming `TCreatorPage` to `TInstanceManagerPage`. Adapting the `option` of `TInstanceManagerPage` regarding the `createInstance` and `instances` - `types/ui/editor/index`: Adapting the Exports. - `lib/index.browser`: Exporting `ui` elements - `lib/index.nodejs`: Exporting `ui` elements - `lib/types/index`: Exporting `ui` elements - Fixes: - `types/nope/nopeInstanceManager.interface`: Fixing Type of createInstance. Now the Type `I` extends `INopeModule` instead of being set to `IGenericNopeModule`
2022-07-02 09:30:10 +00:00
return BEGIN_STR + str + END_STR;
}
return value;
},
...args
);
}
/**
* Function to parse a JSON String, in which methods should be available.
* @param json A String containing the json Object
2022-01-26 12:03:03 +00:00
* @param scope An Scope to use during parsing.
* @returns
*/
2022-01-26 12:03:03 +00:00
export function parseWithFunctions(
json: string,
scope: { [index: string]: any } = {}
) {
return JSON.parse(json, (key, value) => {
2021-12-04 07:25:26 +00:00
if (
typeof value === "string" &&
# 1.2.0 - Added: - `lib/cli/nope` adding scan for ui service - `lib/decorators/container`: Main Container, holding all `methods` and `classes`. Use `getCentralDecoratedContainer()` to get this decorator. - `types/nope/nopePackage.interface` added `IClassDescription` which contains the class description used in the Package Description. - `logger/nopeLogger`: added methods: `enabledFor`, `enableFor`, `shouldLog` - `package.json`: installed types of `ace` text editor. - `ui/helpers.browser`: Created `convertInstanceRenderPage` and `IUiDefinition` - `ui/helpers.nodejs`: Added a Helper to write the Ui-File (`writeUiFile`) and parse its arguments (`readInwriteUiFileArgs`) - `ui/index.*`: Crated the corresponding exports. - Modified: - `lib/decorators/*` Adding the main `container` where every function, service method etc is added. All decorators now safe the decorated elements in the container. - `helpers/json`: Adding `BEGIN_STR` and `END_STR` for parsing functions as constants. - `logger/eventLogging`: simplify `useEventLogger` - `logger/index.browser`: Adating exports. - `loader/loadPackages`: Modifing `IPackageConfig` now extends Partial the `IPackageDescription` - `types/ui/editor/IEditPage`: adapting Type of `getData` to `T`->`any`. Adapting the return of `getPorts` (The Ports will be generated in the ui then) - `types/ui/editor/helpers.interface`: Adapting the `w2ui` and added `w2uiHelpers` and added `ace`. Rearanging `IRenderData` element. to compact the data. - `types/ui/editor/render.callbacks`: Rearange the Generic Type of `TRenderInstancePage` and Renaming `TCreatorPage` to `TInstanceManagerPage`. Adapting the `option` of `TInstanceManagerPage` regarding the `createInstance` and `instances` - `types/ui/editor/index`: Adapting the Exports. - `lib/index.browser`: Exporting `ui` elements - `lib/index.nodejs`: Exporting `ui` elements - `lib/types/index`: Exporting `ui` elements - Fixes: - `types/nope/nopeInstanceManager.interface`: Fixing Type of createInstance. Now the Type `I` extends `INopeModule` instead of being set to `IGenericNopeModule`
2022-07-02 09:30:10 +00:00
value.startsWith(BEGIN_STR) &&
value.endsWith(END_STR)
2021-12-04 07:25:26 +00:00
) {
# 1.2.0 - Added: - `lib/cli/nope` adding scan for ui service - `lib/decorators/container`: Main Container, holding all `methods` and `classes`. Use `getCentralDecoratedContainer()` to get this decorator. - `types/nope/nopePackage.interface` added `IClassDescription` which contains the class description used in the Package Description. - `logger/nopeLogger`: added methods: `enabledFor`, `enableFor`, `shouldLog` - `package.json`: installed types of `ace` text editor. - `ui/helpers.browser`: Created `convertInstanceRenderPage` and `IUiDefinition` - `ui/helpers.nodejs`: Added a Helper to write the Ui-File (`writeUiFile`) and parse its arguments (`readInwriteUiFileArgs`) - `ui/index.*`: Crated the corresponding exports. - Modified: - `lib/decorators/*` Adding the main `container` where every function, service method etc is added. All decorators now safe the decorated elements in the container. - `helpers/json`: Adding `BEGIN_STR` and `END_STR` for parsing functions as constants. - `logger/eventLogging`: simplify `useEventLogger` - `logger/index.browser`: Adating exports. - `loader/loadPackages`: Modifing `IPackageConfig` now extends Partial the `IPackageDescription` - `types/ui/editor/IEditPage`: adapting Type of `getData` to `T`->`any`. Adapting the return of `getPorts` (The Ports will be generated in the ui then) - `types/ui/editor/helpers.interface`: Adapting the `w2ui` and added `w2uiHelpers` and added `ace`. Rearanging `IRenderData` element. to compact the data. - `types/ui/editor/render.callbacks`: Rearange the Generic Type of `TRenderInstancePage` and Renaming `TCreatorPage` to `TInstanceManagerPage`. Adapting the `option` of `TInstanceManagerPage` regarding the `createInstance` and `instances` - `types/ui/editor/index`: Adapting the Exports. - `lib/index.browser`: Exporting `ui` elements - `lib/index.nodejs`: Exporting `ui` elements - `lib/types/index`: Exporting `ui` elements - Fixes: - `types/nope/nopeInstanceManager.interface`: Fixing Type of createInstance. Now the Type `I` extends `INopeModule` instead of being set to `IGenericNopeModule`
2022-07-02 09:30:10 +00:00
const _value = value.substring(
BEGIN_STR.length,
value.length - END_STR.length
);
try {
2022-01-26 12:03:03 +00:00
return eval("(" + _value + ")").bind(scope);
} catch (e) {
2021-12-04 07:25:26 +00:00
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
2021-12-04 07:25:26 +00:00
* @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);
2021-12-04 07:25:26 +00:00
}