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
0.3.2
to
1.0.0
+3
.travis.yml
language: node_js
node_js:
- 0.10
/*jslint indent: 2*/
/*global require: true, module: true*/
(function () {
'use strict';
var util = require('util');
function InvalidInputError(input) {
this.name = 'InvalidInputError';
if (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.input = input;
}
InvalidInputError.prototype = new Error();
InvalidInputError.prototype.constructor = InvalidInputError;
module.exports = InvalidInputError;
}());
/*jslint indent: 2*/
/*global require: true, module: true*/
(function () {
'use strict';
module.exports = {
'+': function (value) {
return [
'sum',
['add', value]
];
},
'-': function (value) {
return [
'sum',
['subtract', value]
];
},
'*': function (value) {
return [
'sum',
['multiply', value]
];
},
'/': function (value) {
return [
'sum',
['divide', value]
];
},
'b': function (value) {
return [
['best-of', value],
'sum'
];
},
'w': function (value) {
return [
['worst-of', value],
'sum'
];
}
};
}());
/*jslint indent: 2*/
/*global require: true, module: true*/
(function () {
'use strict';
module.exports = function (number, value) {
return number + value;
};
}());
/*jslint indent: 2*/
/*global require: true, module: true*/
(function () {
'use strict';
module.exports = function (rolledDice, value) {
var result = [],
sorted = rolledDice.sort(function (a, b) {
return b - a;
}),
i;
for (i = 0; i < sorted.length && i < (value / 1); i = i + 1) {
result.push(sorted[i]);
}
return result;
};
}());
/*jslint indent: 2*/
/*global require: true, module: true*/
(function () {
'use strict';
module.exports = function (number, value) {
return number / value;
};
}());
/*jslint indent: 2*/
/*global require: true, module: true*/
module.exports = require('require-directory')(module);
/*jslint indent: 2*/
/*global require: true, module: true*/
(function () {
'use strict';
module.exports = function (number, value) {
return number * value;
};
}());
/*jslint indent: 2*/
/*global require: true, module: true*/
(function () {
'use strict';
module.exports = function (number, value) {
return number - value;
};
}());
/*jslint indent: 2*/
/*global require: true, module: true*/
(function () {
'use strict';
module.exports = function (rolledDice) {
return rolledDice.reduce(function (sum, value) {
return sum + value;
}, 0);
};
}());
/*jslint indent: 2*/
/*global require: true, module: true*/
(function () {
'use strict';
module.exports = function (rolledDice, value) {
var result = [],
sorted = rolledDice.sort(function (a, b) {
return a - b;
}),
i;
for (i = 0; i < sorted.length && i < (value / 1); i = i + 1) {
result.push(sorted[i]);
}
return result;
};
}());
/*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;
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 */
(function () {
'use strict';
var exec = require('child_process').exec,
should = require('should'),
Roll = require('../lib'),
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
]),
roll = new Roll(random.next.bind(random));
describe('roll', function () {
beforeEach(random.reset.bind(random));
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('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('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);
});
it('bin/roll garbage', function (done) {
exec(__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(__dirname + '/../bin/roll 2d20', function (err, stdout, stderr) {
if (err) {
return done(err);
}
/^\d+\n$/.test(stdout).should.eql(true);
done();
});
});
});
}());
+76
-131

@@ -1,134 +0,75 @@

