diff --git a/src/v1/ExportToJson.ts b/src/v1/ExportToJson.ts index 45546b71..a6c8c3d2 100644 --- a/src/v1/ExportToJson.ts +++ b/src/v1/ExportToJson.ts @@ -1,59 +1,26 @@ -import { workerMethods } from "./app.worker"; +import {workerMethods} from "./app.worker"; +import {mapToGenericObject} from "./utils/ExportedValueMappers"; export function exportToJson(useHex: boolean = false) { - var indentLen = 2; - var result = ""; - - function expToNative(value: IExportedValue, padLvl: number = 0) { - var pad = " ".repeat((padLvl + 0) * indentLen); - var childPad = " ".repeat((padLvl + 1) * indentLen); - - var isArray = value.type === ObjectType.Array; + const overrideDefaultNumbersWithHex = (key: string, value: any) => { + const isPropertyNumber = typeof value === 'number' + return isPropertyNumber + ? '0x' + value.toString(16) + : value; + } - if (value.type === ObjectType.Object || isArray) { - result += isArray ? "[" : "{"; + const getResultOrThrowError = (response: IWorkerResponse) => { + if (response.error) throw response.error; + return response.result; + } - var keys: any[] = isArray ? value.arrayItems : Object.keys(value.object.fields); - if (keys.length > 0) { - result += `\n${childPad}`; - let nextItemPrefix = ""; - keys.forEach((arrItem, i) => { - const propValue: IExportedValue = isArray ? arrItem : value.object.fields[arrItem]; - if (propValue.type === ObjectType.Undefined) return true; - result += nextItemPrefix + (isArray ? "" : `"${arrItem}": `); - expToNative(propValue, padLvl + 1); - var lineCont = isArray && arrItem.type === ObjectType.Primitive && typeof arrItem.primitiveValue !== "string" && i % 16 !== 15; - var last = i === keys.length - 1; - nextItemPrefix = "," + (lineCont ? " " : `\n${childPad}`); - }); - result += `\n${pad}`; - } - result += isArray ? "]" : "}"; - } else if (value.type === ObjectType.TypedArray) { - if (value.bytes.length <= 64) - result += "[" + Array.from(value.bytes).join(", ") + "]"; - else - result += `{ "$start": ${value.ioOffset + value.start}, "$end": ${value.ioOffset + value.end - 1} }`; - } else if (value.type === ObjectType.Primitive) { - if (value.enumStringValue) - result += `{ "name": ${JSON.stringify(value.enumStringValue)}, "value": ${value.primitiveValue} }`; - else if (typeof value.primitiveValue === "number") - result += useHex ? `0x${value.primitiveValue.toString(16)}` : `${value.primitiveValue}`; - else - result += `${JSON.stringify(value.primitiveValue)}`; - } + const mapResultToJson = (objectToExport: IExportedValue) => { + const genericObject = mapToGenericObject(objectToExport); + const hexReplacer = useHex ? overrideDefaultNumbersWithHex : null; + return JSON.stringify(genericObject, hexReplacer, 2); } return workerMethods.reparse(true) - .then(response => { - if (response.error) { - throw response.error; - } - return response.result; - }) - .then(exportedRoot => { - console.log("exported", exportedRoot); - expToNative(exportedRoot); - return result; - }); + .then(getResultOrThrowError) + .then(mapResultToJson); } diff --git a/src/v1/utils/ExportedValueMappers.ts b/src/v1/utils/ExportedValueMappers.ts new file mode 100644 index 00000000..fa0c9b70 --- /dev/null +++ b/src/v1/utils/ExportedValueMappers.ts @@ -0,0 +1,5 @@ +import {ExportedValueToGenericObjectMapper} from "./ExportedValueMappers/ExportedValueToGenericObjectMapper"; + +export const mapToGenericObject = (objectToMap: IExportedValue): any => { + return new ExportedValueToGenericObjectMapper().map(objectToMap); +} \ No newline at end of file diff --git a/src/v1/utils/ExportedValueMappers/AbstractExportedValueMapper.ts b/src/v1/utils/ExportedValueMappers/AbstractExportedValueMapper.ts new file mode 100644 index 00000000..4e607066 --- /dev/null +++ b/src/v1/utils/ExportedValueMappers/AbstractExportedValueMapper.ts @@ -0,0 +1,46 @@ +import {IExportedValueMapper} from "./IExportedValueMapper"; + +export class AbstractExportedValueMapper implements IExportedValueMapper { + public map(value: IExportedValue): T { + switch (value.type) { + case ObjectType.Primitive: + const isEnumValue = !!value.enumStringValue; + return isEnumValue + ? this.visitEnum(value) + : this.visitPrimitive(value) + case ObjectType.Array: + return this.visitArray(value); + case ObjectType.TypedArray: + return this.visitTypedArray(value); + case ObjectType.Object: + return this.visitObject(value); + case ObjectType.Undefined: + return this.visitUndefined(value); + } + } + + protected visitPrimitive(value: IExportedValue): T { + return null; + } + + protected visitArray(value: IExportedValue): T { + return null; + } + + protected visitTypedArray(value: IExportedValue): T { + return null; + } + + protected visitObject(value: IExportedValue): T { + return null; + } + + protected visitEnum(value: IExportedValue): T { + return null; + } + + protected visitUndefined(value: IExportedValue): T { + return null; + } + +} \ No newline at end of file diff --git a/src/v1/utils/ExportedValueMappers/ExportedValueToGenericObjectMapper.ts b/src/v1/utils/ExportedValueMappers/ExportedValueToGenericObjectMapper.ts new file mode 100644 index 00000000..332e4207 --- /dev/null +++ b/src/v1/utils/ExportedValueMappers/ExportedValueToGenericObjectMapper.ts @@ -0,0 +1,41 @@ +import {AbstractExportedValueMapper} from "./AbstractExportedValueMapper"; + +export class ExportedValueToGenericObjectMapper extends AbstractExportedValueMapper { + + protected visitPrimitive(value: IExportedValue) { + super.visitPrimitive(value); + return value.primitiveValue; + } + + protected visitArray(value: IExportedValue) { + super.visitArray(value); + return ([...value.arrayItems] || []).map((item) => this.map(item)); + } + + protected visitTypedArray(value: IExportedValue) { + super.visitTypedArray(value); + return [...value.bytes] || []; + } + + protected visitObject(value: IExportedValue) { + super.visitObject(value); + const newObject = {}; + Object.keys(value.object.fields || {}).forEach(key => { + newObject[key] = this.map(value.object.fields[key]) + }) + return newObject; + } + + protected visitEnum(value: IExportedValue) { + super.visitEnum(value); + return { + name: value.enumStringValue, + value: value.primitiveValue + } + } + + protected visitUndefined(value: IExportedValue): any { + return undefined; + } + +} \ No newline at end of file diff --git a/src/v1/utils/ExportedValueMappers/IExportedValueMapper.ts b/src/v1/utils/ExportedValueMappers/IExportedValueMapper.ts new file mode 100644 index 00000000..e53afd99 --- /dev/null +++ b/src/v1/utils/ExportedValueMappers/IExportedValueMapper.ts @@ -0,0 +1,3 @@ +export interface IExportedValueMapper { + map(value: IExportedValue): T; +} \ No newline at end of file