From 2294281b32ed844ca75aaf5a2611d6fcd55e4100 Mon Sep 17 00:00:00 2001 From: Billy Kong Date: Wed, 22 Apr 2020 20:42:00 +0800 Subject: [PATCH] doc and test for options.matcher --- readme.md | 16 ++++++++++++---- simple_templating_engine.js | 5 +---- test/simple_templating_engine_test.js | 13 ++++++++++++- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/readme.md b/readme.md index 9a1e368..0768b27 100644 --- a/readme.md +++ b/readme.md @@ -10,14 +10,12 @@ ```JavaScript var templeteEngine = require('./simple_templating_engine.js'); var template = "Hello, <% change_me %>"; - - var handlerWorld = function(key) { return { '<% change_me %>': 'World' } }; + templeteEngine.populate(template, handlerWorld).then(populated => { console.log(populated); // "Hello, World" }); - var handlerAsyncWorld = function(key) { return { '<% change_me %>': 'Async World' } }; (async function() { let populated = await templeteEngine.populate(template, handlerAsyncWorld); @@ -29,7 +27,6 @@ var handlerAsyncWorld = function(key) { return { '<% change_me %>': 'Async World It also work with JSON template: ```JavaScript var templateJSON = `{ "root": <% change_me %> }`; - var handlerJSON = function(key) { return { '<% change_me %>': { @@ -59,3 +56,14 @@ templeteEngine.populate(templateJSON, handlerJSON).then(populated => { console.log(populated); }); ``` + +You can also use another matcher pattern other than `<% ... %>`: +```JavaScript +var template = "Hello, {_ change_me _}"; +var options = { matcher: /"{_([^%>]+)?_}"/g} +var handler = function(key) { return { '{_ change_me _}': 'World' } }; + +templeteEngine.populate(template, handler, options).then(populated => { + console.log(populated); // "Hello, World" +}); +``` diff --git a/simple_templating_engine.js b/simple_templating_engine.js index e67c6ce..22481e6 100644 --- a/simple_templating_engine.js +++ b/simple_templating_engine.js @@ -10,7 +10,7 @@ function parse(template, options={}) { let re = options.matcher || /"<%([^%>]+)?%>"/g; let match; while(match = re.exec(template)) { - keys.push(match[0]); + keys.push(match[0]); // this line is never executed } return [template, keys]; }; @@ -23,8 +23,6 @@ function merge(template, data, options={}) { // console.log(data) console.log("\nPopulating...") Object.keys(data).forEach(key => { - let re = options.matcher || /"<%([^%>]+)?%>"/g; - let match; while(template.includes(key)) { if (typeof data[key] === 'string') { console.log(`- mapping ${key} to ${data[key]}`); @@ -33,7 +31,6 @@ function merge(template, data, options={}) { console.log(`- mapping ${key} to ${JSON.stringify(data[key])}`); template = template.replace(key, JSON.stringify(data[key])); } - } }); console.log("\nFinishing..."); diff --git a/test/simple_templating_engine_test.js b/test/simple_templating_engine_test.js index f33d944..804c6d7 100644 --- a/test/simple_templating_engine_test.js +++ b/test/simple_templating_engine_test.js @@ -55,8 +55,19 @@ describe('templateEngine#populate()', function() { let populated = await templeteEngine.populate(template, handler); assert(_.isEqual(JSON.parse(populated), { root: { key1: 'value1', key2: 'value2' }})); }); + }); + context('with custom matcher regex in options', function() { + let template, options, handler; + beforeEach(function() { + template = "Hello, {_ change_me _}"; + options = { matcher: /"{_([^%>]+)?_}"/g} + handler = function(key) { return { '{_ change_me _}': 'World' } } + }); + it('should replace the placeholder with the async handler', async function() { + let populated = await templeteEngine.populate(template, handler, options); + assert.equal(populated, 'Hello, World'); + }); }); - }); \ No newline at end of file