Skip to content

Commit

Permalink
Parse to Read
Browse files Browse the repository at this point in the history
Changes:
- Renamed the `parse` function to `read`! This is more consistent with the Read and Write kind of idea, considering the Parse class is called Reader, instead of Parser. This also simplifies things for when I add JSON support too, with my mixed SNBT-JSON format that I want to create.
- The JSON functions will likely be called `parse` and `stringify`, and the binary NBT functions will be `read` and `write`.
  • Loading branch information
Offroaders123 committed Jun 25, 2022
1 parent ba81e9d commit 2ddb39a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import parse from "./parse.js";
import read from "./read.js";
import write from "./write.js";

export { parse, write };
export { read, write };

export default { parse, write, [Symbol.toStringTag]: "NBT" };
export default { read, write, [Symbol.toStringTag]: "NBT" };
10 changes: 5 additions & 5 deletions src/parse.js → src/read.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { tags, types } from "./tags.js";
import { decompress } from "./compression.js";

export default async function parse(data,{ endian } = {}){
export default async function read(data,{ endian } = {}){
if (!data) throw new Error("Unexpected falsy value for the data parameter");

if (typeof endian !== "undefined" && !["big","little"].includes(endian)){
Expand All @@ -15,18 +15,18 @@ export default async function parse(data,{ endian } = {}){

if (typeof endian !== "undefined"){
try {
const result = await runParser(data,endian);
const result = await runReader(data,endian);
return result;
} catch (error){
throw error;
}
} else {
let result = null;
try {
result = await runParser(data,"big");
result = await runReader(data,"big");
} catch (error){
try {
result = await runParser(data,"little");
result = await runReader(data,"little");
} catch {
throw error;
}
Expand All @@ -35,7 +35,7 @@ export default async function parse(data,{ endian } = {}){
}
}

async function runParser(data,endian){
async function runReader(data,endian){
if (hasGzipHeader(data)) data = await decompress(data,{ encoding: "gzip" });

const reader = new Reader(data,endian);
Expand Down

0 comments on commit 2ddb39a

Please sign in to comment.