+26
-14
@@ -12,8 +12,7 @@ /*jslint indent: 2*/ | ||
| cleaner, | ||
| sumResult = false; | ||
| sumResult = false, | ||
| filler = []; | ||
| roll = function (random) { | ||
| this.random = random || function () { | ||
| return Math.random(); | ||
| }; | ||
| this.random = random || Math.random.bind(Math); | ||
| }; | ||
@@ -38,3 +37,5 @@ | ||
| opIndex = 0, | ||
| segments = s.split(/[\+\-]/); | ||
| segments = s.split(/[\+\-]/), | ||
| outsideRoll, | ||
| seg; | ||
@@ -44,3 +45,4 @@ if (segments[0].indexOf('b') > -1) { | ||
| } | ||
| for (var seg = 1; seg < segments.length; seg++) { | ||
| for (seg = 1; seg < segments.length; seg += 1) { | ||
| opIndex += segments[seg - 1].length; | ||
@@ -50,4 +52,5 @@ operator = s[opIndex]; // 2d20+3 => "+" | ||
| transformationParameter = segments[seg]; // 2d20+3 => 3 | ||
| if (transformationParameter.indexOf('d') > -1) { | ||
| transforms.push(transformationKeys[operator](parseInt(this.roll(transformationParameter).result, 10))); | ||
| if (transformationParameter.indexOf('d') > -1) { // perform a roll | ||
| outsideRoll = this.roll(transformationParameter, true); | ||
| transforms.push(transformationKeys[operator](outsideRoll.result)); | ||
| } else { | ||
@@ -68,3 +71,3 @@ transforms.push(transformationKeys[operator](parseInt(transformationParameter, 10))); | ||
| roll.prototype.roll = function (input) { | ||
| roll.prototype.roll = function (input, invokedByParse) { | ||
| if (!input) { | ||
@@ -77,3 +80,4 @@ throw new InvalidInputError(); | ||
| var rolled = [], | ||
| calculations = []; | ||
| calculations = [], | ||
| carryFiller = []; | ||
@@ -84,2 +88,4 @@ while (rolled.length < input.quantity) { | ||
| filler.push(rolled); | ||
| calculations = input.transformations.reduce(function (previous, transformation) { | ||
@@ -89,3 +95,2 @@ var transformationFunction, | ||
| sumParam = false; | ||
| if (typeof transformation === 'function') { // lets you pass something custom in | ||
@@ -108,3 +113,3 @@ transformationFunction = transformation; | ||
| } | ||
| if (sumParam === true && previous[0] instanceof Array){ | ||
| if (sumParam === true && previous[0] instanceof Array) { | ||
| previous[0] = transformationFunctions[cleaner](previous[0]); | ||
@@ -121,6 +126,14 @@ } | ||
| if (!invokedByParse) { | ||
| if (filler.length > 1) { | ||
| filler.unshift(filler.pop()); | ||
| } | ||
| carryFiller = filler.length === 1 ? filler[0] : filler; | ||
| filler = []; | ||
| } | ||
| return { | ||
| input: input, | ||
| calculations: calculations, | ||
| rolled: calculations[calculations.length - 1], | ||
| rolled: carryFiller, | ||
| result: calculations[0] | ||
@@ -134,2 +147,1 @@ }; | ||
| }()); | ||
| /*jslint indent: 2*/ | ||
| /*global require: true, module: true*/ | ||
| module.exports = require('require-directory')(module); | ||
| module.exports = { | ||
| add: require('./add'), | ||
| 'best-of': require('./best-of'), | ||
| divide: require('./divide'), | ||
| multiply: require('./multiply'), | ||
| subtract: require('./subtract'), | ||
| sum: require('./sum'), | ||
| 'worst-of': require('./worst-of') | ||
| }; | ||
+5
-14
| { | ||
| "name": "roll", | ||
| "version": "1.1.0", | ||
| "version": "1.2.0", | ||
| "author": "Troy Goode <troygoode@gmail.com> (https://github.com/troygoode/)", | ||
@@ -26,8 +26,3 @@ "description": "node.js package for rolling dice and adding modifiers. ex: 2d6+1", | ||
| ], | ||
| "licenses": [ | ||
| { | ||
| "type": "MIT", | ||
| "url": "http://www.opensource.org/licenses/mit-license.php" | ||
| } | ||
| ], | ||
| "license": "MIT", | ||
| "bugs": { | ||
@@ -38,12 +33,8 @@ "url": "https://github.com/TroyGoode/node-roll/issues" | ||
| "engines": { | ||
| "node": ">=0.10.0" | ||
| "node": ">=4" | ||
| }, | ||
| "dependencies": { | ||
| "require-directory": "^2.0.0" | ||
| }, | ||
| "devDependencies": { | ||
| "lint": "^1.1.2", | ||
| "mocha": "^1.18.2", | ||
| "should": "^3.3.1", | ||
| "srand": "^1.1.4" | ||
| "mocha": "^3.0.2", | ||
| "should": "^11.1.0" | ||
| }, | ||
@@ -50,0 +41,0 @@ "scripts": { |
@@ -17,2 +17,5 @@ /*jslint indent: 2*/ | ||
| this.pointer = this.pointer + 1; | ||
| if (this.pointer > this.numbers.length) { | ||
| this.pointer = 1; | ||
| } | ||
| return this.numbers[this.pointer - 1]; | ||
@@ -19,0 +22,0 @@ }; |
+70
-5
| /*jslint indent: 2*/ | ||
| /*global require: true, module: true, describe: true, it: true, beforeEach: true, __dirname: true */ | ||
| /*global require: true, module: true, describe: true, it: true, beforeEach: true, __dirname: true, console: true, process: true */ | ||
| (function () { | ||
@@ -8,2 +8,3 @@ 'use strict'; | ||
| should = require('should'), | ||
| fs = require('fs'), | ||
| Roll = require('../lib'), | ||
@@ -16,3 +17,5 @@ FakeRandom = require('./fake-random'), | ||
| 0.13, // 20 * .13 => 2.6 => 3 | ||
| 0.66 // 20 * .66 => 13.2 => 14 | ||
| 0.66, // 20 * .66 => 13.2 => 14 | ||
| 0.33, // 20 * .66 => 6.6 => 7 | ||
| 0.12 // 20 * .12 => 2.4 => 2 | ||
| ]), | ||
@@ -47,4 +50,19 @@ roll = new Roll(random.next.bind(random)); | ||
| it('2d20b1+1d4', function () { | ||
| var result = roll.roll('2d20b1+1d4'); | ||
| result.rolled.should.eql([[13, 11], [1]]); | ||
| }); | ||
| }); | ||
| describe('bug reports', function () { | ||
| it('issue #7', function () { | ||
| // https://github.com/troygoode/node-roll/issues/7 | ||
| var result = roll.roll('2d10+5'); | ||
| result.rolled.length.should.equal(2); | ||
| result.rolled[0].should.equal(3); | ||
| result.rolled[1].should.equal(7); | ||
| result.result.should.equal(15); // (3 + 7) + 5 = 15 | ||
| }); | ||
| }); | ||
| it('d20', function () { | ||
@@ -72,2 +90,50 @@ var result = roll.roll('d20'); | ||
| it('1d20+2d20', function () { | ||
| var result = roll.roll('1d20+2d20'); | ||
| result.rolled.length.should.equal(2); | ||
| result.rolled[0][0].should.equal(11); | ||
| result.rolled[1][0].should.equal(5); | ||
| result.rolled[1][1].should.equal(13); | ||
| result.result.should.equal(29); | ||
| }); | ||
| it('1d20+2d20+3d20', function () { | ||
| var result = roll.roll('1d20+2d20+3d20'); | ||
| result.rolled.length.should.equal(3); | ||
| result.rolled[0][0].should.equal(7); | ||
| result.rolled[1][0].should.equal(5); | ||
| result.rolled[1][1].should.equal(13); | ||
| result.rolled[2][0].should.equal(11); | ||
| result.rolled[2][1].should.equal(3); | ||
| result.rolled[2][2].should.equal(14); | ||
| result.result.should.equal(53); | ||
| }); | ||
| it('yahtzee', function () { | ||
| var result = roll.roll('5d6'); | ||
| result.rolled.length.should.equal(5); | ||
| result.rolled[0].should.equal(2); | ||
| result.rolled[1].should.equal(4); | ||
| result.rolled[2].should.equal(4); | ||
| result.rolled[3].should.equal(1); | ||
| result.rolled[4].should.equal(4); | ||
| result.result.should.equal(15); | ||
| }); | ||
| it('double yahtzee', function () { | ||
| var result = roll.roll('5d6+5d6'); | ||
| result.rolled.length.should.equal(2); | ||
| result.rolled[0][0].should.equal(2); | ||
| result.rolled[0][1].should.equal(1); | ||
| result.rolled[0][2].should.equal(2); | ||
| result.rolled[0][3].should.equal(4); | ||
| result.rolled[0][4].should.equal(4); | ||
| result.rolled[1][0].should.equal(2); | ||
| result.rolled[1][1].should.equal(4); | ||
| result.rolled[1][2].should.equal(4); | ||
| result.rolled[1][3].should.equal(1); | ||
| result.rolled[1][4].should.equal(4); | ||
| result.result.should.equal(28); | ||
| }); | ||
| it('2d20+3', function () { | ||
@@ -138,3 +204,3 @@ var result = roll.roll('2d20+3'); | ||
| it('bin/roll garbage', function (done) { | ||
| exec(__dirname + '/../bin/roll garbage', function (err, stdout, stderr) { | ||
| exec((process.platform === 'win32' ? 'node ././bin/roll' : __dirname + '/../bin/roll') + ' garbage', function (err, stdout, stderr) { | ||
| if (err) { | ||
@@ -150,3 +216,3 @@ should.exist(err); | ||
| it('bin/roll 2d20', function (done) { | ||
| exec(__dirname + '/../bin/roll 2d20', function (err, stdout, stderr) { | ||
| exec((process.platform === 'win32' ? 'node ././bin/roll' : __dirname + '/../bin/roll') + ' 2d20', function (err, stdout, stderr) { | ||
| if (err) { | ||
@@ -162,2 +228,1 @@ return done(err); | ||
| }()); | ||
Sorry, the diff of this file is not supported yet
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
21498
16.89%0
-100%3
-25%475
21.17%165
1.85%1
Infinity%- Removed
- Removed