Skip to content

Commit 9f0374f

Browse files
committed
fix: get regex to actually work
1 parent e5776a7 commit 9f0374f

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

src/StreamCleaner.test.ts

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,44 @@ import { Readable } from "stream";
22
import StreamCleaner, { MATCH_NON_PRINTABLE } from "./StreamCleaner";
33

44
describe("StreamCleaner", () => {
5-
test("remove non-printable characters", () => {
5+
test("remove non-printable characters", async () => {
66
const source = new Readable();
77

88
const transform = new StreamCleaner(MATCH_NON_PRINTABLE);
99

10-
source
11-
.pipe(transform)
12-
.on("data", chunk =>
13-
expect(chunk.toString()).not.toMatch(MATCH_NON_PRINTABLE)
14-
);
10+
let result = new Promise(resolve => {
11+
source
12+
.pipe(transform)
13+
.on("data", (chunk: Buffer) => expect(chunk).toHaveLength(1))
14+
.on("end", resolve);
15+
});
1516

1617
for (let i = 0; i <= 31; i++) {
18+
const chr = String.fromCharCode(i);
19+
source.push(chr + "A" + chr);
20+
}
21+
source.push(null);
22+
23+
await result;
24+
});
25+
26+
test("keep printable characters", async () => {
27+
const source = new Readable();
28+
29+
const transform = new StreamCleaner(MATCH_NON_PRINTABLE);
30+
31+
let result = new Promise(resolve => {
32+
source
33+
.pipe(transform)
34+
.on("data", (chunk: Buffer) => expect(chunk).toHaveLength(1))
35+
.on("end", resolve);
36+
});
37+
38+
for (let i = 32; i <= 126; i++) {
1739
source.push(String.fromCharCode(i));
1840
}
1941
source.push(null);
42+
43+
await result;
2044
});
2145
});

src/StreamCleaner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Transform, TransformCallback } from "stream";
22

3-
export const MATCH_NON_PRINTABLE = /[^\000-\031]+/gi;
3+
export const MATCH_NON_PRINTABLE = /[\u0000-\u0031]+/g;
44

55
export default class StreamCleaner extends Transform {
66
constructor(private readonly pattern: RegExp) {

0 commit comments

Comments
 (0)