Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
NoobKilla5412 committed Jan 24, 2024
1 parent 7aa4944 commit 00baeeb
Showing 1 changed file with 126 additions and 129 deletions.
255 changes: 126 additions & 129 deletions src/Json2Ts.ts
Original file line number Diff line number Diff line change
@@ -1,160 +1,157 @@
import * as _ from "underscore";

export class Json2Ts {
convert(content: string): string {
let jsonContent = JSON.parse(content);
convert(content: string): string {
let jsonContent = JSON.parse(content);

if (_.isArray(jsonContent)) {
return this.convertObjectToTsInterfaces(jsonContent[0]);
}

return this.convertObjectToTsInterfaces(jsonContent);
if (_.isArray(jsonContent)) {
return this.convertObjectToTsInterfaces(jsonContent[0]);
}

private convertObjectToTsInterfaces(jsonContent: any, objectName: string = "RootObject"): string {
let optionalKeys: string[] = [];
let objectResult: string[] = [];

for (let key in jsonContent) {
let value = jsonContent[key];

if (_.isObject(value) && !_.isArray(value)) {
let childObjectName = this.toUpperFirstLetter(key);
objectResult.push(this.convertObjectToTsInterfaces(value, childObjectName));
jsonContent[key] = this.removeMajority(childObjectName) + ";";
} else if (_.isArray(value)) {
let arrayTypes: any = this.detectMultiArrayTypes(value);

if (this.isMultiArray(arrayTypes)) {
let multiArrayBrackets = this.getMultiArrayBrackets(value);

if (this.isAllEqual(arrayTypes)) {
jsonContent[key] = arrayTypes[0].replace("[]", multiArrayBrackets);
} else {
jsonContent[key] = "any" + multiArrayBrackets + ";";
}
} else if (value.length > 0 && _.isObject(value[0])) {
let childObjectName = this.toUpperFirstLetter(key);
objectResult.push(this.convertObjectToTsInterfaces(value[0], childObjectName));
jsonContent[key] = this.removeMajority(childObjectName) + "[];";
} else {
jsonContent[key] = arrayTypes[0];
}

} else if (_.isDate(value)) {
jsonContent[key] = "Date;";
} else if (_.isString(value)) {
jsonContent[key] = "string;";
} else if (_.isBoolean(value)) {
jsonContent[key] = "boolean;";
} else if (_.isNumber(value)) {
jsonContent[key] = "number;";
} else {
jsonContent[key] = "any;";
optionalKeys.push(key);
}
return this.convertObjectToTsInterfaces(jsonContent);
}

private convertObjectToTsInterfaces(jsonContent: any, objectName: string = "RootObject"): string {
let optionalKeys: string[] = [];
let objectResult: string[] = [];

for (let key in jsonContent) {
let value = jsonContent[key];

if (_.isObject(value) && !_.isArray(value)) {
let childObjectName = this.toUpperFirstLetter(key);
objectResult.push(this.convertObjectToTsInterfaces(value, childObjectName));
jsonContent[key] = this.removeMajority(childObjectName) + ";";
} else if (_.isArray(value)) {
let arrayTypes: any = this.detectMultiArrayTypes(value);

if (this.isMultiArray(arrayTypes)) {
let multiArrayBrackets = this.getMultiArrayBrackets(value);

if (this.isAllEqual(arrayTypes)) {
jsonContent[key] = arrayTypes[0].replace("[]", multiArrayBrackets);
} else {
jsonContent[key] = "any" + multiArrayBrackets + ";";
}
} else if (value.length > 0 && _.isObject(value[0])) {
let childObjectName = this.toUpperFirstLetter(key);
objectResult.push(this.convertObjectToTsInterfaces(value[0], childObjectName));
jsonContent[key] = this.removeMajority(childObjectName) + "[];";
} else {
jsonContent[key] = arrayTypes[0];
}
} else if (_.isDate(value)) {
jsonContent[key] = "Date;";
} else if (_.isString(value)) {
jsonContent[key] = "string;";
} else if (_.isBoolean(value)) {
jsonContent[key] = "boolean;";
} else if (_.isNumber(value)) {
jsonContent[key] = "number;";
} else {
jsonContent[key] = "any;";
optionalKeys.push(key);
}
}

let result = this.formatCharsToTypeScript(jsonContent, objectName, optionalKeys);
objectResult.push(result);
let result = this.formatCharsToTypeScript(jsonContent, objectName, optionalKeys);
objectResult.push(result);

return objectResult.join("\n\n");
}
return objectResult.join("\n\n");
}

private detectMultiArrayTypes(value: any, valueType: string[] = []): string[] {
if (_.isArray(value)) {
if (value.length === 0) {
valueType.push("any[];");
} else if (_.isArray(value[0])) {
for (let index = 0, length = value.length; index < length; index++) {
let element = value[index];

let valueTypeResult = this.detectMultiArrayTypes(element, valueType);
valueType.concat(valueTypeResult);
}
} else if (_.all(value, _.isString)) {
valueType.push("string[];");
} else if (_.all(value, _.isNumber)) {
valueType.push("number[];");
} else if (_.all(value, _.isBoolean)) {
valueType.push("boolean[];");
} else {
valueType.push("any[];");
}
}
private detectMultiArrayTypes(value: any, valueType: string[] = []): string[] {
if (_.isArray(value)) {
if (value.length === 0) {
valueType.push("any[];");
} else if (_.isArray(value[0])) {
for (let index = 0, length = value.length; index < length; index++) {
let element = value[index];

return valueType;
let valueTypeResult = this.detectMultiArrayTypes(element, valueType);
valueType.concat(valueTypeResult);
}
} else if (_.all(value, _.isString)) {
valueType.push("string[];");
} else if (_.all(value, _.isNumber)) {
valueType.push("number[];");
} else if (_.all(value, _.isBoolean)) {
valueType.push("boolean[];");
} else {
valueType.push("any[];");
}
}

private isMultiArray(arrayTypes: string[]) {
return arrayTypes.length > 1;
}
return valueType;
}

private isAllEqual(array: string[]) {
return _.all(array.slice(1), _.partial(_.isEqual, array[0]));
}
private isMultiArray(arrayTypes: string[]) {
return arrayTypes.length > 1;
}

private getMultiArrayBrackets(content: string): string {
let jsonString = JSON.stringify(content);
let brackets = "";
private isAllEqual(array: string[]) {
return _.all(array.slice(1), _.partial(_.isEqual, array[0]));
}

for (let index = 0, length = jsonString.length; index < length; index++) {
let element = jsonString[index];
private getMultiArrayBrackets(content: string): string {
let jsonString = JSON.stringify(content);
let brackets = "";

if (element === "[") {
brackets = brackets + "[]";
} else {
index = length;
}
}
for (let index = 0, length = jsonString.length; index < length; index++) {
let element = jsonString[index];

return brackets;
if (element === "[") {
brackets = brackets + "[]";
} else {
index = length;
}
}

private formatCharsToTypeScript(jsonContent: any, objectName: string, optionalKeys: string[]): string {
let result = JSON.stringify(jsonContent, null, "\t")
.replace(new RegExp("\"", "g"), "")
.replace(new RegExp(",", "g"), "");

let allKeys = _.allKeys(jsonContent);
for (let index = 0, length = allKeys.length; index < length; index++) {
let key = allKeys[index];
if (_.contains(optionalKeys, key)) {
result = result.replace(new RegExp(key + ":", "g"), this.toLowerFirstLetter(key) + "?:");
} else {
result = result.replace(new RegExp(key + ":", "g"), this.toLowerFirstLetter(key) + ":");
}
}
return brackets;
}

objectName = this.removeMajority(objectName);
private formatCharsToTypeScript(jsonContent: any, objectName: string, optionalKeys: string[]): string {
let result = JSON.stringify(jsonContent, null, "\t").replace(new RegExp('"', "g"), "").replace(new RegExp(",", "g"), "");

return "export interface " + objectName + " " + result;
let allKeys = _.allKeys(jsonContent);
for (let index = 0, length = allKeys.length; index < length; index++) {
let key = allKeys[index];
if (_.contains(optionalKeys, key)) {
result = result.replace(new RegExp(key + ":", "g"), key + "?:");
} else {
result = result.replace(new RegExp(key + ":", "g"), key + ":");
}
}

private removeMajority(objectName: string): string {
if (_.last(objectName, 3).join("").toUpperCase() === "IES") {
return objectName.substring(0, objectName.length - 3) + "y";
} else if (_.last(objectName).toUpperCase() === "S") {
return objectName.substring(0, objectName.length - 1);
}
objectName = this.removeMajority(objectName);

return "export interface " + objectName + " " + result;
}

return objectName;
private removeMajority(objectName: string): string {
if (_.last(objectName, 3).join("").toUpperCase() === "IES") {
return objectName.substring(0, objectName.length - 3) + "y";
} else if (_.last(objectName).toUpperCase() === "S") {
return objectName.substring(0, objectName.length - 1);
}

private toUpperFirstLetter(text: string) {
return text.charAt(0).toUpperCase() + text.slice(1);
};
return objectName;
}

private toLowerFirstLetter(text: string) {
return text.charAt(0).toLowerCase() + text.slice(1);
};
private toUpperFirstLetter(text: string) {
return text.charAt(0).toUpperCase() + text.slice(1);
}

private toLowerFirstLetter(text: string) {
return text.charAt(0).toLowerCase() + text.slice(1);
}
}

export function isJson(stringContent) {
try {
JSON.parse(stringContent);
} catch (e) {
return false;
}
return true;
}
try {
JSON.parse(stringContent);
} catch (e) {
return false;
}
return true;
}

0 comments on commit 00baeeb

Please sign in to comment.