git @ Cat's Eye Technologies Lexeduct / 054a004
Change the filter interface to allow configuring with parameters. Chris Pressey 10 years ago
6 changed file(s) with 46 addition(s) and 29 deletion(s). Raw diff Collapse all Expand all
2020
2121 So, for example,
2222
23 lexeduct.js upper <input.txt
23 echo 'Hello!' | lexeduct.js upper
2424
25 and an uppercased version of the contents of `input.txt` will be dumped to
26 the standard output.
25 will dump the string `HELLO!` to standard output.
2726
2827 You can of course use shell pipelines to compose filters:
2928
4140 of possible text filters that can be composed. Or at least, more than four.
4241
4342 Each filter is in a seperate Javascript file in the `src/filter` directory
44 which exports, node-style, a single function called `filter` which takes
45 two arguments. For example, here is the source of the `upper` filter, found
43 which exports, node-style, a single function called `makeFilter` which takes
44 a configuration object and returns a filter function. The filter function
45 takes two arguments: the current string of text to process, and (optionally)
46 an object which can be used to store ancillary state. It should return
47 either a string, or null (not yet supported), or an array of strings (not yet
48 supported.)
49
50 As a simple example, here is the source of the `upper` filter, found
4651 in `src/filter/upper.js`:
4752
4853 module.exports = {
49 filter: function(line, state) {
50 return line.toUpperCase();
54 makeFilter: function(cfg) {
55 return function(line, state) {
56 return line.toUpperCase();
57 };
5158 }
5259 };
5360
5461 `state` is an object whose members may be read or written to store ancillary
5562 state. (Doing so will make it an 'impure' pipeline.)
56
57 **NOTE**: this interface is likely to change real soon now, to support passing
58 parameters to filters.
5963
6064 TODO
6165 ----
00 module.exports = {
1 filter: function(line, state) {
2 var s = "";
3 for (var i = 0; i < line.length; i++) {
4 s += line.charAt(i) + " ";
5 }
6 return s;
1 makeFilter: function(cfg) {
2 return function(line, state) {
3 var s = "";
4 for (var i = 0; i < line.length; i++) {
5 s += line.charAt(i) + " ";
6 }
7 return s;
8 };
79 }
810 };
00 module.exports = {
1 filter: function(line, state) {
2 var s = "";
3 var vowels = "aeiou";
4 for (var i = 0; i < line.length; i++) {
5 s += line.charAt(i) + vowels.charAt(Math.floor(Math.random() * vowels.length));
6 }
7 return s;
1 makeFilter: function(cfg) {
2 return function(line, state) {
3 var s = "";
4 var vowels = "aeiou";
5 for (var i = 0; i < line.length; i++) {
6 s += line.charAt(i) + vowels.charAt(Math.floor(Math.random() * vowels.length));
7 }
8 return s;
9 };
810 }
911 };
00 module.exports = {
1 filter: function(line, state) {
2 return line.toLowerCase();
1 makeFilter: function(cfg) {
2 return function(line, state) {
3 return line.toLowerCase();
4 };
35 }
46 };
00 module.exports = {
1 filter: function(line, state) {
2 return line.toUpperCase();
1 makeFilter: function(cfg) {
2 return function(line, state) {
3 return line.toUpperCase();
4 };
35 }
46 };
1515 };
1616 };
1717
18 /*
19 * Load the filters that were specified on the command line.
20 */
1821 for (var i = 0; i < args.length; i++) {
22 // TODO: parse filter parameters off end of args[i]
1923 var module = require('./filter/' + args[i]);
24 var loadedFilter = module.makeFilter({});
2025 if (filter === undefined) {
21 filter = module.filter;
26 filter = loadedFilter;
2227 } else {
23 filter = compose(module.filter, filter);
28 filter = compose(loadedFilter, filter);
2429 }
2530 }
2631