git @ Cat's Eye Technologies Lexeduct / da8086e
Allow parameters to be passed to filters on command line. Chris Pressey 6 years ago
3 changed file(s) with 37 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
1616
1717 The basic usage is
1818
19 lexeduct.js {filter}
19 lexeduct.js {param=value|filter-name}
2020
2121 So, for example,
2222
3232 them:
3333
3434 lexeduct.js upper double-space <input.txt
35
36 Parameters can be given with the syntax `name=value` *before* the name of the
37 filter they are to be applied to. So, for example,
38
39 echo 'Hello' | lexeduct.js chars=e remove-chars
40
41 Hllo
3542
3643 Filters
3744 -------
6471 TODO
6572 ----
6673
67 * Allow filters to take parameters, possibly.
6874 * Allow filters to do something at the very end, maybe.
75 * Allow filters return multiple, or no, strings.
76 * Remove "line" nomenclature; they're "records", default record sep = newline.
6977 * Many, many other things.
0 module.exports = {
1 makeFilter: function(cfg) {
2 return function(line, state) {
3 var s = "";
4 for (var i = 0; i < line.length; i++) {
5 var c = line.charAt(i);
6 if (cfg.chars.lastIndexOf(c) === -1) {
7 s += c;
8 }
9 }
10 return s;
11 };
12 }
13 };
4545 */
4646 var loadFilters = function(args) {
4747 var filter = undefined;
48 var cfg = {};
49
4850 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;
51 var paramPair = args[i].split('=');
52 if (paramPair.length == 2) {
53 cfg[paramPair[0]] = paramPair[1];
5454 } else {
55 filter = compose(loadedFilter, filter);
55 var module = require('./filter/' + args[i]);
56 var loadedFilter = module.makeFilter(cfg);
57 cfg = {};
58 if (filter === undefined) {
59 filter = loadedFilter;
60 } else {
61 filter = compose(loadedFilter, filter);
62 }
5663 }
5764 }
5865 return filter;