git @ Cat's Eye Technologies Lexeduct / eeeffc6
Refactor lexeduct.js into a more self-contained unit (for now.) Chris Pressey 6 years ago
2 changed file(s) with 56 addition(s) and 39 deletion(s). Raw diff Collapse all Expand all
00 #!/usr/bin/env node
11
2 var pipe = require('./lib/pipe');
3 var args = process.argv.slice(2);
4
5 var filter = undefined;
2 /*** FUNCTIONS ***/
63
74 /*
85 * Like mathematical https://en.wikipedia.org/wiki/Function_composition,
1613 };
1714
1815 /*
16 * Implement the pipeline, streaming, from the input file, using the
17 * given filter, calling the given output function at the end of the pipe.
18 */
19 var pipeline = function(infile, output, filter) {
20 var buffer = "";
21 var state = {};
22
23 infile.resume();
24 infile.setEncoding('utf8');
25
26 infile.on('data', function(data) {
27 data = data.replace(/\r/g, '');
28 var lines = data.split("\n");
29
30 lines[0] = buffer + lines[0];
31 buffer = lines[lines.length - 1];
32
33 for (var i = 0; i < lines.length - 1; i++) {
34 output(filter(lines[i], state));
35 }
36 });
37
38 infile.on('end', function() {
39 output(filter(buffer, state));
40 });
41 };
42
43 /*
1944 * Load the filters that were specified on the command line.
2045 */
21 for (var i = 0; i < args.length; i++) {
22 // TODO: parse filter parameters off end of args[i]
23 var module = require('./filter/' + args[i]);
24 var loadedFilter = module.makeFilter({});
25 if (filter === undefined) {
26 filter = loadedFilter;
27 } else {
28 filter = compose(loadedFilter, filter);
46 var loadFilters = function(args) {
47 var filter = undefined;
48 for (var i = 0; i < args.length; i++) {
49 // TODO: parse filter parameters off end of args[i]
50 var module = require('./filter/' + args[i]);
51 var loadedFilter = module.makeFilter({});
52 if (filter === undefined) {
53 filter = loadedFilter;
54 } else {
55 filter = compose(loadedFilter, filter);
56 }
2957 }
30 }
58 return filter;
59 };
3160
61 /*
62 * End-of-pipe callback used for pipeline().
63 */
3264 var output = function(line) {
3365 process.stdout.write(line + "\n");
3466 };
3567
36 pipe.line(process.stdin, output, filter);
68 /*** MAIN ***/
69
70 /*
71 * Enclosed in an anonymous function so that we don't have to worry about
72 * shadowing the name of an already-used variable.
73 */
74 (function() {
75 var args = process.argv.slice(2);
76 var filter = loadFilters(args);
77 pipeline(process.stdin, output, filter);
78 })();
+0
-25
src/lib/pipe.js less more
0 module.exports = {
1 line: function(infile, output, processLine) {
2 var buffer = "";
3 var state = {};
4
5 infile.resume();
6 infile.setEncoding('utf8');
7
8 infile.on('data', function(data) {
9 data = data.replace(/\r/g, '');
10 var lines = data.split("\n");
11
12 lines[0] = buffer + lines[0];
13 buffer = lines[lines.length - 1];
14
15 for (var i = 0; i < lines.length - 1; i++) {
16 output(processLine(lines[i], state));
17 }
18 });
19
20 infile.on('end', function() {
21 output(processLine(buffer, state));
22 });
23 }
24 };