Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

roll

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

roll - npm Package Compare versions

Comparing version
1.3.1
to
1.3.2
+7
-7
package.json
{
"name": "roll",
"version": "1.3.1",
"version": "1.3.2",
"author": "Troy Goode <troygoode@gmail.com> (https://github.com/troygoode/)",

@@ -35,9 +35,9 @@ "description": "node.js package for rolling dice and adding modifiers. ex: 2d6+1",

"devDependencies": {
"lint": "^1.1.2",
"mocha": "^8.2.1",
"should": "^11.1.0"
"prettier": "^3.3.3",
"mocha": "^10.7.0",
"should": "^13.2.3"
},
"scripts": {
"test": "./node_modules/mocha/bin/mocha",
"lint": "./node_modules/lint/bin/node-lint lib test"
"test": "./node_modules/.bin/_mocha",
"prettier": "./node_modules/.bin/prettier package.json bin/roll \"{example,src,test}/**/*.js\" --write"
},

@@ -48,4 +48,4 @@ "bin": {

"dependencies": {
"minimist": "^1.2.5"
"minimist": "^1.2.8"
}
}

@@ -7,4 +7,2 @@ # roll

[![build status](https://secure.travis-ci.org/troygoode/node-roll.png)](http://travis-ci.org/troygoode/node-roll)
## How To Use (From Shell)

@@ -11,0 +9,0 @@

@@ -1,10 +0,9 @@

/*jslint indent: 2*/
/*global require: true, module: true*/
(function () {
'use strict';
"use strict";
var InvalidInputError = require('./input-error.js'),
transformationFunctions = require('./transforms'),
transformationKeys = require('./keys'),
regex = /^(\d*)d(\d+|\%)(([\+\-\/\*bw])(\d+))?(([\+\-\/\*])(\d+|(\d*)d(\d+|\%)(([\+\-\/\*bw])(\d+))?))*$/,
var InvalidInputError = require("./input-error.js"),
transformationFunctions = require("./transforms"),
transformationKeys = require("./keys"),
regex =
/^(\d*)d(\d+|\%)(([\+\-\/\*bw])(\d+))?(([\+\-\/\*])(\d+|(\d*)d(\d+|\%)(([\+\-\/\*bw])(\d+))?))*$/,
roll,

@@ -40,3 +39,3 @@ cleaner,

if (segments[0].indexOf('b') > -1 || segments[0].indexOf('w') > -1) {
if (segments[0].indexOf("b") > -1 || segments[0].indexOf("w") > -1) {
transforms.push(transformationKeys[match[4]](parseInt(match[5], 10)));

@@ -50,7 +49,10 @@ }

transformationParameter = segments[seg]; // 2d20+3 => 3
if (transformationParameter.indexOf('d') > -1) { // perform a roll
if (transformationParameter.indexOf("d") > -1) {
// perform a roll
outsideRoll = this.roll(transformationParameter, true);
transforms.push(transformationKeys[operator](outsideRoll.result));
} else {
transforms.push(transformationKeys[operator](parseInt(transformationParameter, 10)));
transforms.push(
transformationKeys[operator](parseInt(transformationParameter, 10)),
);
}

@@ -61,7 +63,12 @@ }

quantity: quantity ? parseInt(quantity, 10) : 1,
sides: sides === '%' ? 100 : parseInt(sides, 10),
transformations: hasTransformation || transforms.length > 0 ? transforms.length > 0 ? transforms : transformationKeys[match[4]](parseInt(match[5], 10)) : ['sum'],
sides: sides === "%" ? 100 : parseInt(sides, 10),
transformations:
hasTransformation || transforms.length > 0
? transforms.length > 0
? transforms
: transformationKeys[match[4]](parseInt(match[5], 10))
: ["sum"],
toString: function () {
return s;
}
},
};

@@ -73,3 +80,3 @@ };

throw new InvalidInputError();
} else if (typeof input === 'string') {
} else if (typeof input === "string") {
input = this.parse(input);

@@ -79,7 +86,7 @@ }

var rolled = [],
calculations = [],
carryFiller = [];
calculations = [],
carryFiller = [];
while (rolled.length < input.quantity) {
rolled.push(Math.floor((this.random() * input.sides) + 1));
rolled.push(Math.floor(this.random() * input.sides + 1));
}

@@ -89,29 +96,40 @@

calculations = input.transformations.reduce(function (previous, transformation) {
var transformationFunction,
transformationAdditionalParameter,
sumParam = false;
if (typeof transformation === 'function') { // lets you pass something custom in
transformationFunction = transformation;
} else if (typeof transformation === 'string') { // "sum"
transformationFunction = transformationFunctions[transformation];
} else if (transformation instanceof Array) { // ["add", 3]
if (transformation[0] instanceof Array) {
sumResult = true;
cleaner = transformation[1];
transformation = transformation[0];
} else if (transformation[1] instanceof Array) {
sumParam = true;
cleaner = transformation[0];
transformation = transformation[1];
calculations = input.transformations.reduce(
function (previous, transformation) {
var transformationFunction,
transformationAdditionalParameter,
sumParam = false;
if (typeof transformation === "function") {
// lets you pass something custom in
transformationFunction = transformation;
} else if (typeof transformation === "string") {
// "sum"
transformationFunction = transformationFunctions[transformation];
} else if (transformation instanceof Array) {
// ["add", 3]
if (transformation[0] instanceof Array) {
sumResult = true;
cleaner = transformation[1];
transformation = transformation[0];
} else if (transformation[1] instanceof Array) {
sumParam = true;
cleaner = transformation[0];
transformation = transformation[1];
}
transformationFunction = transformationFunctions[transformation[0]]; // fn for "add"
transformationAdditionalParameter = transformation[1];
}
transformationFunction = transformationFunctions[transformation[0]]; // fn for "add"
transformationAdditionalParameter = transformation[1];
}
if (sumParam === true && previous[0] instanceof Array) {
previous[0] = transformationFunctions[cleaner](previous[0]);
}
previous.unshift(transformationFunction(previous[0], transformationAdditionalParameter));
return previous;
}, [rolled]);
if (sumParam === true && previous[0] instanceof Array) {
previous[0] = transformationFunctions[cleaner](previous[0]);
}
previous.unshift(
transformationFunction(
previous[0],
transformationAdditionalParameter,
),
);
return previous;
},
[rolled],
);

@@ -135,3 +153,3 @@ if (sumResult === true && calculations[0] instanceof Array) {

rolled: carryFiller,
result: calculations[0]
result: calculations[0],
};

@@ -142,3 +160,2 @@ };

module.exports.InvalidInputError = InvalidInputError;
}());
})();

