Skip to content

Commit

Permalink
Use bun to read files.
Browse files Browse the repository at this point in the history
  • Loading branch information
FS-Frost committed Aug 6, 2024
1 parent 364eb3b commit d4ef958
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 17 deletions.
2 changes: 1 addition & 1 deletion build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const buildDir = "./examples/build";
}
}

const dtsContent = await fs.readFile(`${buildDir}/asu.d.ts`);
const dtsContent = await Bun.file(`${buildDir}/asu.d.ts`).text();
if (await fs.exists(buildDir)) {
await fs.rm(buildDir, {
recursive: true,
Expand Down
2 changes: 1 addition & 1 deletion jsr.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@fs-frost/asu",
"version": "1.3.0",
"version": "1.4.0",
"exports": "./src/asu.ts"
}
16 changes: 3 additions & 13 deletions src/assFile/assFile.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { expect, test } from "bun:test";
import fs from "fs/promises";
import { parseASSFile } from "./assFile";

test("assFile: parse ass file", async () => {
const text = await fs.readFile("test/parseFile.ass", {
encoding: "utf-8",
});

const text = await Bun.file("test/parseFile.ass").text();
const assFile = parseASSFile(text);
expect(assFile).not.toBeNull();
if (assFile == null) {
Expand All @@ -30,10 +26,7 @@ test("assFile: parse ass file", async () => {
});

test("assFile: parse karaoke file", async () => {
const text = await fs.readFile("test/karaoke.ass", {
encoding: "utf-8",
});

const text = await Bun.file("test/karaoke.ass").text();
const assFile = parseASSFile(text);
expect(assFile).not.toBeNull();
if (assFile == null) {
Expand All @@ -54,10 +47,7 @@ test("assFile: parse karaoke file", async () => {
});

test("assFile: parse mix file", async () => {
const text = await fs.readFile("test/mix.ass", {
encoding: "utf-8",
});

const text = await Bun.file("test/mix.ass").text();
const assFile = parseASSFile(text);
expect(assFile).not.toBeNull();
if (assFile == null) {
Expand Down
12 changes: 10 additions & 2 deletions src/assFile/assFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export function parseASSFile(text: string): ASSFile | null {

const linesToParse: string[] = text.split("\n");
for (let i = 0; i < linesToParse.length; i++) {
const lineNumber = i + 1;
const line = removeUtf8Boom(linesToParse[i]);
if (line.length === 0) {
continue;
Expand Down Expand Up @@ -121,11 +122,12 @@ export function parseASSFile(text: string): ASSFile | null {
processExtraDataLine(assFile, line);
break;
default:
console.error(`failed to parse line ${lineNumber}`);
break;
}

if (err.length > 0) {
console.error(`failed to parse ass file at line ${i + 1}: ${err}\nLine:\n${line}`);
console.error(`failed to parse ass file at line ${lineNumber}: ${err}\nLine:\n${line}`);
return null;
}
}
Expand All @@ -134,7 +136,13 @@ export function parseASSFile(text: string): ASSFile | null {
}

function removeUtf8Boom(s: string): string {
return s.replaceAll("\\uFEFF", "");
// remove boom
s = s.replaceAll("\\uFEFF", "");

// remove windows end of line
s = s.replaceAll("\r", "");

return s;
}


Expand Down

0 comments on commit d4ef958

Please sign in to comment.