From bff74c70e52ed5a881ebf2997025727d38776a59 Mon Sep 17 00:00:00 2001 From: Dan Sargeant Date: Wed, 26 Aug 2015 15:20:37 +1000 Subject: [PATCH] Option to remove BOM from start of file. Unit tests to ensure BOM removed --- index.js | 5 +++++ test/bom.hbs | 1 + test/bom_test.js | 28 ++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 test/bom.hbs create mode 100644 test/bom_test.js diff --git a/index.js b/index.js index 4e1dadb..0091ca6 100644 --- a/index.js +++ b/index.js @@ -60,6 +60,11 @@ function hbsfy(file, opts) { }, function() { var js; + + if (opts && (opts.b || opts.bom) && buffer.indexOf('') === 0) { + buffer = buffer.substring(1); + } + try { js = precompiler.precompile(buffer, opts && opts.precompilerOptions); } catch (e) { diff --git a/test/bom.hbs b/test/bom.hbs new file mode 100644 index 0000000..4875e8b --- /dev/null +++ b/test/bom.hbs @@ -0,0 +1 @@ +Hello {{msg}}! \ No newline at end of file diff --git a/test/bom_test.js b/test/bom_test.js new file mode 100644 index 0000000..d7b5836 --- /dev/null +++ b/test/bom_test.js @@ -0,0 +1,28 @@ + +var assert = require("assert"); +var concat = require("concat-stream"); +var fs = require("fs"); +var hbsfyBom = require("hbsfy"); +var hbsfyNoBom = require("hbsfy").configure({ + bom: true +}); + +var templatePath = __dirname + "/bom.hbs"; + +fs.createReadStream(templatePath) +.pipe(hbsfyBom(templatePath)) +.pipe(concat(function(data) { + assert( + //.test(data.toString()), + "The template should contain a bom" + ); +})); + +fs.createReadStream(templatePath) +.pipe(hbsfyNoBom(templatePath)) +.pipe(concat(function(data) { + assert( + //.test(data.toString()) === false, + "The template should not contain a bom" + ); +})); \ No newline at end of file