git @ Cat's Eye Technologies Lexeduct / f962220
filter => transformer, line => string. I will sleep better now. --HG-- rename : src/filter/double-space.js => src/transformers/double-space.js rename : src/filter/insert-vowels.js => src/transformers/insert-vowels.js rename : src/filter/lower.js => src/transformers/lower.js rename : src/filter/remove-chars.js => src/transformers/remove-chars.js rename : src/filter/upper.js => src/transformers/upper.js Chris Pressey 6 years ago
14 changed file(s) with 104 addition(s) and 104 deletion(s). Raw diff Collapse all Expand all
1616
1717 The basic usage is
1818
19 lexeduct.js {param=value|filter-name}
19 lexeduct.js {param=value|transformer-name}
2020
2121 So, for example,
2222
23 echo 'Hello!' | lexeduct.js upper
23 $ echo 'Hello!' | lexeduct.js upper
24 HELLO
2425
25 will dump the string `HELLO!` to standard output.
26 You can of course use shell pipelines to compose transformers:
2627
27 You can of course use shell pipelines to compose filters:
28 $ echo 'Hello!' | lexeduct.js upper | lexeduct.js double-space
29 H E L L O !
2830
29 cat input.txt | lexeduct.js upper | lexeduct.js double-space
31 *Or* you can name multiple transformers on `lexeduct.js`'s command line to
32 compose them:
3033
31 *Or* you can name multiple filters on `lexeduct.js`'s command line to compose
32 them:
33
34 lexeduct.js upper double-space <input.txt
34 $ echo 'Hello!' | lexeduct.js upper double-space
35 H E L L O !
3536
3637 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 transformer they are to be applied to. So, for example,
3839
39 echo 'Hello' | lexeduct.js chars=e remove-chars
40
40 $ echo 'Hello' | lexeduct.js chars=e remove-chars
4141 Hllo
4242
43 Filters
44 -------
43 Transformers
44 ------------
4545
4646 The idea is that this repository will eventually contain a giant catalogue
47 of possible text filters that can be composed. Or at least, more than four.
47 of possible text transformers that can be composed. Or at least, more than
48 are presently included.
4849
49 Each filter is in a seperate Javascript file in the `src/filter` directory
50 which exports, node-style, a single function called `makeFilter` which takes
51 a configuration object and returns a filter function. The filter function
52 takes two arguments: the current string of text to process, and (optionally)
53 an object which can be used to store ancillary state. It should return
54 either a string, or null (not yet supported), or an array of strings (not yet
55 supported.)
50 Each transformer is in a seperate Javascript file in the `src/transformers`
51 directory which exports, node-style, a single function called `makeTransformer`
52 which takes a configuration object and returns a transformer function. The
53 transformer function takes two arguments: the current string to process, and
54 (optionally) an object which can be used to store ancillary state. Every
55 transformer function should return either a string, or null (not yet supported),
56 or an array of strings (not yet supported.)
5657
57 As a simple example, here is the source of the `upper` filter, found
58 in `src/filter/upper.js`:
58 As a simple example, here is the source of the `upper` transformer, found
59 in `src/transformers/upper.js`:
5960
6061 module.exports = {
61 makeFilter: function(cfg) {
62 return function(line, state) {
63 return line.toUpperCase();
62 makeTransformer: function(cfg) {
63 return function(str, state) {
64 return str.toUpperCase();
6465 };
6566 }
6667 };
7374
7475 * Allow filters to do something at the very end, maybe.
7576 * Allow filters return multiple, or no, strings.
76 * Remove "line" nomenclature; they're "records", default record sep = newline.
7777 * Many, many other things.
2525 <script>
2626 function process() {
2727 var inputText = document.getElementById('input').value;
28 var f = makeFilter['upper']({});
29 var outputText = f(inputText);
28 var t = makeTransformer['upper']({});
29 var outputText = t(inputText);
3030 document.getElementById('output').value = outputText;
3131 }
3232 </script>
+0
-11
src/filter/double-space.js less more
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 s += line.charAt(i) + " ";
6 }
7 return s;
8 };
9 }
10 };
+0
-12
src/filter/insert-vowels.js less more
0 module.exports = {
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 };
10 }
11 };
+0
-7
src/filter/lower.js less more
0 module.exports = {
1 makeFilter: function(cfg) {
2 return function(line, state) {
3 return line.toLowerCase();
4 };
5 }
6 };
+0
-14
src/filter/remove-chars.js less more
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 };
+0
-7
src/filter/upper.js less more
0 module.exports = {
1 makeFilter: function(cfg) {
2 return function(line, state) {
3 return line.toUpperCase();
4 };
5 }
6 };
66 }
77
88 write("module = {};");
9 write("makeFilter = {};");
10 var files = fs.readdirSync('filter');
9 write("makeTransformer = {};");
10 var dirname = 'transformers';
11 var files = fs.readdirSync(dirname);
1112 for (var i = 0; i < files.length; i++) {
1213 var filename = files[i];
13 var filterName = filename.split('.js')[0];
14 var text = fs.readFileSync('filter/' + filename);
14 var transformerName = filename.split('.js')[0];
15 var text = fs.readFileSync(dirname + '/' + filename);
1516 write(text);
16 write("makeFilter['" + filterName + "'] = module.exports.makeFilter;");
17 write("makeTransformer['" + transformerName + "'] = module.exports.makeTransformer;");
1718 }
66 * except with 'data' parameter also.
77 */
88 var compose = function(g, f) {
9 return function(line, data) {
10 line = f(line, data);
11 return g(line, data);
9 return function(str, data) {
10 str = f(str, data);
11 return g(str, data);
1212 };
1313 };
1414
1515 /*
1616 * Implement the pipeline, streaming, from the input file, using the
17 * given filter, calling the given output function at the end of the pipe.
17 * given transformer, calling the given output function at the end of the pipe.
1818 */
19 var pipeline = function(infile, output, filter) {
19 var pipeline = function(infile, output, transformer) {
2020 var buffer = "";
2121 var state = {};
2222
3131 buffer = lines[lines.length - 1];
3232
3333 for (var i = 0; i < lines.length - 1; i++) {
34 output(filter(lines[i], state));
34 output(transformer(lines[i], state));
3535 }
3636 });
3737
3838 infile.on('end', function() {
39 output(filter(buffer, state));
39 output(transformer(buffer, state));
4040 });
4141 };
4242
4343 /*
44 * Load the filters that were specified on the command line.
44 * Load the transformers that were specified on the command line.
4545 */
46 var loadFilters = function(args) {
47 var filter = undefined;
46 var loadTransformers = function(args) {
47 var transformer = undefined;
4848 var cfg = {};
4949
5050 for (var i = 0; i < args.length; i++) {
5252 if (paramPair.length == 2) {
5353 cfg[paramPair[0]] = paramPair[1];
5454 } else {
55 var module = require('./filter/' + args[i]);
56 var loadedFilter = module.makeFilter(cfg);
55 var module = require('./transformers/' + args[i]);
56 var loadedTransformer = module.makeTransformer(cfg);
5757 cfg = {};
58 if (filter === undefined) {
59 filter = loadedFilter;
58 if (transformer === undefined) {
59 transformer = loadedTransformer;
6060 } else {
61 filter = compose(loadedFilter, filter);
61 transformer = compose(loadedTransformer, transformer);
6262 }
6363 }
6464 }
65 return filter;
65 return transformer;
6666 };
6767
6868 /*
8080 */
8181 (function() {
8282 var args = process.argv.slice(2);
83 var filter = loadFilters(args);
84 pipeline(process.stdin, output, filter);
83 var transformer = loadTransformers(args);
84 pipeline(process.stdin, output, transformer);
8585 })();
0 module.exports = {
1 makeTransformer: function(cfg) {
2 return function(str, state) {
3 var s = "";
4 for (var i = 0; i < str.length; i++) {
5 s += str.charAt(i) + " ";
6 }
7 return s;
8 };
9 }
10 };
0 module.exports = {
1 makeTransformer: function(cfg) {
2 return function(str, state) {
3 var s = "";
4 var vowels = "aeiou";
5 for (var i = 0; i < str.length; i++) {
6 s += str.charAt(i) + vowels.charAt(Math.floor(Math.random() * vowels.length));
7 }
8 return s;
9 };
10 }
11 };
0 module.exports = {
1 makeTransformer: function(cfg) {
2 return function(str, state) {
3 return str.toLowerCase();
4 };
5 }
6 };
0 module.exports = {
1 makeTransformer: function(cfg) {
2 return function(str, state) {
3 var s = "";
4 for (var i = 0; i < str.length; i++) {
5 if (cfg.chars.lastIndexOf(str.charAt(i)) === -1) {
6 s += c;
7 }
8 }
9 return s;
10 };
11 }
12 };
0 module.exports = {
1 makeTransformer: function(cfg) {
2 return function(str, state) {
3 return str.toUpperCase();
4 };
5 }
6 };