| var localRegex = new RegExp(regStr); | var localRegex = new RegExp(regStr); | ||||
| var globalRegex = new RegExp(regStr, "g"); | var globalRegex = new RegExp(regStr, "g"); | ||||
| function expectType(type, val) { | |||||
| if (typeof val !== type) | |||||
| throw new TypeError("Expected "+type+", got "+typeof val); | |||||
| } | |||||
| function expectInstance(obj, val) { | |||||
| if (!(val instanceof obj)) { | |||||
| var regex = /function (.+)\(/; | |||||
| var expectedName = obj.toString().match(regex)[1]; | |||||
| try { | |||||
| var realName = val.constructor.toString().match(regex)[1]; | |||||
| } catch (err) { | |||||
| var realName = "undefined"; | |||||
| } | |||||
| throw new TypeError("Expected "+expectedName+", got "+realName); | |||||
| } | |||||
| } | |||||
| function hasMultipleStatements(str) { | function hasMultipleStatements(str) { | ||||
| return (str.indexOf(";") !== -1) | return (str.indexOf(";") !== -1) | ||||
| } | } | ||||
| Func.prototype = Function.prototype; | Func.prototype = Function.prototype; | ||||
| function Templish(str, vals) { | function Templish(str, vals) { | ||||
| expectType("string", str); | |||||
| expectInstance(Array, vals); | |||||
| str = str.replace(/\s+/g, " ").replace(/}}/g, "}}\n"); | str = str.replace(/\s+/g, " ").replace(/}}/g, "}}\n"); | ||||
| this.string = str; | this.string = str; | ||||
| this.vals = vals; | this.vals = vals; | ||||
| } | } | ||||
| Templish.prototype.exec = function(vals, cb) { | Templish.prototype.exec = function(vals, cb) { | ||||
| expectInstance(Object, vals); | |||||
| expectType("function", cb); | |||||
| var str = this.string; | var str = this.string; | ||||
| //Prepare generic arguments | //Prepare generic arguments |
| { | { | ||||
| "name": "templish", | "name": "templish", | ||||
| "version": "0.0.2", | |||||
| "version": "0.0.3", | |||||
| "description": "HTML templating library for JS", | "description": "HTML templating library for JS", | ||||
| "main": "index.js", | "main": "index.js", | ||||
| "scripts": { | "scripts": { | ||||
| "test": "echo \"Error: no test specified\" && exit 1" | |||||
| "test": "mocha test" | |||||
| }, | }, | ||||
| "author": "Martin Dørum Nygaard", | "author": "Martin Dørum Nygaard", | ||||
| "license": "MIT", | "license": "MIT", |
| var assert = require("assert"); | |||||
| var Templish = require("../index.js"); | |||||
| describe("templish", function() { | |||||
| it("does basic substitution", function(done) { | |||||
| var t = new Templish("{{conf.foo}}", ["conf"]); | |||||
| t.exec({conf: {foo: "bar"}}, function(err, res) { | |||||
| assert.equal(res, "bar"); | |||||
| done(err); | |||||
| }); | |||||
| }); | |||||
| it("does basic async substitution", function(done) { | |||||
| var t = new Templish("{{cb(null, 'hi')}}", []); | |||||
| t.exec({}, function(err, res) { | |||||
| assert.equal(res, "hi"); | |||||
| done(err); | |||||
| }); | |||||
| }); | |||||
| it("does a mix of async and sync substitution", function(done) { | |||||
| var t = new Templish("{{'a'}} {{cb(null, 'b')}} {{'c'}} {{cb(null, 'd')}}", []); | |||||
| t.exec({}, function(err, res) { | |||||
| assert.equal(res, "a b c d"); | |||||
| done(err); | |||||
| }); | |||||
| }); | |||||
| it("doesn't break when the result of a substitution looks like a template", function(done) { | |||||
| var t = new Templish("foo: {{foo}}, again: {{foo}}", ["foo"]); | |||||
| t.exec({foo: "{{foo}}"}, function(err, res) { | |||||
| assert.equal(res, "foo: {{foo}}, again: {{foo}}"); | |||||
| done(err); | |||||
| }); | |||||
| }); | |||||
| }); |