Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

Commit add54e4

Browse files
committed
Restructuring the code to be more readable
1 parent ccf27cf commit add54e4

File tree

1 file changed

+31
-30
lines changed

1 file changed

+31
-30
lines changed

lib/parser.js

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
const parse = function (trace) {
2-
const dir = function (ident) {
3-
if (ident === '=>') {
4-
return 'request';
5-
}
6-
if (ident === '<=') {
7-
return 'response';
8-
}
9-
};
1+
function dir(ident) {
2+
if (ident === '=>') {
3+
return 'request';
4+
}
5+
if (ident === '<=') {
6+
return 'response';
7+
}
8+
}
109

10+
function parse(trace) {
1111
// better regex, please, start here:
1212
// http://scriptular.com/#%5E(%3F%3A%5Ba-z0-9%5D%7B4%7D%3A)%20((%3F%3A%5Ba-z0-9%5D%7B2%7D%20)%7B1%2C16%7D)%7C%7C%7C%7C%7C%7C%7C%7C%5B%2200a0%3A%2011%2022%2033%2044%2055%2066%2077%2088%2099%2010%2011%2012%2013%2014%2015%2016%2017%20%20%7B.%20%20%5C%22headers%5C%22%3A%20%7B%22%5D
13-
1413
const traceLines = trace.split('\n');
1514
const dataPattern = /^(?:[a-z0-9]{4}:) ((?:[a-z0-9]{2} ){1,16})/;
1615
const dirPattern = /^(=>|<=)/;
@@ -35,7 +34,6 @@ const parse = function (trace) {
3534
}
3635
}
3736

38-
3937
// split lines by spaces and make array of ASCII hex bytes
4038
const asciiHexBuffer = { request: [], response: [] };
4139
for (const [direction, data] of asciiHexSets) {
@@ -62,14 +60,13 @@ const parse = function (trace) {
6260
}
6361
}
6462

65-
const output = {};
66-
output.request = stringBuffer.request.join('');
67-
output.response = stringBuffer.response.join('');
68-
return output;
69-
};
70-
63+
return {
64+
request: stringBuffer.request.join(''),
65+
response: stringBuffer.response.join('')
66+
};
67+
}
7168

72-
const parseToString = function (trace) {
69+
function parseToString(trace) {
7370
const message = parse(trace);
7471
let output = '';
7572

@@ -89,34 +86,38 @@ const parseToString = function (trace) {
8986
output += response.join('\r\n');
9087
output += '\n';
9188
return output;
92-
};
89+
}
9390

94-
95-
const parseBackRequestAndResponseFromString = function (string) {
96-
const output = {};
91+
function parseBackRequestAndResponseFromString(string) {
92+
const output = {request: '', response: ''};
9793

9894
const request = [];
9995
const stringLines = string.split('\r\n');
10096
for (const line of stringLines) {
101-
if (/^> /.test(line)) { request.push(line.replace(/^> /, '')); }
97+
if (line.startsWith('> ')) {
98+
request.push(line.replace(/^> /, ''));
99+
}
102100
}
103101

104102
// removing trailing LF
105103
output.request = request.join('\r\n').replace(/\n$/, '');
106104

107105
const response = [];
108106
for (const line of stringLines) {
109-
if (/^< /.test(line)) { response.push(line.replace(/^< /, '')); }
107+
if (line.startsWith('< ')) {
108+
response.push(line.replace(/^< /, ''));
109+
}
110110
}
111111

112112
// removing trailing LF
113113
output.response = response.join('\r\n').replace(/\n$/, '');
114114

115115
return output;
116-
};
116+
}
117117

118-
119-
module.exports.parseBackRequestAndResponseFromString = parseBackRequestAndResponseFromString;
120-
module.exports.parseBack = parseBackRequestAndResponseFromString;
121-
module.exports.parseToString = parseToString;
122-
module.exports.parse = parse;
118+
module.exports = {
119+
parseBackRequestAndResponseFromString,
120+
parseBack: parseBackRequestAndResponseFromString,
121+
parseToString,
122+
parse
123+
};

0 commit comments

Comments
 (0)