Create a gist now

Instantly share code, notes, and snippets.

@jakearchibald /parse-json.js Secret
Last active Dec 10, 2016

export default function parseJSON() {
return new TransformStream({
transform(chunk, controller) {
controller.enqueue(JSON.parse(chunk));
}
});
}
export default function splitStream(splitOn) {
let buffer = '';
return new TransformStream({
transform(chunk, controller) {
buffer += chunk;
const parts = buffer.split(splitOn);
parts.slice(0, -1).forEach(part => controller.enqueue(part));
buffer = parts[parts.length - 1];
},
flush(controller) {
if (buffer) controller.enqueue(buffer);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment