Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow recursive variable expansion #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 21 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,30 +51,34 @@ function Sentencer() {
// THE GOODS
// ---------------------------------------------

Sentencer.prototype.make = function(template) {
Sentencer.prototype.make = function(template, maxIterations) {
maxIterations = maxIterations || 1;
var self = this;

var sentence = template;
var occurrences = template.match(/\{\{(.+?)\}\}/g);

if(occurrences && occurrences.length) {
for(var i = 0; i < occurrences.length; i++) {
var action = occurrences[i].replace('{{', '').replace('}}', '').trim();
var result = '';
if(action.match(/\((.+?)\)/)) {
try {
result = eval('self.actions.' + action);
}
catch(e) { }
} else {
if(self.actions[action]) {
result = self.actions[action]();
for(var j = 0; j < maxIterations; j++) {
var occurrences = sentence.match(/\{\{(.+?)\}\}/g);

if(occurrences && occurrences.length) {
for(var i = 0; i < occurrences.length; i++) {
var action = occurrences[i].replace('{{', '').replace('}}', '').trim();
var result = '';
if(action.match(/\((.+?)\)/)) {
try {
result = eval('self.actions.' + action);
}
catch(e) { }
} else {
result = '{{ ' + action + ' }}';
if(self.actions[action]) {
result = self.actions[action]();
} else {
result = '{{ ' + action + ' }}';
}
}
sentence = sentence.replace(occurrences[i], result);
}
sentence = sentence.replace(occurrences[i], result);
}
else break;
}
return sentence;
};
Expand Down
29 changes: 28 additions & 1 deletion tests/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,35 @@ describe('Sentencer:', function() {

});

describe('# Recursive variable expansion', function() {

Sentencer.configure({
actions: {
recurse1: function() {
return "recurse1 ({{ recurse2 }})";
},
recurse2: function() {
return "recurse2 ({{ recurse3 }})";
},
recurse3: function() {
return "hello world";
}
}
});

it('should not contain variables with sufficient depth', function() {
assert.equal( Sentencer.make('{{ recurse1 }}',3), 'recurse1 (recurse2 (hello world))' );
});

it('should contain variables if depth is insufficient', function() {
assert.equal( Sentencer.make('{{ recurse1 }}',2), 'recurse1 (recurse2 ({{ recurse3 }}))' );
});

});

});


describe('Test Print', function() {

it('should have logged a sentence', function() {
Expand All @@ -132,4 +159,4 @@ describe('Sentencer:', function() {

});

});
});