Skip to content

Commit

Permalink
doc and test for options.matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
billykong committed Apr 22, 2020
1 parent c316c3f commit 2294281
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
16 changes: 12 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 %>': {
Expand Down Expand Up @@ -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"
});
```
5 changes: 1 addition & 4 deletions simple_templating_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
};
Expand All @@ -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]}`);
Expand All @@ -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...");
Expand Down
13 changes: 12 additions & 1 deletion test/simple_templating_engine_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});

});

0 comments on commit 2294281

Please sign in to comment.