@@ -1,14 +0,15 @@

/*jslint indent: 2*/
/*global require: true, module: true*/
(function () {
'use strict';
"use strict";
var util = require('util');
var util = require("util");
function InvalidInputError(input) {
this.name = 'InvalidInputError';
this.name = "InvalidInputError";
if (input) {
this.message = util.format('"%s" is not a valid input string for node-roll.', input);
this.message = util.format(
'"%s" is not a valid input string for node-roll.',
input,
);
} else {
this.message = 'No input string was supplied to node-roll.';
this.message = "No input string was supplied to node-roll.";
}

@@ -22,4 +23,2 @@ this.input = input;

module.exports = InvalidInputError;
}());
})();

@@ -1,46 +0,24 @@

/*jslint indent: 2*/
/*global require: true, module: true*/
(function () {
'use strict';
"use strict";
module.exports = {
'+': function (value) {
return [
'sum',
['add', value]
];
"+": function (value) {
return ["sum", ["add", value]];
},
'-': function (value) {
return [
'sum',
['subtract', value]
];
"-": function (value) {
return ["sum", ["subtract", value]];
},
'*': function (value) {
return [
'sum',
['multiply', value]
];
"*": function (value) {
return ["sum", ["multiply", value]];
},
'/': function (value) {
return [
'sum',
['divide', value]
];
"/": function (value) {
return ["sum", ["divide", value]];
},
'b': function (value) {
return [
['best-of', value],
'sum'
];
b: function (value) {
return [["best-of", value], "sum"];
},
'w': function (value) {
return [
['worst-of', value],
'sum'
];
}
w: function (value) {
return [["worst-of", value], "sum"];
},
};
}());
})();

@@ -1,5 +0,3 @@

/*jslint indent: 2*/
/*global require: true, module: true*/
(function () {
'use strict';
"use strict";

@@ -9,4 +7,2 @@ module.exports = function (number, value) {

};
}());
})();

@@ -1,5 +0,3 @@

/*jslint indent: 2*/
/*global require: true, module: true*/
(function () {
'use strict';
"use strict";

@@ -12,3 +10,3 @@ module.exports = function (rolledDice, value) {

i;
for (i = 0; i < sorted.length && i < (value / 1); i = i + 1) {
for (i = 0; i < sorted.length && i < value / 1; i = i + 1) {
result.push(sorted[i]);

@@ -18,4 +16,2 @@ }

};
}());
})();

