From 799b5f8afa3d380e21975cb55373c394e2fb4dc5 Mon Sep 17 00:00:00 2001 From: shawrkbait <43147032+shawrkbait@users.noreply.github.com> Date: Fri, 9 Nov 2018 10:13:21 -0600 Subject: [PATCH 1/2] Allow recursive variable expansion --- index.js | 37 ++++++++++++++++++++----------------- tests/main.js | 29 ++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index 03ec9ad..24711b3 100644 --- a/index.js +++ b/index.js @@ -51,30 +51,33 @@ function Sentencer() { // THE GOODS // --------------------------------------------- -Sentencer.prototype.make = function(template) { +Sentencer.prototype.make = function(template, 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; }; diff --git a/tests/main.js b/tests/main.js index 2f1f1a1..1334d21 100644 --- a/tests/main.js +++ b/tests/main.js @@ -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() { @@ -132,4 +159,4 @@ describe('Sentencer:', function() { }); -}); \ No newline at end of file +}); From 813719326f6bd0b508634b364baa8ec472112e39 Mon Sep 17 00:00:00 2001 From: shawrkbait <43147032+shawrkbait@users.noreply.github.com> Date: Fri, 9 Nov 2018 11:01:49 -0600 Subject: [PATCH 2/2] Support old node versions --- index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 24711b3..24ab252 100644 --- a/index.js +++ b/index.js @@ -51,7 +51,8 @@ function Sentencer() { // THE GOODS // --------------------------------------------- -Sentencer.prototype.make = function(template, maxIterations = 1) { +Sentencer.prototype.make = function(template, maxIterations) { + maxIterations = maxIterations || 1; var self = this; var sentence = template;