Skip to content

Commit de59cbc

Browse files
authored
Revert "Remove traverse dependency" (#9)
1 parent d0cc258 commit de59cbc

File tree

3 files changed

+13
-34
lines changed

3 files changed

+13
-34
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"spawn-error-forwarder": "~1.0.0",
99
"split2": "~1.0.0",
1010
"stream-combiner2": "~1.1.1",
11-
"through2": "~2.0.0"
11+
"through2": "~2.0.0",
12+
"traverse": "~0.6.6"
1213
},
1314
"devDependencies": {
1415
"chai": "~1.10.0",

src/fields.js

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
var traverse = require('traverse');
4+
35
exports.config = {
46
commit: {
57
long: 'H',
@@ -30,27 +32,15 @@ exports.config = {
3032
};
3133

3234
exports.map = function () {
33-
var fields = [];
34-
35-
function crawl(node, path, parent) {
36-
if (typeof node === 'object') {
37-
for (var childKey in node) {
38-
if (!Object.prototype.hasOwnProperty.call(node, childKey)) continue;
39-
path.push(childKey);
40-
crawl(node[childKey], path, node);
41-
path.pop(childKey);
42-
}
43-
} else if (typeof node === 'string') {
44-
var typed = path[path.length - 1] === 'key';
35+
return traverse.reduce(exports.config, function (fields, node) {
36+
if (this.isLeaf && typeof node === 'string') {
37+
var typed = this.key === 'key';
4538
fields.push({
46-
path: typed ? path.slice(0, path.length - 1) : path.slice(0),
39+
path: typed ? this.parent.path : this.path,
4740
key: node,
48-
type: parent.type
41+
type: this.parent.node.type
4942
});
5043
}
51-
}
52-
53-
crawl(exports.config, [], undefined);
54-
55-
return fields;
44+
return fields;
45+
}, []);
5646
};

src/index.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var spawn = require('child_process').spawn;
44
var through = require('through2');
55
var split = require('split2');
6+
var traverse = require('traverse');
67
var fields = require('./fields');
78
var toArgv = require('argv-formatter').format;
89
var combine = require('stream-combiner2');
@@ -41,19 +42,6 @@ function args (config, fieldMap) {
4142
return toArgv(config);
4243
}
4344

44-
function setByPath(obj, path, value) {
45-
var dest = obj;
46-
for (var i = 0; i < path.length - 1; i++) {
47-
var key = path[i];
48-
if (!Object.prototype.hasOwnProperty.call(obj, key)) {
49-
obj[key] = {};
50-
}
51-
dest = dest[key];
52-
}
53-
54-
dest[path[path.length - 1]] = value;
55-
}
56-
5745
exports.parse = function parseLogStream (config, options) {
5846
config = config || {};
5947
var map = fields.map();
@@ -65,7 +53,7 @@ exports.parse = function parseLogStream (config, options) {
6553
var fields = chunk.toString('utf8').split(FIELD);
6654
callback(null, map.reduce(function (parsed, field, index) {
6755
var value = fields[index];
68-
setByPath(parsed, field.path, field.type ? new field.type(value) : value);
56+
traverse(parsed).set(field.path, field.type ? new field.type(value) : value);
6957
return parsed;
7058
}, {}));
7159
})

0 commit comments

Comments
 (0)