@@ -1,5 +0,3 @@

/*jslint indent: 2*/
/*global require: true, module: true*/
(function () {
'use strict';
"use strict";

@@ -9,4 +7,2 @@ module.exports = function (number, value) {

};
}());
})();

@@ -1,12 +0,9 @@

/*jslint indent: 2*/
/*global require: true, module: true*/
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')
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"),
};

@@ -1,5 +0,3 @@

/*jslint indent: 2*/
/*global require: true, module: true*/
(function () {
'use strict';
"use strict";

@@ -9,4 +7,2 @@ module.exports = function (number, value) {

};
}());
})();

@@ -1,5 +0,3 @@

/*jslint indent: 2*/
/*global require: true, module: true*/
(function () {
'use strict';
"use strict";

@@ -9,4 +7,2 @@ module.exports = function (number, value) {

};
}());
})();

@@ -1,5 +0,3 @@

/*jslint indent: 2*/
/*global require: true, module: true*/
(function () {
'use strict';
"use strict";

@@ -11,4 +9,2 @@ module.exports = function (rolledDice) {

};
}());
})();

@@ -1,5 +0,3 @@

/*jslint indent: 2*/
/*global require: true, module: true*/
(function () {
'use strict';
"use strict";

@@ -12,3 +10,3 @@ module.exports = function (rolledDice, value) {

i;
for (i = 0; i < sorted.length && i < (value / 1); i = i + 1) {
for (i = 0; i < sorted.length && i < value / 1; i = i + 1) {
result.push(sorted[i]);

@@ -18,4 +16,2 @@ }

};
}());
})();
language: node_js
node_js:
- 12.0
/*jslint indent: 2*/
/*global require: true, module: true, describe: true, it: true, beforeEach: true, __dirname: true, console: true, process: true */
(function () {
'use strict';
var exec = require('child_process').exec,
should = require('should'),
FakeRandom = require('./fake-random'),
random = new FakeRandom([ // can only test this library if we make things not actually random
0.24, // 20 * .24 => 4.8 => 5
0.62, // 20 * .62 => 12.4 => 13
0.51, // 20 * .51 => 10.2 => 11
0.13, // 20 * .13 => 2.6 => 3
0.66, // 20 * .66 => 13.2 => 14
0.33, // 20 * .66 => 6.6 => 7
0.12 // 20 * .12 => 2.4 => 2
]);
describe('bin/roll', function () {
beforeEach(random.reset.bind(random));
it('bin/roll garbage', function (done) {
exec((process.platform === 'win32' ? 'node ././bin/roll' : __dirname + '/../bin/roll') + ' garbage', function (err, stdout, stderr) {
if (err) {
should.exist(err);
stderr.should.eql('"garbage" is not a valid input string for node-roll.\n');
return done();
}
done('Should not be reachable.');
});
});
it('bin/roll 2d20', function (done) {
exec((process.platform === 'win32' ? 'node ././bin/roll' : __dirname + '/../bin/roll') + ' 2d20', function (err, stdout, stderr) {
if (err) {
return done(err);
}
/^\d+\n$/.test(stdout).should.eql(true);
done();
});
});
it('bin/roll -d 2d20', function (done) {
exec((process.platform === 'win32' ? 'node ././bin/roll' : __dirname + '/../bin/roll') + ' -d 2d20', function (err, stdout, stderr) {
if (err) {
return done(err);
}
stdout.should.match(/^Dice: (\d+)(,\s*\d+)*\nTotal: \d+\n$/);
done();
});
});
});
}());
/*jslint indent: 2*/
/*global require: true, module: true, describe: true, it: true, beforeEach: true */
(function () {
'use strict';
var FakeRandomNumberGenerator = function (numbers) {
this.pointer = 0;
this.numbers = numbers;
};
FakeRandomNumberGenerator.prototype.reset = function () {
this.pointer = 0;
};
FakeRandomNumberGenerator.prototype.next = function () {
this.pointer = this.pointer + 1;
if (this.pointer > this.numbers.length) {
this.pointer = 1;
}
return this.numbers[this.pointer - 1];
};
module.exports = FakeRandomNumberGenerator;
}());

