Skip to content

Commit

Permalink
whitespace fix
Browse files Browse the repository at this point in the history
  • Loading branch information
DTrejo committed Mar 27, 2011
1 parent 415b58c commit 4d49923
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 39 deletions.
60 changes: 30 additions & 30 deletions streamify.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
//
// Pretty much all written by James Halliday (substack.net)
// Small bits by David Trejo (dtrejo.com)
//
//
var Traverse = require('traverse')
, fs = require('fs')
;
Expand All @@ -11,33 +11,33 @@ var Traverse = require('traverse')
exports.streamify = function streamify(obj, emit) {

Traverse(obj).forEach(function to_s (node) {
if (Array.isArray(node)) {
this.before(function () { emit('['); });
this.post(function (child) {
if (!child.isLast) emit(',');
});
this.after(function () { emit(']'); });
}
else if (typeof node == 'object') {
this.before(function () { emit('{'); });
this.pre(function (x, key) {
to_s(key);
emit(':');
});
this.post(function (child) {
if (!child.isLast) emit(',');
});
this.after(function () { emit('}'); });
}
else if (typeof node == 'string') {
emit('"' + node.toString().replace(/"/g, '\\"') + '"');
}
else if (typeof node == 'function') {
emit('null');
}
else {
emit(node.toString());
}
if (Array.isArray(node)) {
this.before(function () { emit('['); });
this.post(function (child) {
if (!child.isLast) emit(',');
});
this.after(function () { emit(']'); });

} else if (typeof node == 'object') {
this.before(function () { emit('{'); });
this.pre(function (x, key) {
to_s(key);
emit(':');
});
this.post(function (child) {
if (!child.isLast) emit(',');
});
this.after(function () { emit('}'); });

} else if (typeof node == 'string') {
emit(JSON.stringify(node));

} else if (typeof node == 'function') {
emit('null');

} else {
emit(node);
}
});
};

Expand All @@ -51,7 +51,7 @@ exports.streamingWrite = function streamingWrite(path, object, cb) {
exports.streamify(object, function(chunk) {
stream.write(chunk);
});

// all writes have been sent, b/c streamify is a sync function.
stream.end();

Expand Down
18 changes: 9 additions & 9 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var assert = require('assert')
, size = 100
, big = []
;

while(size--) {
big.push(medium);
}
Expand All @@ -17,15 +17,15 @@ module.exports = testCase({
test_small_object: function(assert) {
var str = '';
streamify(small, function(data) { str += data; });

assert.equals(JSON.stringify(small), str
, 'JSON.stringify and streamify return the same string for a small object');
assert.done();
}
, test_big_object: function(assert) {
, test_big_object: function(assert) {
var str = '';
streamify(big, function(data) { str += data; });

assert.equals(JSON.stringify(big), str
, 'JSON.stringify and streamify return the same string for a big object');
assert.done();
Expand All @@ -45,22 +45,22 @@ module.exports = testCase({
assert.equals(JSON.stringify(big), fs.readFileSync(path).toString('utf8')
, 'Streaming write to file with big object returns same as JSON.stringify');
fs.unlinkSync(path);
assert.done();
assert.done();
});
}
, test_encode_object: function(assert) {
, test_encode_object: function(assert) {
var s = '';
for (var i = 0; i < 255; i++) { s += String.fromCharCode(i); }
var obj = { "a tricky string": s };
var str = ''
streamify(obj, function(data) { str += data; });

assert.equals(JSON.stringify(obj), str
, 'Properly escapes strings');

assert.equals(JSON.parse(JSON.stringify(obj)), JSON.parse(str)
, 'Properly parses strings');
assert.done();
}

});

0 comments on commit 4d49923

Please sign in to comment.