Skip to content

Commit

Permalink
Merge pull request #22 from samchon/v2.0
Browse files Browse the repository at this point in the history
Close #20 and Close #21
  • Loading branch information
samchon authored Apr 21, 2022
2 parents 7740415 + b4d9913 commit d1de90b
Show file tree
Hide file tree
Showing 13 changed files with 445 additions and 387 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ interface ICompany
interface IEmployee
{
name: string;
gendor: string | number | null;
gender: string | number | null;
}
const company: ICompany;

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typescript-json",
"version": "2.0.2",
"version": "2.0.3",
"description": "Faster JSON stringify with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down
49 changes: 49 additions & 0 deletions src/factories/ExpressionFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import ts from "typescript";

export namespace ExpressionFactory
{
export function generate(input: any)
{
if (input instanceof Array)
return generate_array(input);
else if (typeof input === "object")
return generate_object(input);
else if (typeof input === "boolean")
return generate_boolean(input);
else if (typeof input === "string")
return generate_string(input);
else
throw new Error("Unknown type.");
}

function generate_object(obj: object): ts.ObjectLiteralExpression
{
const properties = Object
.entries(obj)
.map(([key, value]) => ts.factory.createPropertyAssignment
(
key,
generate(value)
));
return ts.factory.createObjectLiteralExpression(properties, true);
}

function generate_array(array: any[]): ts.ArrayLiteralExpression
{
return ts.factory.createArrayLiteralExpression
(
array.map(generate),
true
);
}

function generate_boolean(value: boolean): ts.Identifier
{
return ts.factory.createIdentifier(value.toString());
}

function generate_string(value: string): ts.StringLiteral
{
return ts.factory.createStringLiteral(value);
}
}
22 changes: 13 additions & 9 deletions src/factories/FunctionFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import crypto from "crypto";
import path from "path";
import ts from "typescript";

import { JsonFactory } from "../factories/JsonFactory";
import { SchemaFactory } from "../factories/SchemaFactory";
import { IMetadata } from "../structures/IMetadata";
import { IProject } from "../structures/IProject";
import { ISchema } from "../structures/ISchema";
import { ExpressionFactory } from "./ExpressionFactory";
import { MetadataFactory } from "./MetadataFactory";

export namespace FunctionFactory
{
Expand Down Expand Up @@ -61,12 +62,14 @@ export namespace FunctionFactory
type = project.checker.getTypeAtLocation(top);
}

const app: ISchema.IApplication | null = SchemaFactory.generate(project.checker, type);
const tuple: ts.ArrayLiteralExpression = JsonFactory.application(app);
const app: IMetadata.IApplication | null = MetadataFactory.generate(project.checker, type);
const tuple = SchemaFactory.application(app);
const literal = ExpressionFactory.generate(tuple);

const script: string = project.printer.printNode
(
ts.EmitHint.Unspecified,
tuple,
literal,
expression.getSourceFile()
);
const key: string = crypto
Expand All @@ -84,7 +87,7 @@ export namespace FunctionFactory
[],
undefined,
undefined,
tuple
literal
)
]);
}
Expand All @@ -104,8 +107,9 @@ export namespace FunctionFactory
throw new Error(`Error on TSON.createStringifier(): the generic argument must be specified - ${file.fileName}:${line + 1}:${character + 1}.`);
}

const app: ISchema.IApplication | null = SchemaFactory.generate(project.checker, type);
const tuple: ts.ArrayLiteralExpression = JsonFactory.application(app);
const app: IMetadata.IApplication | null = MetadataFactory.generate(project.checker, type);
const tuple = SchemaFactory.application(app);
const literal = ExpressionFactory.generate(tuple);

return ts.factory.createArrowFunction
(
Expand All @@ -114,7 +118,7 @@ export namespace FunctionFactory
[],
undefined,
undefined,
tuple
literal
);
}

Expand Down
138 changes: 0 additions & 138 deletions src/factories/JsonFactory.ts

This file was deleted.

Loading

0 comments on commit d1de90b

Please sign in to comment.