Skip to content

Commit

Permalink
expose JSONStream serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
max-mapper committed Jan 14, 2012
1 parent 8c995ae commit 2befe96
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
10 changes: 10 additions & 0 deletions README.markdown
Expand Up @@ -66,6 +66,16 @@ $ echo '[{"a":3},{"b":5},{"a":10,"b":3},{"b":55,"c":6}]' | node example/async.js
]
```

custom parsing and stringification
----------------------------------

see [JSONStream](https://github.com/dominictarr/JSONStream) for more details

transfuse(['rows', /./, 'doc'], function(doc, map) {
doc.pizza = "tacos"
map(doc)
}, JSONStream.stringify("{\"docs\":[\n", "\n,\n", "\n]}\n"));

methods
=======

Expand Down
12 changes: 7 additions & 5 deletions index.js
Expand Up @@ -3,17 +3,17 @@ var Stream = require('stream').Stream;
var es = require('event-stream');
var vm = require('vm');

var transfuse = module.exports = function (keyPath, fn) {
var transfuse = module.exports = function (keyPath, fn, stringify) {
if (typeof keyPath === 'function') {
fn = keyPath;
keyPath = [ /./ ];
}

if (typeof fn === 'function' && fn.length === 1) {
return transfuse.sync(keyPath, fn);
return transfuse.sync(keyPath, fn, stringify);
}
else {
return transfuse.async(keyPath, fn);
return transfuse.async(keyPath, fn, stringify);
}
};

Expand All @@ -26,7 +26,7 @@ transfuse.sync = transform(function (fn, doc, map) {
});

function transform (cb) {
return function (keyPath, fn) {
return function (keyPath, fn, stringify) {
if (fn === undefined) {
fn = keyPath;
keyPath = undefined;
Expand All @@ -41,6 +41,8 @@ function transform (cb) {
;
}

if (!stringify) stringify = JSONStream.stringify()

return es.connect(
JSONStream.parse(keyPath),
es.map(function (doc, map) {
Expand All @@ -49,7 +51,7 @@ function transform (cb) {
};
cb.call(context, fn, doc, map.bind(null, null));
}),
JSONStream.stringify()
stringify
);
};
}

0 comments on commit 2befe96

Please sign in to comment.