var transformationKeys = {
'+': function(value){
return [
'sum',
['add', value]
];
},
'-': function(value){
return [
'sum',
['subtract', value]
];
},
'*': function(value){
return [
'sum',
['multiply', value]
];
},
'/': function(value){
return [
'sum',
['divide', value]
];
},
'b': function(value){
return [
['bestOf', value],
'sum'
];
}
};
/*jslint indent: 2*/
/*global require: true, module: true*/
(function () {
'use strict';
var transformationFunctions = {
'sum': function(rolledDice, value){
var sum = 0;
for(var i = 0; i < rolledDice.length; i++)
sum += rolledDice[i];
return sum;
},
'bestOf': function(rolledDice, value){
var sorted = rolledDice.sort(function(a,b){
return b - a;
});
var result = [];
for(var i = 0; i < sorted.length && i < (value / 1); i++)
result.push(sorted[i]);
return result;
},
'worstOf': function(rolledDice, value){
var sorted = rolledDice.sort(function(a,b){
return a - b;
});
var result = [];
for(var i = 0; i < sorted.length && i < (value / 1); i++)
result.push(sorted[i]);
return result;
},
'add': function(number, value){
return number + (value / 1);
},
'subtract': function(number, value){
return number - value;
},
'multiply': function(number, value){
return number * value;
},
'divide': function(number, value){
return number / value;
}
};
var InvalidInputError = require('./input-error.js'),
transformationFunctions = require('./transforms'),
transformationKeys = require('./keys'),
regex = /^(\d*)d(\d+|\%)(([\+\-\/\*bw])(\d+))?$/,
roll;
var roll = module.exports = {
random: function(){ return Math.random(); },
parse: function(s){
var regex = /^(\d*)d(\d+|\%)(([\+\-\/\*b])(\d+))?$/;
var match = regex.exec(s);
//for(var i = 0; i < match.length; i++) console.log('match#%d: %s', i, match[i]);
roll = function (random) {
this.random = random || function () {
return Math.random();
};
};
roll.prototype.validate = function (s) {
return regex.test(s);
};
roll.prototype.parse = function (s) {
if (!this.validate(s)) {
throw new InvalidInputError(s);
}
var match = regex.exec(s),
quantity = match[1], // 2d20+3 => 2
sides = match[2], // 2d20+3 => 20
hasTransformation = !!match[3], // 2d20+3 => true
operator = match[4], // 2d20+3 => "+"
transformationParameter = match[5]; // 2d20+3 => 3
return {
quantity: match[1]
? match[1]
: 1,
sides: match[2] === '%'
? 100
: match[2],
transformations: match[3]
? transformationKeys[match[4]](match[5])
: ['sum'],
toString: function(){
return s;
}
};
},
roll: function(input){
if(typeof input === 'string')
quantity: quantity ? parseInt(quantity, 10) : 1,
sides: sides === '%' ? 100 : parseInt(sides, 10),
transformations: hasTransformation ? transformationKeys[operator](parseInt(transformationParameter, 10)) : ['sum'],
toString: function () {
return s;
}
};
};
roll.prototype.roll = function (input) {
if (!input) {
throw new InvalidInputError();
} else if (typeof input === 'string') {
input = this.parse(input);
var rolled = [];
for(var i = 0; i < input.quantity; i++)
}
var rolled = [],
calculations = [];
while (rolled.length < input.quantity) {
rolled.push(Math.floor((this.random() * input.sides) + 1));
var calculations = [];
var result = rolled;
for(var i = 0; i < input.transformations.length; i++){
calculations.unshift(result);
var transformation = input.transformations[i];
var transformationFunction =
typeof transformation === 'function'
? transformation
: transformationFunctions[
typeof transformation === 'string'
? transformation
: transformation[0]
];
result = transformationFunction(
result,
typeof transformation === 'string' ||
typeof transformation === 'function'
? null
: transformation[1]
);
}
calculations.unshift(result);
calculations = input.transformations.reduce(function (previous, transformation) {
var transformationFunction,
transformationAdditionalParameter;
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]
transformationFunction = transformationFunctions[transformation[0]]; // fn for "add"
transformationAdditionalParameter = transformation[1]; // 3
}
previous.unshift(transformationFunction(previous[0], transformationAdditionalParameter));
return previous;
}, [rolled]);
return {

@@ -138,6 +79,10 @@ input: input,

rolled: calculations[calculations.length - 1],
result: result
result: calculations[0]
};
}
};
};
module.exports = roll;
module.exports.InvalidInputError = InvalidInputError;
}());
{
"name": "roll",
"version": "1.0.0",
"author": "Troy Goode <troygoode@gmail.com> (https://github.com/troygoode/)",
"name": "roll",
"version": "0.3.2",
"description": "node.js package for rolling dice and adding modifiers. ex: 2d6+1",
"keywords": ["roll", "random", "dice", "games", "rpg", "role playing"],
"keywords": [
"roll",
"random",
"dice",
"games",
"rpg",
"role playing"
],
"homepage": "https://github.com/troygoode/node-roll/",

@@ -20,12 +27,27 @@ "repository": {

"licenses": [
{"type": "MIT", "url": "http://www.opensource.org/licenses/mit-license.php"}
{
"type": "MIT",
"url": "http://www.opensource.org/licenses/mit-license.php"
}
],
"bugs": { "url": "https://github.com/TroyGoode/node-roll/issues" },
"bugs": {
"url": "https://github.com/TroyGoode/node-roll/issues"
},
"main": "./lib/index.js",
"engines": {
"node": ">=0.4.0"
"node": ">=0.10.0"
},
"dependencies": {},
"devDependencies": {},
"scripts": {},
"dependencies": {
"require-directory": "^2.0.0"
},
"devDependencies": {
"lint": "^1.1.2",
"mocha": "^1.18.2",
"should": "^3.3.1",
"srand": "^1.1.4"
},
"scripts": {
"test": "./node_modules/mocha/bin/mocha",
"lint": "./node_modules/lint/bin/node-lint lib test"
},
"bin": {

@@ -32,0 +54,0 @@ "roll": "bin/roll"

npm-debug.log
var roll = require("./lib/index");
var result1 = roll.roll("2d6+3"); // same as below
var result2 = roll.roll({
quantity: 2,
sides: 6,
transformations: [
'sum',
['add', 3]
]
});
console.log(result2.rolled); //[5, 2]
console.log(result2.result); //10 (because 7 from above + 3)
var dropOnes = function(result){
var nextResult = [];
for(var i = 0; i < result.length; i++)
if(result[i] !== 1)
nextResult.push(result[i]);
return nextResult;
};
var result3 = roll.roll({
quantity: 5,
sides: 4,
transformations: [
dropOnes,
'sum'
]
});
console.log(result3);

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet