stream-handlebars
Advanced tools
Comparing version
@@ -1,107 +0,107 @@ | ||
"use strict"; | ||
var util = require("util"); | ||
var Transform = require("stream").Transform; | ||
var handlebarsMain = require("handlebars"); | ||
var o = require("object-tools"); | ||
'use strict' | ||
var util = require('util') | ||
var Transform = require('stream').Transform | ||
var handlebarsMain = require('handlebars') | ||
var o = require('object-tools') | ||
/** | ||
Extends handlebars with a streaming interface for .compile(). | ||
* Extends handlebars with a streaming interface for .compile(). | ||
* | ||
* @module stream-handlebars | ||
* @example | ||
* var handlebars = require('stream-handlebars') | ||
* var fs = require('fs') | ||
* | ||
* var template = '<p>\{{paragraph}}</p>' | ||
* | ||
* // it's just regular handlebars.. | ||
* handlebars.registerPartial('whatever', 'the partial content') | ||
* | ||
* // ..with the addition of a streaming interface for .compile() | ||
* var compileStream = handlebars.createCompileStream(template) | ||
* | ||
* // the template is compiled using the piped-in JSON as context | ||
* fs.createReadStream('./template-data.json', 'utf8') | ||
* .pipe(compileStream) | ||
* .pipe(process.stdout) | ||
*/ | ||
@module stream-handlebars | ||
@example | ||
var handlebars = require("stream-handlebars"); | ||
var fs = require("fs"); | ||
var template = "<p>\{{paragraph}}</p>" | ||
// it's just regular handlebars.. | ||
handlebars.registerPartial("whatever", "the partial content"); | ||
// ..with the addition of a streaming interface for .compile() | ||
var compileStream = handlebars.createCompileStream(template); | ||
// the template is compiled using the piped-in JSON as context | ||
fs.createReadStream("./template-data.json", "utf8") | ||
.pipe(compileStream) | ||
.pipe(process.stdout); | ||
*/ | ||
/** | ||
The regular [handlebars](http://handlebarsjs.com) module. | ||
@namespace | ||
@extends {external:handlebars} | ||
@alias module:stream-handlebars | ||
*/ | ||
var handlebars = Object.create(handlebarsMain); | ||
* The regular [handlebars](http://handlebarsjs.com) module. | ||
* @namespace | ||
* @extends {external:handlebars} | ||
* @alias module:stream-handlebars | ||
*/ | ||
var handlebars = Object.create(handlebarsMain) | ||
/** | ||
a stream wrapper for the [handlebars.compile](http://handlebarsjs.com/reference.html) function | ||
@param {string} - required template | ||
@param [options] {object} - options passed to both Transform() and .compile() | ||
@param [options.objectMode] {object} - set to true if you wish you pass in the data as an object | ||
@param [options.data] {object} - default data object | ||
@return {external:Transform} | ||
*/ | ||
handlebars.createCompileStream = function(template, options){ | ||
return new CompileStream(template, options); | ||
}; | ||
* a stream wrapper for the [handlebars.compile](http://handlebarsjs.com/reference.html) function | ||
* @param {string} - required template | ||
* @param [options] {object} - options passed to both Transform() and .compile() | ||
* @param [options.objectMode] {object} - set to true if you wish you pass in the data as an object | ||
* @param [options.data] {object} - default data object | ||
* @return {external:Transform} | ||
*/ | ||
handlebars.createCompileStream = function (template, options) { | ||
return new CompileStream(template, options) | ||
} | ||
function CompileStream(template, options){ | ||
Transform.call(this, options); | ||
function CompileStream (template, options) { | ||
Transform.call(this, options) | ||
this.buf = new Buffer(0); | ||
this.options = options || {}; | ||
this.template = template; | ||
this.buf = new Buffer(0) | ||
this.options = options || {} | ||
this.template = template | ||
} | ||
util.inherits(CompileStream, Transform); | ||
util.inherits(CompileStream, Transform) | ||
CompileStream.prototype._transform = function(chunk, enc, done){ | ||
if (chunk){ | ||
if (this._writableState.objectMode){ | ||
this.buf = chunk; | ||
} else { | ||
this.buf = Buffer.concat([ this.buf, chunk ]); | ||
} | ||
CompileStream.prototype._transform = function (chunk, enc, done) { | ||
if (chunk) { | ||
if (this._writableState.objectMode) { | ||
this.buf = chunk | ||
} else { | ||
this.buf = Buffer.concat([ this.buf, chunk ]) | ||
} | ||
done(); | ||
}; | ||
} | ||
done() | ||
} | ||
CompileStream.prototype._flush = function(){ | ||
var data; | ||
if (this._writableState.objectMode){ | ||
data = this.buf; | ||
CompileStream.prototype._flush = function () { | ||
var data | ||
if (this._writableState.objectMode) { | ||
data = this.buf | ||
} else { | ||
try { | ||
data = JSON.parse(this.buf) | ||
} catch(err) { | ||
err.message = util.format('[%s] %s', err.message, this.buf.toString()) | ||
return this.emit('error', err) | ||
} | ||
} | ||
try { | ||
if (Array.isArray(data)) { | ||
for (var prop in this.options.data) { | ||
data[prop] = this.options.data[prop] | ||
} | ||
} else { | ||
try { | ||
data = JSON.parse(this.buf); | ||
} catch(err){ | ||
err.message = util.format("[%s] %s", err.message, this.buf.toString()); | ||
return this.emit("error", err); | ||
} | ||
data = o.extend(this.options.data, data) | ||
} | ||
try { | ||
if (Array.isArray(data)){ | ||
for (var prop in this.options.data){ | ||
data[prop] = this.options.data[prop]; | ||
} | ||
} else { | ||
data = o.extend(this.options.data, data); | ||
} | ||
var result = handlebars.compile(this.template, this.options)(data); | ||
this.push(result); | ||
this.push(null); | ||
} catch(err){ | ||
return this.emit("error", err); | ||
} | ||
}; | ||
var result = handlebars.compile(this.template, this.options)(data) | ||
this.push(result) | ||
this.push(null) | ||
} catch(err) { | ||
return this.emit('error', err) | ||
} | ||
} | ||
module.exports = handlebars; | ||
module.exports = handlebars | ||
/** | ||
@external Transform | ||
@see https://nodejs.org/api/stream.html#stream_class_stream_transform | ||
*/ | ||
* @external Transform | ||
* @see https://nodejs.org/api/stream.html#stream_class_stream_transform | ||
*/ | ||
/** | ||
@external handlebars | ||
@see http://handlebarsjs.com | ||
*/ | ||
* @external handlebars | ||
* @see http://handlebarsjs.com | ||
*/ |
{ | ||
"name": "stream-handlebars", | ||
"author": "Lloyd Brookes <75pound@gmail.com>", | ||
"version": "0.1.6", | ||
"version": "0.2.0", | ||
"description": "Extends handlebars with a streaming interface for .compile()", | ||
@@ -18,14 +18,13 @@ "repository": "https://github.com/75lb/stream-handlebars.git", | ||
"test": "tape test/*.js", | ||
"lint": "jshint lib/*.js test/*.js; echo", | ||
"docs": "jsdoc2md -t jsdoc2md/README.hbs -l js lib/*.js > README.md; echo" | ||
}, | ||
"dependencies": { | ||
"handlebars": "^3.0.0", | ||
"object-tools": "^1.2.1" | ||
"handlebars": "^4.0.5", | ||
"object-tools": "^2.0.6" | ||
}, | ||
"devDependencies": { | ||
"jsdoc-to-markdown": "^1.1.1", | ||
"tape": "^4" | ||
"jsdoc-to-markdown": "^1.3.6", | ||
"tape": "^4.6.0" | ||
}, | ||
"license": "MIT" | ||
} |
@@ -5,4 +5,6 @@ [](https://www.npmjs.org/package/stream-handlebars) | ||
[](https://david-dm.org/75lb/stream-handlebars) | ||
[](https://github.com/feross/standard) | ||
<a name="module_stream-handlebars"></a> | ||
## stream-handlebars | ||
@@ -13,24 +15,25 @@ Extends handlebars with a streaming interface for .compile(). | ||
```js | ||
var handlebars = require("stream-handlebars"); | ||
var fs = require("fs"); | ||
var handlebars = require('stream-handlebars') | ||
var fs = require('fs') | ||
var template = "<p>\{{paragraph}}</p>" | ||
var template = '<p>\{{paragraph}}</p>' | ||
// it's just regular handlebars.. | ||
handlebars.registerPartial("whatever", "the partial content"); | ||
handlebars.registerPartial('whatever', 'the partial content') | ||
// ..with the addition of a streaming interface for .compile() | ||
var compileStream = handlebars.createCompileStream(template); | ||
var compileStream = handlebars.createCompileStream(template) | ||
// the template is compiled using the piped-in JSON as context | ||
fs.createReadStream("./template-data.json", "utf8") | ||
fs.createReadStream('./template-data.json', 'utf8') | ||
.pipe(compileStream) | ||
.pipe(process.stdout); | ||
.pipe(process.stdout) | ||
``` | ||
* [stream-handlebars](#module_stream-handlebars) | ||
* [handlebars](#exp_module_stream-handlebars--handlebars) : <code>object</code> ⏏ | ||
* [.createCompileStream(template, [options])](#module_stream-handlebars--handlebars.createCompileStream) ⇒ <code>[Transform](https://nodejs.org/api/stream.html#stream_class_stream_transform)</code> | ||
* [handlebars](#exp_module_stream-handlebars--handlebars) : <code>object</code> ⏏ | ||
* [.createCompileStream(template, [options])](#module_stream-handlebars--handlebars.createCompileStream) ⇒ <code>[Transform](https://nodejs.org/api/stream.html#stream_class_stream_transform)</code> | ||
<a name="exp_module_stream-handlebars--handlebars"></a> | ||
### handlebars : <code>object</code> ⏏ | ||
@@ -42,2 +45,3 @@ The regular [handlebars](http://handlebarsjs.com) module. | ||
<a name="module_stream-handlebars--handlebars.createCompileStream"></a> | ||
#### handlebars.createCompileStream(template, [options]) ⇒ <code>[Transform](https://nodejs.org/api/stream.html#stream_class_stream_transform)</code> | ||
@@ -58,2 +62,2 @@ a stream wrapper for the [handlebars.compile](http://handlebarsjs.com/reference.html) function | ||
© 2015 Lloyd Brookes \<75pound@gmail.com\>. Documented by [jsdoc-to-markdown](https://github.com/75lb/jsdoc-to-markdown). | ||
© 2015-16 Lloyd Brookes \<75pound@gmail.com\>. Documented by [jsdoc-to-markdown](https://github.com/75lb/jsdoc-to-markdown). |
233
test/test.js
@@ -1,125 +0,126 @@ | ||
var test = require("tape"); | ||
var streamHandlebars = require("../"); | ||
'use strict' | ||
var test = require('tape') | ||
var streamHandlebars = require('../') | ||
test("regular handlebars API exists", function(t){ | ||
t.ok(streamHandlebars.registerPartial); | ||
t.ok(streamHandlebars.compile); | ||
t.end(); | ||
}); | ||
test('regular handlebars API exists', function (t) { | ||
t.ok(streamHandlebars.registerPartial) | ||
t.ok(streamHandlebars.compile) | ||
t.end() | ||
}) | ||
test("events", function(t){ | ||
t.plan(3); | ||
var data = '{ "message": "test, yeah?" }'; | ||
var compileStream = streamHandlebars.createCompileStream("result: {{message}}"); | ||
compileStream.on("end", t.pass.bind(null, "end fired")); | ||
compileStream.on("finish", t.pass.bind(null, "finish fired")); | ||
compileStream.on("readable", function(){ | ||
if (this.read()){ | ||
t.pass("readable fired"); | ||
} | ||
}); | ||
compileStream.end(data); | ||
}); | ||
test('events', function (t) { | ||
t.plan(3) | ||
var data = '{ "message": "test, yeah?" }' | ||
var compileStream = streamHandlebars.createCompileStream('result: {{message}}') | ||
compileStream.on('end', t.pass.bind(null, 'end fired')) | ||
compileStream.on('finish', t.pass.bind(null, 'finish fired')) | ||
compileStream.on('readable', function () { | ||
if (this.read()) { | ||
t.pass('readable fired') | ||
} | ||
}) | ||
compileStream.end(data) | ||
}) | ||
test("text interface", function(t){ | ||
t.plan(1); | ||
var data = '{ "message": "test, yeah?" }'; | ||
var compileStream = streamHandlebars.createCompileStream("result: {{message}}"); | ||
compileStream.on("readable", function(){ | ||
var chunk = this.read(); | ||
if (chunk){ | ||
t.strictEqual(chunk.toString(), "result: test, yeah?"); | ||
} | ||
}) | ||
compileStream.end(data); | ||
}); | ||
test('text interface', function (t) { | ||
t.plan(1) | ||
var data = '{ "message": "test, yeah?" }' | ||
var compileStream = streamHandlebars.createCompileStream('result: {{message}}') | ||
compileStream.on('readable', function () { | ||
var chunk = this.read() | ||
if (chunk) { | ||
t.strictEqual(chunk.toString(), 'result: test, yeah?') | ||
} | ||
}) | ||
compileStream.end(data) | ||
}) | ||
test("text interface with bad JSON", function(t){ | ||
t.plan(1); | ||
var data = 'kjsdhfkashdflk'; | ||
var compileStream = streamHandlebars.createCompileStream("a template"); | ||
compileStream.on("error", function(err){ | ||
t.pass("failed, correctly: " + err.message); | ||
}) | ||
compileStream.end(data); | ||
}); | ||
test('text interface with bad JSON', function (t) { | ||
t.plan(1) | ||
var data = 'kjsdhfkashdflk' | ||
var compileStream = streamHandlebars.createCompileStream('a template') | ||
compileStream.on('error', function (err) { | ||
t.pass('failed, correctly: ' + err.message) | ||
}) | ||
compileStream.end(data) | ||
}) | ||
test("text interface with default data", function(t){ | ||
t.plan(1); | ||
var input = '{ "message": "test, yeah?" }'; | ||
var template = "result {{one}}: {{message}}"; | ||
var compileStream = streamHandlebars.createCompileStream(template, { data: { one: 1 }}); | ||
compileStream.on("readable", function(){ | ||
var chunk = this.read(); | ||
if (chunk){ | ||
t.strictEqual(chunk.toString(), "result 1: test, yeah?"); | ||
} | ||
}); | ||
compileStream.end(input); | ||
}); | ||
test('text interface with default data', function (t) { | ||
t.plan(1) | ||
var input = '{ "message": "test, yeah?" }' | ||
var template = 'result {{one}}: {{message}}' | ||
var compileStream = streamHandlebars.createCompileStream(template, { data: { one: 1 }}) | ||
compileStream.on('readable', function () { | ||
var chunk = this.read() | ||
if (chunk) { | ||
t.strictEqual(chunk.toString(), 'result 1: test, yeah?') | ||
} | ||
}) | ||
compileStream.end(input) | ||
}) | ||
test("objectMode", function(t){ | ||
t.plan(1); | ||
var data = { message: "objectMode", other: "something" }; | ||
var template = "one: {{message}}, two: {{other}}"; | ||
var compileStream = streamHandlebars.createCompileStream( | ||
template, | ||
{ objectMode: true } | ||
); | ||
compileStream.on("readable", function(){ | ||
var chunk = this.read(); | ||
if (chunk){ | ||
t.strictEqual(chunk.toString(), "one: objectMode, two: something"); | ||
} | ||
}) | ||
compileStream.end(data); | ||
}); | ||
test('objectMode', function (t) { | ||
t.plan(1) | ||
var data = { message: 'objectMode', other: 'something' } | ||
var template = 'one: {{message}}, two: {{other}}' | ||
var compileStream = streamHandlebars.createCompileStream( | ||
template, | ||
{ objectMode: true } | ||
) | ||
compileStream.on('readable', function () { | ||
var chunk = this.read() | ||
if (chunk) { | ||
t.strictEqual(chunk.toString(), 'one: objectMode, two: something') | ||
} | ||
}) | ||
compileStream.end(data) | ||
}) | ||
test("objectMode with default data", function(t){ | ||
t.plan(1); | ||
var data = { message: "objectMode", other: "something" }; | ||
var template = "one: {{message}}, two: {{other}}, three: {{three}}"; | ||
var compileStream = streamHandlebars.createCompileStream( | ||
template, | ||
{ objectMode: true, data: { three: "what" } } | ||
); | ||
compileStream.on("readable", function(){ | ||
var chunk = this.read(); | ||
if (chunk){ | ||
t.strictEqual(chunk.toString(), "one: objectMode, two: something, three: what"); | ||
} | ||
}) | ||
compileStream.end(data); | ||
}); | ||
test('objectMode with default data', function (t) { | ||
t.plan(1) | ||
var data = { message: 'objectMode', other: 'something' } | ||
var template = 'one: {{message}}, two: {{other}}, three: {{three}}' | ||
var compileStream = streamHandlebars.createCompileStream( | ||
template, | ||
{ objectMode: true, data: { three: 'what' } } | ||
) | ||
compileStream.on('readable', function () { | ||
var chunk = this.read() | ||
if (chunk) { | ||
t.strictEqual(chunk.toString(), 'one: objectMode, two: something, three: what') | ||
} | ||
}) | ||
compileStream.end(data) | ||
}) | ||
test("objectMode with array input", function(t){ | ||
t.plan(1); | ||
var data = [{ number: 1 }, { number: 2 }]; | ||
var template = "{{#each this}}{{number}}{{/each}}"; | ||
var compileStream = streamHandlebars.createCompileStream( | ||
template, { objectMode: true } | ||
); | ||
compileStream.on("readable", function(){ | ||
var chunk = this.read(); | ||
if (chunk){ | ||
t.strictEqual(chunk.toString(), "12"); | ||
} | ||
}) | ||
compileStream.end(data); | ||
}); | ||
test('objectMode with array input', function (t) { | ||
t.plan(1) | ||
var data = [{ number: 1 }, { number: 2 }] | ||
var template = '{{#each this}}{{number}}{{/each}}' | ||
var compileStream = streamHandlebars.createCompileStream( | ||
template, { objectMode: true } | ||
) | ||
compileStream.on('readable', function () { | ||
var chunk = this.read() | ||
if (chunk) { | ||
t.strictEqual(chunk.toString(), '12') | ||
} | ||
}) | ||
compileStream.end(data) | ||
}) | ||
test("objectMode with array input and default options", function(t){ | ||
t.plan(1); | ||
var data = [{ number: 1 }, { number: 2 }]; | ||
var template = "{{#each this}}{{number}}{{/each}}{{yeah}}"; | ||
var compileStream = streamHandlebars.createCompileStream( | ||
template, { objectMode: true, data: { yeah: "what" } } | ||
); | ||
compileStream.on("readable", function(){ | ||
var chunk = this.read(); | ||
if (chunk){ | ||
t.strictEqual(chunk.toString(), "12what"); | ||
} | ||
}) | ||
compileStream.end(data); | ||
}); | ||
test('objectMode with array input and default options', function (t) { | ||
t.plan(1) | ||
var data = [{ number: 1 }, { number: 2 }] | ||
var template = '{{#each this}}{{number}}{{/each}}{{yeah}}' | ||
var compileStream = streamHandlebars.createCompileStream( | ||
template, { objectMode: true, data: { yeah: 'what' } } | ||
) | ||
compileStream.on('readable', function () { | ||
var chunk = this.read() | ||
if (chunk) { | ||
t.strictEqual(chunk.toString(), '12what') | ||
} | ||
}) | ||
compileStream.end(data) | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
214
2.88%60
7.14%12027
-3.95%8
-11.11%1
Infinity%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
Updated
Updated