Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

tests, readme

  • Loading branch information...
commit 557f2d3855b04f1cbfc4d0f2ebc94888e1a3d9f7 1 parent a84e056
@DTrejo authored
View
15 examples/example.js
@@ -0,0 +1,15 @@
+var assert = require('assert')
+ , streamify = require('../streamify').streamify
+ , streamingWrite = require('../streamify').streamingWrite
+ , fs = require('fs')
+ , object = { a: 1, b: 2, c: 3}
+
+var str = '';
+streamify(object, function(data) { str += data; });
+console.log(str);
+// => {"a":1,"b":2,"c":3}
+
+var str = '';
+streamingWrite('./ACOOLFILE.txt', object, function(data) { str += data; });
+console.log(fs.readFileSync('./ACOOLFILE'));
+// => {"a":1,"b":2,"c":3}
View
40 test/no-file-test.js
@@ -1,40 +0,0 @@
-var fs = require('fs')
- , path = './DELETEME.txt'
- , stream = fs.createWriteStream(path, { flags: 'w+', encoding: 'utf8' })
- , data = { "I":0.18181818181818182,"bought":0.18181818181818182 }
- ;
-
-function cb(err) {
- if (err) console.log(err);
- console.log('cool, all done.');
-}
-
-stream.on('close', cb);
-stream.on('error', cb);
-
-var arr = [ JSON.stringify(data)
- , '\n'
- , JSON.stringify(data)
- , '\n'
- ];
-
-// doesn't work:
-// (function(arr, fn){
-// for (var i = 0; i < arr.length; i++) {
-// fn(arr[i]);
-// }
-// })(arr, stream.write);
-
-// however, this works:
-
-stream.write(JSON.stringify(data));
-stream.write('\n');
-stream.write(JSON.stringify(data));
-stream.write('\n');
-stream.write(JSON.stringify(data));
-
-// Wierd eh?
-
-
-// all writes have been sent
-stream.end();
View
35 test/readme.md
@@ -0,0 +1,35 @@
+# JSON Streamify
+
+A streaming version of `JSON.stringify`.
+
+## Methods
+
+### `streamify(object, function)`
+
+Passes the function chunks of the object until there is none of the object left.
+`streamify` is a synchronous function.
+
+ var assert = require('assert')
+ , streamify = require('../streamify').streamify
+ , streamingWrite = require('../streamify').streamingWrite
+ , fs = require('fs')
+ , object = { a: 1, b: 2, c: 3}
+
+ var str = '';
+ streamify(object, function(data) { str += data; });
+ console.log(str);
+ // => {"a":1,"b":2,"c":3}
+
+### `streamingWrite(filepath, object, callback)`
+
+Writes chunks of the object to the given filepath until there is none of the object left.
+When it finishes, it calls `callback`.
+
+ var str = '';
+ streamingWrite('./ACOOLFILE.txt', object, function(data) { str += data; }, function() {
+
+ console.log(fs.readFileSync('./ACOOLFILE'));
+ // => {"a":1,"b":2,"c":3}
+ });
+
+Most of this was written by [James Halliday](http://substack.net), with a few small things by [David Trejo](http://dtrejo.com/).
View
8 test/test.js
@@ -35,17 +35,17 @@ module.exports = testCase({
streamingWrite(path, small, function() {
assert.equals(JSON.stringify(small), fs.readFileSync(path).toString('utf8')
, 'Streaming write to file with small object returns same as JSON.stringify');
+ fs.unlinkSync(path);
+ assert.done();
});
- fs.unlinkSync(path);
- assert.done();
}
, test_big_streaming_write: function(assert) {
path = './DELETEME.txt';
streamingWrite(path, big, function() {
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();
});
- fs.unlinkSync(path);
- assert.done();
}
});
Please sign in to comment.
Something went wrong with that request. Please try again.