Skip to content

Commit

Permalink
Merge pull request #13 from lordnull/mw-allow-nested-scopes
Browse files Browse the repository at this point in the history
Allow Nested Scopes
  • Loading branch information
lordnull committed Feb 4, 2016
2 parents fc8d417 + 502e720 commit 307705e
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 29 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,20 @@ Or if you need to roll half your level in d6's:

c([Level] / 2)d6

For more advanced usage, scopes can be nested.

var scope = {
'takes.priority': 3,
'takes':{
'priority': 5
},
'nested'{
'value': 7
}
};
var result = dice.roll("[takes.priority] + [nested.value]", scope);
result == 10; // because we look for an exact match of property name first

Contributing
============

Expand Down
21 changes: 18 additions & 3 deletions build/dice.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,22 @@ var ops = {
'lookup': function(){
var variableName = this.value;
return function(scope){
return scope[variableName];
var undef;
var out = scope[variableName];
if(out != undef){
return out;
}
var split = variableName.split('.');
if(variableName == split){
return out;
}
reduceRes = split.reduce(function(acc, elem){
if(acc == undef){
return;
}
return acc[elem];
}, scope);
return reduceRes;
}
},

Expand Down Expand Up @@ -448,8 +463,8 @@ module.exports = (function() {
peg$c52 = function(v) { return {'op':'lookup', 'value':v}; },
peg$c53 = "[",
peg$c54 = { type: "literal", value: "[", description: "\"[\"" },
peg$c55 = /^[a-zA-Z 0-9]/,
peg$c56 = { type: "class", value: "[a-zA-Z 0-9]", description: "[a-zA-Z 0-9]" },
peg$c55 = /^[^[\]]/,
peg$c56 = { type: "class", value: "[^[\\]]", description: "[^[\\]]" },
peg$c57 = "]",
peg$c58 = { type: "literal", value: "]", description: "\"]\"" },
peg$c59 = function(varname) { return varname.join(""); },
Expand Down
44 changes: 23 additions & 21 deletions build/dice.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/dice.peg
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ lookup
= v:variable { return {'op':'lookup', 'value':v}; }

variable
= "[" varname:[a-zA-Z 0-9]+ "]" { return varname.join(""); }
= "[" varname:[^[\]]+ "]" { return varname.join(""); }

integer "integer"
= digits:[0-9]+ { return parseInt(digits.join(""), 10); }
Expand Down
17 changes: 16 additions & 1 deletion src/evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,22 @@ var ops = {
'lookup': function(){
var variableName = this.value;
return function(scope){
return scope[variableName];
var undef;
var out = scope[variableName];
if(out != undef){
return out;
}
var split = variableName.split('.');
if(variableName == split){
return out;
}
reduceRes = split.reduce(function(acc, elem){
if(acc == undef){
return;
}
return acc[elem];
}, scope);
return reduceRes;
}
},

Expand Down
4 changes: 2 additions & 2 deletions src/parser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 31 additions & 1 deletion tests/dice.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ describe("Dice", function(){
"( 2d6 * 7)", "1d6 + (3d6 *7 )", "( 1 + 2) * 3",
"1d20 * (1d6 * ( 3 + [variable] ) )", "r([variable] + 2)d6",
"f(5 /3)", "( c([variable] / 7) + 3) * 2",
"3 + r(3/ [variable])"];
"3 + r(3/ [variable])", "1d[variable with space]",
"[variable.with.dot]d20", "3d6 + [varible with-mixed.odd_characters]"];

strings.map(function(toParse){
it("parses " + toParse, function(){
Expand Down Expand Up @@ -215,6 +216,35 @@ describe("Dice", function(){
expect(results.mean).toEqual(1);
});

it("can lookup weirdly named properties", function(){
var propName = "ಠ_ಠ and-more_unusual.characters#$!^*&";
var scope = {};
scope[propName] = 53;
var results = dice.roll("[" + propName + "]", scope);
expect(results).toEqual(53);
});

it("can lookup nested scope variables", function(){
var scope = {
upper: {
nested: 7
}
};
var results = dice.roll("[upper.nested]", scope);
expect(results).toEqual(7);
});

it("prefers full property name over nested scope", function(){
var scope = {
"upper.nested": 5,
upper: {
nested: 93
}
};
var results = dice.roll("[upper.nested]", scope);
expect(results).toEqual(5);
});

});
});

0 comments on commit 307705e

Please sign in to comment.