Skip to content

Commit

Permalink
toRawString method
Browse files Browse the repository at this point in the history
  • Loading branch information
MrZillaGold committed Mar 22, 2021
1 parent 3d9bbb1 commit f527dfe
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rawjsonbuilder",
"version": "1.1.8",
"version": "1.1.9",
"description": "Minecraft Raw JSON text builder",
"main": "./lib/RawJSONBuilder.js",
"types": "./lib/interfaces.d.ts",
Expand Down
14 changes: 13 additions & 1 deletion src/RawJSONBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ export class RawJSONBuilder {
message: IText | ITranslate | IClickEvent | IKeybind | NBT | IScore | ISelector;
extra: (IText | ITranslate | IClickEvent | IKeybind | NBT | IScore | ISelector)[] = [];

constructor(rawJSON: RawJSON = {}) {
constructor(rawJSON: RawJSON | string = {}) {
if (rawJSON instanceof RawJSONBuilder) {
rawJSON = rawJSON.toJSON();
} else if (typeof rawJSON === "string") {
try {
rawJSON = JSON.parse(rawJSON) as RawJSON;
} catch {
throw new Error("Invalid JSON string!");
}
}

const { extra, ...message } = rawJSON;
Expand Down Expand Up @@ -113,4 +119,10 @@ export class RawJSONBuilder {
toString(): string {
return JSON.stringify(this.toJSON());
}

toRawString(): string {
return parser.parseJSON(
this.toJSON()
);
}
}

0 comments on commit f527dfe

Please sign in to comment.