Skip to content

Commit 21a47f2

Browse files
committed
let sync version work
1 parent 3ececdf commit 21a47f2

File tree

7 files changed

+68
-23
lines changed

7 files changed

+68
-23
lines changed

index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
var TreeLine = require('./lib/tree-line');
22
var AsciiTree = require('./lib/ascii-tree');
3-
var BufferFilter = require('./lib/buffer-filter');
3+
var BufferConvertor = require('./lib/buffer-convertor');
44
var LineUtil = require('./lib/line-util');
55
var Char = require('./lib/char');
66

77
module.exports.TreeLine = TreeLine;
88
module.exports.AsciiTree = AsciiTree;
9-
module.exports.BufferFilter = BufferFilter;
9+
module.exports.BufferConvertor = BufferConvertor;
1010
module.exports.LineUtil = LineUtil;
1111
module.exports.Char = Char;
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,46 @@
11
var BytesLine = require('./bytes-line');
2+
var AsciiTree = require('./ascii-tree');
23

3-
module.exports = BufferFilter;
4+
module.exports = BufferConvertor;
45

56
/**
6-
* @constructor BufferFilter
7+
* @constructor BufferConvertor
78
* @param {(string|Buffer)} bufOrStr
9+
* @param {string|Regex} startTag
10+
* @param {string|Regex} endTag
811
* @param {{string}} [enc=UTF-8] - encoding.
912
*/
10-
function BufferFilter(bufOrStr, enc) {
13+
function BufferConvertor(bufOrStr, startTag, endTag, enc) {
1114
if (typeof bufOrStr === 'string') {
1215
this.buf = enc ? new Buffer(bufOrStr, enc) : new Buffer(bufOrStr);
1316
} else {
1417
this.buf = bufOrStr;
1518
}
19+
this.startTag = startTag;
20+
this.endTag = endTag;
1621
this.enc = enc;
1722
this.mixedLines = [];
1823
}
1924
/**
2025
* process all AsciiTree in buffer.
2126
* @function filter
22-
* @return {BufferFilter} - return this.
27+
* @return {BufferConvertor} - return this.
2328
*/
2429

25-
BufferFilter.prototype.filter = function() {
30+
BufferConvertor.prototype.convert = function() {
2631
var i = 0,
2732
allLines = BytesLine.getArray(this.buf),
2833
oneLine,
2934
lineArray = [],
3035
startTagReached = false,
3136
lineType;
32-
3337
for (; i < allLines.length; i++) {
3438
oneLine = allLines[i];
35-
lineType = oneLine.isTagLine(startTag, endTag);
39+
lineType = oneLine.isTagLine(this.startTag, this.endTag);
3640
if (lineType === 'START') {
3741
startTagReached = true;
3842
} else if (lineType === 'END') {
39-
this.mixedLines.push(lineArray);
43+
this.mixedLines.push(new AsciiTree(lineArray).convert().toBytesLines());
4044
lineArray = [];
4145
startTagReached = false;
4246
} else {
@@ -57,23 +61,38 @@ BufferFilter.prototype.filter = function() {
5761
* @function toBufferArray
5862
* @return {Buffer[]} - flatted array of Buffer.
5963
*/
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) {
6266
return val.concat(it);
6367
}, []);
64-
return flattedLines.map(function(it){
68+
69+
return flattedLines.map(function(it) {
6570
return it.toBuffer();
6671
});
6772
};
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+
};
6884

6985
/**
7086
* @function toBuffer
7187
* @return {Buffer} - whole buffer with asciitree converted.
7288
*/
73-
BufferFilter.prototype.toBuffer = function() {
89+
BufferConvertor.prototype.toBuffer = function() {
7490
return Buffer.concat(this.toBufferArray());
7591
};
7692

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();
7998
};

lib/bytes-line.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ BytesLine.getArray = function(src, enc) {
119119
crlf = [];
120120
}
121121
byteArray.push(code);
122-
clrfReached = false;
122+
crlfReached = false;
123123
}
124124
}
125125

test/buffer-convertor.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var fs = require('fs');
2+
var assert = require('assert');
3+
var BytesLine = require('../lib/bytes-line');
4+
var BufferConvertor = require('../lib/buffer-convertor');
5+
var LineUtil = require('../lib/line-util');
6+
7+
describe('BlockFilter', function() {
8+
describe('#constructor', function() {
9+
it('array push should work.', function(){
10+
var a = [];
11+
a.push([1, 2, 3]);
12+
assert.equal(1, a.length);
13+
});
14+
it('should handle string', function() {
15+
var s = "a\rxx\rbbbb\r-b1\ryy\rxx\raaa\r-uuuuu\ryy\r";
16+
var lines = BytesLine.getArray(s);
17+
assert.equal(9, lines.length);
18+
var bufConvertor = new BufferConvertor(s, 'xx', 'yy').convert();
19+
var bufArray = bufConvertor.toBufferArray();
20+
assert.equal(5, bufArray.length);
21+
console.log(bufArray);
22+
var strArray = bufConvertor.toStringArray();
23+
console.log(strArray);
24+
assert.equal('└── bbbb\r', strArray[1]);
25+
26+
});
27+
});
28+
});

test/buffer-filter.js

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
var assert = require('assert');
22
var through2 = require('through2');
3-
var FileFilter = require('../lib/buffer-filter');
43

54
var splitterStream = require('../lib/splitter-stream');
65
var blockStream = require('../lib/block-stream');
@@ -28,7 +27,6 @@ describe('FileFilter', function() {
2827
})).on('finish', function() {
2928
assert.equal(1, count);
3029
assert.equal(fixtures.stringArray.length, blocks[0].length);
31-
3230
done();
3331
});
3432

test/bytes-line.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ describe('BytesLine', function() {
2424

2525
describe('#getArray()', function() {
2626
it("should handle string.", function() {
27-
var lines = BytesLine.getArray("a\rb\rc\r\r\r");
27+
var lines = BytesLine.getArray("a\rbbbbbbbb\rc\r\r\r");
2828
assert.equal(3, lines.length);
2929
assert.equal('a'.charCodeAt(0), lines[0].content[0]);
3030
assert.deepEqual([0x0D, 0x0D, 0x0D], lines[2].separator);
3131
});
3232
it("should handle buffer.", function() {
33-
var lines = BytesLine.getArray(new Buffer("a\rb\rc\r\r\r"));
33+
var lines = BytesLine.getArray(new Buffer("a\rb\rcccccccccccccc\r\r\r"));
3434
assert.equal(3, lines.length);
3535
assert.equal('a'.charCodeAt(0), lines[0].content[0]);
3636
assert.deepEqual([0x0D, 0x0D, 0x0D], lines[2].separator);

test/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var assert = require('assert');
22
var AsciiTree = require('../index').AsciiTree;
3-
var BufferFilter = require('../index').BufferFilter;
3+
var BufferConvertor = require('../index').BufferConvertor;
44
var TreeLine = require('../index').TreeLine;
55
var LineUtil = require('../index').LineUtil;
66
var Char = require('../index').Char;
@@ -10,7 +10,7 @@ describe('required', function() {
1010
describe('#require()', function() {
1111
it('should ok', function() {
1212
assert(AsciiTree, "AsciiTree should imported.");
13-
assert(BufferFilter, "BufferFilter should imported.");
13+
assert(BufferConvertor, "BufferConvertor should imported.");
1414
assert(TreeLine, "Line should imported.");
1515
assert(LineUtil, "LineUtil should imported.");
1616
assert(Char, "Char should imported.");

0 commit comments

Comments
 (0)