Skip to content

Commit 37455df

Browse files
author
mochatek
committed
fix: Ignore all white space characters while parsing
1 parent 17d7b7e commit 37455df

File tree

4 files changed

+28
-35
lines changed

4 files changed

+28
-35
lines changed

src/constants.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
11
const CHARACTER = {
22
BRACKET: {
3-
OPEN: `[`,
4-
CLOSE: `]`,
3+
OPEN: "[",
4+
CLOSE: "]",
55
},
66
BRACE: {
7-
OPEN: `{`,
8-
CLOSE: `}`,
7+
OPEN: "{",
8+
CLOSE: "}",
99
},
10-
QUOTE: `"`,
11-
ESCAPE: `\\`,
10+
QUOTE: '"',
11+
ESCAPE: "\\",
1212
SPACE: " ",
1313
COMMA: ",",
14+
TAB: "\t",
15+
CARRIAGE_RETURN: "\r",
1416
NEW_LINE: "\n",
17+
ZERO_WIDTH_NO_BREAK_SPACE: "\uFEFF",
1518
};
1619

20+
const WHITESPACE_CHARACTERS = [
21+
CHARACTER.SPACE,
22+
CHARACTER.TAB,
23+
CHARACTER.NEW_LINE,
24+
CHARACTER.CARRIAGE_RETURN,
25+
CHARACTER.ZERO_WIDTH_NO_BREAK_SPACE,
26+
];
27+
1728
const ERRORS = {
1829
INVALID_FILE: "Invalid JSON array file",
1930
INVALID_ELEMENT: (element: string) =>
2031
`Invalid element found in JSON array - ${element}`,
2132
};
2233

23-
export { CHARACTER, ERRORS };
34+
export { CHARACTER, WHITESPACE_CHARACTERS, ERRORS };

src/modules/JsonArrayAppend.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { stat, open, FileHandle } from "fs/promises";
2-
import { CHARACTER, ERRORS } from "../constants";
2+
import { CHARACTER, ERRORS, WHITESPACE_CHARACTERS } from "../constants";
33

44
const appendToJsonArray = async (
55
filePath: string,
@@ -28,9 +28,7 @@ const appendToJsonArray = async (
2828
insertPosition +=
2929
i === 0 ? 0 : Buffer.byteLength(windowValue.slice(0, i), encoding);
3030
break searchForArrayEnd;
31-
} else if (
32-
![CHARACTER.NEW_LINE, CHARACTER.SPACE].includes(windowValue[i])
33-
) {
31+
} else if (!WHITESPACE_CHARACTERS.includes(windowValue[i])) {
3432
throw new Error(ERRORS.INVALID_FILE);
3533
}
3634
}
@@ -55,7 +53,7 @@ const appendToJsonArray = async (
5553
const windowValue = window.toString(encoding);
5654

5755
for (let i = windowValue.length - 1; i >= 0; i--) {
58-
if (![CHARACTER.NEW_LINE, CHARACTER.SPACE].includes(windowValue[i])) {
56+
if (!WHITESPACE_CHARACTERS.includes(windowValue[i])) {
5957
if (windowValue[i] !== CHARACTER.BRACKET.OPEN) {
6058
insertData = `,${insertData}`;
6159
}

src/modules/JsonArrayStreamer.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { createReadStream } from "fs";
22
import { Readable } from "stream";
33
import { once } from "events";
44
import type { ElementType, ReadStreamOptions } from "../index.types";
5-
import { CHARACTER, ERRORS } from "../constants";
5+
import { CHARACTER, ERRORS, WHITESPACE_CHARACTERS } from "../constants";
66

77
class JsonArrayStreamer<T> {
88
private readStream: Readable | null;
@@ -40,7 +40,7 @@ class JsonArrayStreamer<T> {
4040
if (!this.readStream) throw new Error("Stream not initialized");
4141

4242
for await (const chunk of this.readStream) {
43-
yield chunk;
43+
yield chunk as string;
4444
}
4545
}
4646

@@ -144,11 +144,7 @@ class JsonArrayStreamer<T> {
144144
for (let char of chunk) {
145145
if (!this.rootDetected) {
146146
if (
147-
![
148-
CHARACTER.SPACE,
149-
CHARACTER.NEW_LINE,
150-
CHARACTER.BRACKET.OPEN,
151-
].includes(char)
147+
![...WHITESPACE_CHARACTERS, CHARACTER.BRACKET.OPEN].includes(char)
152148
)
153149
throw new Error(ERRORS.INVALID_FILE);
154150

@@ -159,11 +155,7 @@ class JsonArrayStreamer<T> {
159155
if (!this.elementDetected) {
160156
if (char === CHARACTER.BRACKET.CLOSE) break characterStream;
161157

162-
this.elementDetected = ![
163-
CHARACTER.SPACE,
164-
CHARACTER.COMMA,
165-
CHARACTER.NEW_LINE,
166-
].includes(char);
158+
this.elementDetected = !WHITESPACE_CHARACTERS.includes(char);
167159
}
168160

169161
if (this.elementDetected) {

src/modules/JsonArrayTransformer.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Transform, TransformCallback } from "stream";
2-
import { CHARACTER } from "../constants";
2+
import { CHARACTER, WHITESPACE_CHARACTERS } from "../constants";
33

44
class JsonArrayStreamer<T> extends Transform {
55
private encoding: BufferEncoding;
@@ -126,11 +126,7 @@ class JsonArrayStreamer<T> extends Transform {
126126
for (let char of decodedString) {
127127
if (!this.rootDetected) {
128128
if (
129-
![
130-
CHARACTER.SPACE,
131-
CHARACTER.NEW_LINE,
132-
CHARACTER.BRACKET.OPEN,
133-
].includes(char)
129+
![...WHITESPACE_CHARACTERS, CHARACTER.BRACKET.OPEN].includes(char)
134130
) {
135131
throw new Error("Invalid file structure");
136132
}
@@ -141,11 +137,7 @@ class JsonArrayStreamer<T> extends Transform {
141137

142138
if (!this.elementDetected) {
143139
if (char === CHARACTER.BRACKET.CLOSE) break;
144-
this.elementDetected = ![
145-
CHARACTER.SPACE,
146-
CHARACTER.COMMA,
147-
CHARACTER.NEW_LINE,
148-
].includes(char);
140+
this.elementDetected = !WHITESPACE_CHARACTERS.includes(char);
149141
}
150142

151143
if (this.elementDetected) {

0 commit comments

Comments
 (0)