Skip to content

Commit

Permalink
Merge pull request #2 from rmtuckerphx/custom-lists
Browse files Browse the repository at this point in the history
Resolve kylestetz#16
  • Loading branch information
rmtuckerphx authored Apr 21, 2020
2 parents 55da186 + a25b1db commit bbadbab
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 69 deletions.
36 changes: 18 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ var _ = require('lodash');
function Sentencer() {
var self = this;

self._nouns = require('./words/nouns.js');
self._nouns = require('./words/nouns.js');
self._adjectives = require('./words/adjectives.js');

self.actions = {
noun: function () {
noun: function() {
return randy.choice(self._nouns);
},
a_noun: function () {
a_noun: function() {
return articles.articlize(self.actions.noun());
},
nouns: function () {
nouns: function() {
return nounInflector.pluralize(randy.choice(self._nouns));
},
adjective: function () {
adjective: function() {
return randy.choice(self._adjectives);
},
an_adjective: function () {
an_adjective: function() {
return articles.articlize(self.actions.adjective());
}
};
Expand All @@ -43,12 +43,12 @@ function Sentencer() {
return nounInflector.pluralize( randy.choice(values) );
}

self.configure = function (options) {
self.configure = function(options) {
// merge actions
self.actions = _.merge(self.actions, options.actions || {});
self.actions = _.merge(self.actions, options.actions || {});
// overwrite nouns and adjectives if we got some
self._nouns = options.nounList || self._nouns;
self._adjectives = options.adjectiveList || self._adjectives;
self._nouns = options.nounList || self._nouns;
self._adjectives = options.adjectiveList || self._adjectives;
self._customLists = options.customLists || [];

self._customLists.forEach(item => {
Expand All @@ -62,7 +62,7 @@ function Sentencer() {
});
};

self.use = function (options) {
self.use = function(options) {
var newInstance = new Sentencer();
newInstance.configure(options);
return newInstance;
Expand All @@ -73,34 +73,34 @@ function Sentencer() {
// THE GOODS
// ---------------------------------------------

Sentencer.prototype.make = function (template) {
Sentencer.prototype.make = function(template) {
var self = this;

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

if (occurrences && occurrences.length) {
for (var i = 0; i < occurrences.length; i++) {
if(occurrences && occurrences.length) {
for(var i = 0; i < occurrences.length; i++) {
var action = occurrences[i].replace('{{', '').replace('}}', '').trim();
var result = '';
var actionIsFunctionCall = action.match(/^\w+\((.+?)\)$/);

if (actionIsFunctionCall) {
if(actionIsFunctionCall) {
var actionNameWithParens = action.match(/^(\w+)\(/);
var actionName = actionNameWithParens[1];
var actionExists = self.actions[actionName];
var actionContents = action.match(/\((.+?)\)/);
actionContents = actionContents && actionContents[1];

if (actionExists && actionContents) {
if(actionExists && actionContents) {
try {
var args = _.map(actionContents.split(','), maybeCastToNumber);
result = self.actions[actionName].apply(null, args);
}
catch (e) { }
catch(e) { }
}
} else {
if (self.actions[action]) {
if(self.actions[action]) {
result = self.actions[action]();
} else {
result = '{{ ' + action + ' }}';
Expand Down
102 changes: 51 additions & 51 deletions tests/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,70 +2,70 @@ var assert = require("assert");

var Sentencer = require('../index.js');

describe('Sentencer:', function () {
describe('Sentencer:', function() {

it('should exist', function () {
it('should exist', function() {
assert(Sentencer);
});

describe('Default', function () {
describe('Default', function() {

describe('# Words', function () {
describe('# Words', function() {

it('should include a list of nouns', function () {
it('should include a list of nouns', function() {
assert(Sentencer._nouns.length);
});

it('should include a list of adjectives', function () {
it('should include a list of adjectives', function() {
assert(Sentencer._adjectives.length);
});

});

describe('# Actions', function () {
describe('# Actions', function() {

it('should include `noun`', function () { assert(Sentencer.actions.noun); });
it('should include `a_noun`', function () { assert(Sentencer.actions.a_noun); });
it('should include `nouns`', function () { assert(Sentencer.actions.nouns); });
it('should include `adjective`', function () { assert(Sentencer.actions.adjective); });
it('should include `an_adjective`', function () { assert(Sentencer.actions.an_adjective); });
it('should include `noun`', function() { assert(Sentencer.actions.noun); });
it('should include `a_noun`', function() { assert(Sentencer.actions.a_noun); });
it('should include `nouns`', function() { assert(Sentencer.actions.nouns); });
it('should include `adjective`', function() { assert(Sentencer.actions.adjective); });
it('should include `an_adjective`', function() { assert(Sentencer.actions.an_adjective); });

});

});

describe('API', function () {
describe('API', function() {

it('should include a `configure` function', function () {
it('should include a `configure` function', function() {
assert(Sentencer.configure);
});

it('should merge a new action', function () {
it('should merge a new action', function() {
Sentencer.configure({
actions: {
firstNewAction: function () { return 'hello'; }
firstNewAction: function() { return 'hello'; }
}
});

assert.equal(Sentencer.actions.firstNewAction(), 'hello');
});

it('should accept another action merge later', function () {
it('should accept another action merge later', function() {
Sentencer.configure({
actions: {
secondNewAction: function () { return 'hello again'; }
secondNewAction: function() { return 'hello again'; }
}
});

assert.equal(Sentencer.actions.firstNewAction(), 'hello', 'first action still exists');
assert.equal(Sentencer.actions.secondNewAction(), 'hello again', 'second action exists as well');
});

it('should include a `make` function', function () {
it('should include a `make` function', function() {
assert(Sentencer.make);
});

it('should merge a new custom list', function () {
it('should merge a new custom list', function() {
Sentencer.configure({
customLists: [
{
Expand All @@ -88,61 +88,61 @@ describe('Sentencer:', function () {

});

describe('Templating', function () {
describe('Templating', function() {

describe('# Default Actions', function () {
describe('# Default Actions', function() {

it('{{ noun }}', function () { assert(Sentencer.make('{{ noun }}')); });
it('{{ a_noun }}', function () { assert(Sentencer.make('{{ a_noun }}')); });
it('{{ nouns }}', function () { assert(Sentencer.make('{{ nouns }}')); });
it('{{ adjective }}', function () { assert(Sentencer.make('{{ adjective }}')); });
it('{{ an_adjective }}', function () { assert(Sentencer.make('{{ an_adjective }}')); });
it('{{ noun }}', function() { assert(Sentencer.make('{{ noun }}')); });
it('{{ a_noun }}', function() { assert(Sentencer.make('{{ a_noun }}')); });
it('{{ nouns }}', function() { assert(Sentencer.make('{{ nouns }}')); });
it('{{ adjective }}', function() { assert(Sentencer.make('{{ adjective }}')); });
it('{{ an_adjective }}', function() { assert(Sentencer.make('{{ an_adjective }}')); });

});

describe('# Custom Actions', function () {
describe('# Custom Actions', function() {

it('{{ firstNewAction }}', function () {
it('{{ firstNewAction }}', function() {
assert.equal(Sentencer.make('{{ firstNewAction }}'), 'hello');
});

it('{{ secondNewAction }}', function () {
it('{{ secondNewAction }}', function() {
assert.equal(Sentencer.make('{{ secondNewAction }}'), 'hello again');
});

it('should return {{ action }} if it does not exist', function () {
it('should return {{ action }} if it does not exist', function() {
assert.equal(Sentencer.make('{{ nonexistant thing }}'), '{{ nonexistant thing }}');
});

});

describe('# Custom Actions With Arguments', function () {
describe('# Custom Actions With Arguments', function() {

Sentencer.configure({
actions: {
withArgument: function (number) {
withArgument: function(number) {
return number;
},
withArguments: function () {
withArguments: function() {
return arguments.length;
}
}
});

it('should allow an action with one argument', function () {
assert.equal(Sentencer.make('{{ withArgument(1) }}'), 1);
it('should allow an action with one argument', function() {
assert.equal( Sentencer.make('{{ withArgument(1) }}'), 1 );
});

it('should allow an action with multiple arguments', function () {
assert.equal(Sentencer.make('{{ withArguments(1,2,3) }}'), 3);
it('should allow an action with multiple arguments', function() {
assert.equal( Sentencer.make('{{ withArguments(1,2,3) }}'), 3 );
});

it('should cast arguments as numbers when possible, otherwise strings', function () {
it('should cast arguments as numbers when possible, otherwise strings', function() {
var result = null;

Sentencer.configure({
actions: {
test: function () {
test: function() {
result = Array.prototype.slice.call(arguments);
}
}
Expand All @@ -152,18 +152,18 @@ describe('Sentencer:', function () {
assert.deepEqual(result, [1, 'hey hello', 2]);
});

it('should fail silently if an action with arguments does not exist', function () {
assert.deepEqual(Sentencer.make('{{ nonExistantThing(1,2,3) }}'), '');
it('should fail silently if an action with arguments does not exist', function() {
assert.deepEqual( Sentencer.make('{{ nonExistantThing(1,2,3) }}'), '' );
});

it('pass text through if someone tries to exploit eval', function () {
it('pass text through if someone tries to exploit eval', function() {
assert.deepEqual(
Sentencer.make('{{ nothing; console.log("This should not evaluate"); }}'),
'{{ nothing; console.log("This should not evaluate"); }}'
);
});

it('should pass text through when handed some garbage', function () {
it('should pass text through when handed some garbage', function() {
assert.deepEqual(
Sentencer.make('{{ &@#&(%*@$UU#I$HTRIGUHW$@) }}'),
'{{ &@#&(%*@$UU#I$HTRIGUHW$@) }}'
Expand All @@ -172,27 +172,27 @@ describe('Sentencer:', function () {

});

describe('# Custom Lists', function () {
describe('# Custom Lists', function() {

it('{{ animal }}', function () {
it('{{ animal }}', function() {
assert.notEqual(["dog", "cat", "elephant"].indexOf(Sentencer.make('{{ animal }}')), -1, "missing animal");
});

it('{{ an_animal }}', function () {
it('{{ an_animal }}', function() {
assert.notEqual(["a dog", "a cat", "an elephant"].indexOf(Sentencer.make('{{ an_animal }}')), -1, "missing an_animal");
});

it('{{ animals }}', function () {
it('{{ animals }}', function() {
assert.notEqual(["dogs", "cats", "elephants"].indexOf(Sentencer.make('{{ animals }}')), -1, "missing animals");
});

});
});

describe('Test Print', function () {
describe('Test Print', function() {

it('should have logged a sentence', function () {
console.log(Sentencer.make(" Here is {{ an_adjective }} sentence generated by Sentencer's {{ nouns }}."));
it('should have logged a sentence', function() {
console.log( Sentencer.make(" Here is {{ an_adjective }} sentence generated by Sentencer's {{ nouns }}.") );
});

});
Expand Down

0 comments on commit bbadbab

Please sign in to comment.