Permalink
Browse files

return string mode

  • Loading branch information...
1 parent 94f5be5 commit d8aaea6f70119817431734259a62c8a13669278e @substack committed Feb 13, 2012
Showing with 80 additions and 21 deletions.
  1. +37 −14 index.js
  2. +43 −7 test/html.js
View
@@ -3,16 +3,34 @@ var Stream = require('stream').Stream;
var charm = require('charm');
var deepEqual = require('deep-equal');
-module.exports = function (opts) {
- return difflet.bind(null, opts);
+module.exports = function (opts_) {
+ var fn = difflet.bind(null, opts_);
+ fn.compare = function (prev, next) {
+ var opts = Object.keys(opts_ || {}).reduce(function (acc, key) {
+ acc[key] = opts_[key];
+ return acc;
+ }, {});
+ var s = opts.stream = new Stream;
+ var data = '';
+ s.write = function (buf) { data += buf };
+ s.end = function () {};
+ s.readable = true;
+ s.writable = true;
+
+ difflet(opts, prev, next);
+ return data;
+ };
+ return fn;
};
function difflet (opts, prev, next) {
- var stream = new Stream;
- stream.readable = true;
- stream.writable = true;
- stream.write = function (buf) { this.emit('data', buf) };
- stream.end = function () { this.emit('end') };
+ var stream = opts.stream || new Stream;
+ if (!opts.stream) {
+ stream.readable = true;
+ stream.writable = true;
+ stream.write = function (buf) { this.emit('data', buf) };
+ stream.end = function () { this.emit('end') };
+ }
if (!opts) opts = {};
if (opts.start === undefined && opts.stop === undefined) {
@@ -28,9 +46,11 @@ function difflet (opts, prev, next) {
c.display('reset');
};
}
- var write = opts.write || function (buf) {
- stream.write(buf);
+ var write = function (buf) {
+ if (opts.write) opts.write(buf, stream)
+ else s.write(buf)
};
+
var commaFirst = opts.comma === 'first';
var indent = opts.indent;
@@ -41,11 +61,6 @@ function difflet (opts, prev, next) {
return stringifier.call(this, false, node);
};
- process.nextTick(function () {
- traverse(next).forEach(stringify);
- stream.emit('end');
- });
-
var levels = 0;
function set (type) {
if (levels === 0) opts.start(type, stream);
@@ -259,6 +274,14 @@ function difflet (opts, prev, next) {
}
}
+ if (opts.stream) {
+ traverse(next).forEach(stringify);
+ }
+ else process.nextTick(function () {
+ traverse(next).forEach(stringify);
+ stream.emit('end');
+ });
+
return stream;
}
View
@@ -2,21 +2,23 @@ var difflet = require('../');
var test = require('tap').test;
var ent = require('ent');
+var tags = {
+ inserted : 'g',
+ updated : 'b',
+ deleted : 'r',
+};
+
test('html output', function (t) {
- var tags = {
- inserted : 'g',
- updated : 'b',
- deleted : 'r',
- };
+ t.plan(1);
var diff = difflet({
start : function (t, s) {
s.write('<' + tags[t] + '>');
},
stop : function (t, s) {
s.write('</' + tags[t] + '>');
},
- write : function (buf) {
- stream.write(ent.encode(buf));
+ write : function (buf, s) {
+ s.write(ent.encode(buf));
}
});
@@ -42,3 +44,37 @@ test('html output', function (t) {
t.end();
});
});
+
+test('compare html output', function (t) {
+ t.plan(1);
+
+ var diff = difflet({
+ start : function (t, s) {
+ s.write('<' + tags[t] + '>');
+ },
+ stop : function (t, s) {
+ s.write('</' + tags[t] + '>');
+ },
+ write : function (buf, s) {
+ s.write(ent.encode(buf));
+ }
+ });
+
+ var data = diff.compare(
+ { yy : 6, zz : 5, a : [1,2,3], fn : function qqq () {} },
+ {
+ a : [ 1, 2, 3, [4], "z", /beep/, new Buffer([0,1,2]) ],
+ fn : function rrr () {},
+ b : [5,6,7]
+ }
+ );
+
+ t.equal(data,
+ '{&quot;a&quot;:[1,2,3,<g>[4]</g>,<g>&quot;z&quot;</g>,'
+ + '<g>/beep/</g>,<g>&lt;Buffer 00 01 02&gt;</g>],'
+ + '&quot;fn&quot;:<b>[Function: rrr]</b>,<g>'
+ + '&quot;b&quot;:[5,6,7]</g>,<r>&quot;yy&quot;:6,'
+ + '&quot;zz&quot;:5</r>}'
+ );
+ t.end();
+});

0 comments on commit d8aaea6

Please sign in to comment.