File tree Expand file tree Collapse file tree 2 files changed +31
-7
lines changed
Expand file tree Collapse file tree 2 files changed +31
-7
lines changed Original file line number Diff line number Diff line change @@ -2,20 +2,44 @@ import { Readable } from "stream";
22import StreamCleaner , { MATCH_NON_PRINTABLE } from "./StreamCleaner" ;
33
44describe ( "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} ) ;
Original file line number Diff line number Diff line change 11import { Transform , TransformCallback } from "stream" ;
22
3- export const MATCH_NON_PRINTABLE = / [ ^ \0 0 0 - \0 3 1 ] + / gi ;
3+ export const MATCH_NON_PRINTABLE = / [ \u0000 - \u0031 ] + / g ;
44
55export default class StreamCleaner extends Transform {
66 constructor ( private readonly pattern : RegExp ) {
You can’t perform that action at this time.
0 commit comments