Sorry, the diff of this file is not supported yet

/*jslint indent: 2*/
/*global require: true, module: true, describe: true, it: true, beforeEach: true, __dirname: true, console: true, process: true */
(function () {
'use strict';
var Roll = require('../src'),
should = require('should'),
FakeRandom = require('./fake-random'),
random = new FakeRandom([ // can only test this library if we make things not actually random
0.24, // 20 * .24 => 4.8 => 5
0.62, // 20 * .62 => 12.4 => 13
0.51, // 20 * .51 => 10.2 => 11
0.13, // 20 * .13 => 2.6 => 3
0.66, // 20 * .66 => 13.2 => 14
0.33, // 20 * .66 => 6.6 => 7
0.12 // 20 * .12 => 2.4 => 2
]),
roll = new Roll(random.next.bind(random));
describe('roll', function () {
beforeEach(random.reset.bind(random));
describe('multiroll', function () {
it('1d20+1d20', function () {
var result = roll.roll('1d20+1d20');
result.result.should.equal(18);
});
it('d20+1d20', function () {
var result = roll.roll('d20+1d20');
result.result.should.equal(18);
});
it('1d20+d20', function () {
var result = roll.roll('1d20+d20');
result.result.should.equal(18);
});
it('d20+d20', function () {
var result = roll.roll('d20+d20');
result.result.should.equal(18);
});
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('issue #11', function () {
// https://github.com/troygoode/node-roll/issues/11
var result = roll.roll('2d20w1+5');
result.rolled.length.should.equal(2);
result.rolled[0].should.equal(5);
result.rolled[1].should.equal(13);
result.result.should.equal(10); // worst(5, 13) + 5 = 5 + 5 = 10
});
});
it('d20', function () {
var result = roll.roll('d20');
result.rolled.length.should.equal(1);
result.rolled[0].should.equal(5);
result.result.should.equal(5);
});
it('d%', function () {
var result = roll.roll('d%');
result.rolled.length.should.equal(1);
result.rolled[0].should.equal(25);
result.result.should.equal(25);
});
it('2d20', function () {
var result = roll.roll('2d20');
result.rolled.length.should.equal(2);
result.rolled[0].should.equal(5);
result.rolled[1].should.equal(13);
result.result.should.equal(18);
});
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 () {
var result = roll.roll('2d20+3');
result.result.should.equal(21); // SUM + 3
});
it('2d20-3', function () {
var result = roll.roll('2d20-3');
result.result.should.equal(15); // SUM - 3
});
it('2d20*3', function () {
var result = roll.roll('2d20*3');
result.result.should.equal(54); // SUM * 3
});
it('2d20/3', function () {
var result = roll.roll('2d20/3');
result.result.should.equal(6); // SUM / 3
});
it('5d20b1', function () {
var result = roll.roll('5d20b1');
result.result.should.equal(14);
});
it('5d20b2', function () {
var result = roll.roll('5d20b2');
result.calculations[1].length.should.equal(2);
result.calculations[1][0].should.equal(14);
result.calculations[1][1].should.equal(13);
result.result.should.equal(27); // 14 + 13
});
it('5d20w1', function () {
var result = roll.roll('5d20w1');
result.result.should.equal(3);
});
it('5d20w2', function () {
var result = roll.roll('5d20w2');
result.calculations[1].length.should.equal(2);
result.calculations[1][0].should.equal(3);
result.calculations[1][1].should.equal(5);
result.result.should.equal(8); // 3 + 5
});
it('2d20b1+1d4', function () {
var result = roll.roll('2d20b1+1d4'); // 5, 13, 11
result.result.should.equal(14);
});
it('2d20w1+1d4', function () {
var result = roll.roll('2d20w1+1d4'); // 5, 13, 11
result.result.should.equal(12);
});
it('validates input', function (done) {
try {
roll.roll('garbage');
done('Should not be reachable.');
} catch (e) {
should.exist(e);
e.name.should.eql('InvalidInputError');
e.message.should.eql('"garbage" is not a valid input string for node-roll.');
e.input.should.eql('garbage');
done();
}
});
it('exposes validation', function () {
roll.validate('2d20+3').should.equal(true);
roll.validate('garbage').should.equal(false);
});
});
}());

Sorry, the diff of this file is not supported yet