1
1
var BytesLine = require ( './bytes-line' ) ;
2
+ var AsciiTree = require ( './ascii-tree' ) ;
2
3
3
- module . exports = BufferFilter ;
4
+ module . exports = BufferConvertor ;
4
5
5
6
/**
6
- * @constructor BufferFilter
7
+ * @constructor BufferConvertor
7
8
* @param {(string|Buffer) } bufOrStr
9
+ * @param {string|Regex } startTag
10
+ * @param {string|Regex } endTag
8
11
* @param {{string} } [enc=UTF-8] - encoding.
9
12
*/
10
- function BufferFilter ( bufOrStr , enc ) {
13
+ function BufferConvertor ( bufOrStr , startTag , endTag , enc ) {
11
14
if ( typeof bufOrStr === 'string' ) {
12
15
this . buf = enc ? new Buffer ( bufOrStr , enc ) : new Buffer ( bufOrStr ) ;
13
16
} else {
14
17
this . buf = bufOrStr ;
15
18
}
19
+ this . startTag = startTag ;
20
+ this . endTag = endTag ;
16
21
this . enc = enc ;
17
22
this . mixedLines = [ ] ;
18
23
}
19
24
/**
20
25
* process all AsciiTree in buffer.
21
26
* @function filter
22
- * @return {BufferFilter } - return this.
27
+ * @return {BufferConvertor } - return this.
23
28
*/
24
29
25
- BufferFilter . prototype . filter = function ( ) {
30
+ BufferConvertor . prototype . convert = function ( ) {
26
31
var i = 0 ,
27
32
allLines = BytesLine . getArray ( this . buf ) ,
28
33
oneLine ,
29
34
lineArray = [ ] ,
30
35
startTagReached = false ,
31
36
lineType ;
32
-
33
37
for ( ; i < allLines . length ; i ++ ) {
34
38
oneLine = allLines [ i ] ;
35
- lineType = oneLine . isTagLine ( startTag , endTag ) ;
39
+ lineType = oneLine . isTagLine ( this . startTag , this . endTag ) ;
36
40
if ( lineType === 'START' ) {
37
41
startTagReached = true ;
38
42
} else if ( lineType === 'END' ) {
39
- this . mixedLines . push ( lineArray ) ;
43
+ this . mixedLines . push ( new AsciiTree ( lineArray ) . convert ( ) . toBytesLines ( ) ) ;
40
44
lineArray = [ ] ;
41
45
startTagReached = false ;
42
46
} else {
@@ -57,23 +61,38 @@ BufferFilter.prototype.filter = function() {
57
61
* @function toBufferArray
58
62
* @return {Buffer[] } - flatted array of Buffer.
59
63
*/
60
- BufferFilter . prototype . toBufferArray = function ( ) {
61
- var flattedLines = this . mixedLines . reduce ( function ( val , it ) {
64
+ BufferConvertor . prototype . toBufferArray = function ( ) {
65
+ var flattedLines = this . mixedLines . reduce ( function ( val , it ) {
62
66
return val . concat ( it ) ;
63
67
} , [ ] ) ;
64
- return flattedLines . map ( function ( it ) {
68
+
69
+ return flattedLines . map ( function ( it ) {
65
70
return it . toBuffer ( ) ;
66
71
} ) ;
67
72
} ;
73
+ /**
74
+ * @function toStringArray
75
+ * @return {string[] } - array of string.
76
+ */
77
+ BufferConvertor . prototype . toStringArray = function ( ) {
78
+ var enc = this . enc ,
79
+ bufs = this . toBufferArray ( ) ;
80
+ return bufs . map ( function ( it ) {
81
+ return enc ? it . toString ( enc ) : it . toString ( ) ;
82
+ } ) ;
83
+ } ;
68
84
69
85
/**
70
86
* @function toBuffer
71
87
* @return {Buffer } - whole buffer with asciitree converted.
72
88
*/
73
- BufferFilter . prototype . toBuffer = function ( ) {
89
+ BufferConvertor . prototype . toBuffer = function ( ) {
74
90
return Buffer . concat ( this . toBufferArray ( ) ) ;
75
91
} ;
76
92
77
- BufferFilter . prototype . toString = function ( ) {
78
- return this . enc ? this . toBuffer ( ) . toString ( this . enc ) : this . toBuffer ( ) . toString ( ) ;
93
+ BufferConvertor . prototype . toString = function ( ) {
94
+ var enc = this . enc ,
95
+ buf = this . toBuffer ( ) ;
96
+
97
+ return enc ? buf . toString ( enc ) : buf . toString ( ) ;
79
98
} ;
0 commit comments