Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

DSL for mapping

  • Loading branch information...
commit dacad8975b206bbea50194d1ba11dbec5d902587 1 parent dc1a1f5
Pedro Teixeira authored

Showing 3 changed files with 99 additions and 16 deletions. Show diff stats Hide diff stats

  1. +46 16 lib/map.js
  2. +8 0 lib/select.js
  3. +45 0 test/dsl_compose_streaming.js
62 lib/map.js
... ... @@ -1,4 +1,5 @@
1 1 var Stream = require('stream').Stream;
  2 +var BufferedStream = require('bufferedstream');
2 3
3 4 var actions = ['remove', 'replace', 'update'];
4 5
@@ -11,12 +12,39 @@ function actionFromVal(val) {
11 12 try {
12 13 throw new Error('Ambiguous action, don\'t know what to do with' + JSON.stringify(val));
13 14 } catch (e) {
14   - throw new Error('Ambiguous action, don\'t know what to do with it');
  15 + throw new Error('Ambiguous action, don\'t know what to do with it:' + val.toString());
15 16 }
16 17 }
17 18 return remainingActions[0];
18 19 }
19 20
  21 +function handleStream(selector, action, stream) {
  22 + var buffer = [], ended = false;
  23 + stream.on('data', function(d) { buffer.push(d); });
  24 + stream.on('end', function() { ended = true; });
  25 +
  26 + this.select(selector, function(node) {
  27 + node[action](function(html) {
  28 + var index = 0;
  29 + var bs = new BufferedStream();
  30 +
  31 + (function flush() {
  32 + if (buffer.length > index) {
  33 + bs.write(buffer[index]);
  34 + index ++;
  35 + }
  36 + if (buffer.length > index || ! ended) {
  37 + process.nextTick(flush);
  38 + } else {
  39 + bs.end();
  40 + }
  41 + }());
  42 +
  43 + return bs;
  44 + });
  45 + });
  46 +}
  47 +
20 48 function map(mapping) {
21 49 var self = this;
22 50
@@ -27,34 +55,36 @@ function map(mapping) {
27 55 Object.keys(mapping).forEach(function(selector) {
28 56 var val = mapping[selector];
29 57
30   - // console.log('val:', val);
31   -
32 58 switch (typeof val) {
33 59
34 60 case 'string':
35 61 case 'function':
36 62
37   - console.log('A');
38   -
39   - console.log('.update(%j, %s)', selector, val);
40   -
41 63 self.update(selector, val);
42 64
43 65 break;
44 66
45 67 case 'object':
46 68
47   - console.log('B');
48   -
49   - var action = actionFromVal(val);
50   - if (! action) {
51   - throw new Error('Value for selector `' + selector + '` should contain action field, either rmeove, replace or update');
  69 + if (val instanceof Stream) {
  70 +
  71 + handleStream.call(self, selector, 'update', val);
  72 +
  73 + } else {
  74 +
  75 + var action = actionFromVal(val);
  76 + if (! action) {
  77 + throw new Error('Value for selector `' + selector +
  78 + '` should contain action field, either remove, replace or update');
  79 + }
  80 + var replaceBy = val[action];
  81 + if (replaceBy instanceof Stream) {
  82 + handleStream.call(self, selector, action, replaceBy);
  83 + } else {
  84 + self[action](selector, replaceBy);
  85 + }
52 86 }
53 87
54   - console.log('.%s(%j, %j)', action, selector, val[action]);
55   -
56   - self[action](selector, val[action]);
57   -
58 88 break;
59 89
60 90 default:
8 lib/select.js
@@ -142,6 +142,14 @@ module.exports = function (parser, opts) {
142 142 if (done) { done(); }
143 143 });
144 144 };
  145 +
  146 + stream.pause = function() {
  147 + parser.pause();
  148 + };
  149 +
  150 + stream.resume = function() {
  151 + parser.resume();
  152 + };
145 153
146 154 return stream;
147 155 };
45 test/dsl_compose_streaming.js
... ... @@ -0,0 +1,45 @@
  1 +var test = require('tap').test;
  2 +var trumpet = require('../');
  3 +var fs = require('fs');
  4 +var BufferedStream = require('bufferedstream');
  5 +
  6 +test('compose with streams', function (t) {
  7 + t.plan(1);
  8 + var html = fs.readFileSync(__dirname + '/compose_target.html', 'utf8');
  9 +
  10 + var tr = trumpet();
  11 + fs.createReadStream(__dirname + '/compose.html').pipe(tr);
  12 +
  13 + var tr2 = trumpet();
  14 + fs.createReadStream(__dirname + '/partial.html').pipe(tr2);
  15 +
  16 + var tr3 = trumpet();
  17 + fs.createReadStream(__dirname + '/partial.html').pipe(tr3);
  18 + tr3.select('.b span', function(node) {
  19 + node.update(function(html) {
  20 + return html.toUpperCase();
  21 + });
  22 + });
  23 +
  24 + var stream = new BufferedStream();
  25 + stream.end('<b>NOTHING TO SEE HERE</b>');
  26 +
  27 + var stream2 = new BufferedStream();
  28 + stream2.end('<blink>TERRIBLE</blink>');
  29 +
  30 + tr.map({
  31 + '.b span': tr2,
  32 + '.c': tr3,
  33 + '.d': { remove: true },
  34 + '.e': { remove: true },
  35 + '.f': { replace: stream },
  36 + '.g': { replace: stream2}
  37 + });
  38 +
  39 + var data = '';
  40 + tr.on('data', function (buf) { data += buf });
  41 +
  42 + tr.on('end', function () {
  43 + t.equal(data, html);
  44 + });
  45 +});

0 comments on commit dacad89

Please sign in to comment.
Something went wrong with that request. Please try again.