Permalink
Browse files

whitespace fix

  • Loading branch information...
1 parent 415b58c commit 4d49923cc0cde512a4a95464d87f3a96249eb417 @DTrejo committed Mar 27, 2011
Showing with 39 additions and 39 deletions.
  1. +30 −30 streamify.js
  2. +9 −9 test/test.js
View
@@ -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')
;
@@ -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);
+ }
});
};
@@ -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();
View
@@ -8,7 +8,7 @@ var assert = require('assert')
, size = 100
, big = []
;
-
+
while(size--) {
big.push(medium);
}
@@ -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();
@@ -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.