Skip to content

Commit db187fb

Browse files
committed
detailed roll option
1 parent e902d1b commit db187fb

File tree

3 files changed

+61
-5
lines changed

3 files changed

+61
-5
lines changed

README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const Dice = require('dice-notation-js');
3131

3232
## Usage
3333

34-
Exposes two functions, `Dice()` and `Dice.parse()`.
34+
Exposes three functions, `Dice()`, `Dice.detailed()` and `Dice.parse()`.
3535

3636
### `Dice()`
3737

@@ -68,6 +68,31 @@ Dice('3d6', prng) + 2;
6868
Dice(3, 6, prng) + 2;
6969
```
7070

71+
### `Dice.detailed()`
72+
73+
**Syntax**:
74+
75+
- `Dice.detailed(notation [, randomFunction])`
76+
- `Dice.detailed(number, type [, randomFunction])`
77+
78+
Takes a string of dice notation, rolls it like `Dice()`, but returns additional details for your use.
79+
80+
**Arguments**:
81+
82+
- `notation` (*string*): You can pass the funciton a string of dice notation, e.g., `3d6+2`, or `1d20`.
83+
- `number` (*number*): You can pass the function a number of dice to roll and the number of sides each die should have. This argument is the number of dice to roll.
84+
- `type` (*number*): You can pass the function a number of dice to roll and the number of sides each die should have. This argument is the number of sides each die should have.
85+
- `randomFunction` (*function*) *optional*: You may pass a function that returns a random number between 0 and 1 that will be used in place of `Math.random()`, such as to use a seedable PRNG.
86+
87+
**Examples**
88+
89+
```javascript
90+
var roll = Dice.detailed('3d6+10');
91+
92+
console.log(roll);
93+
// -> { number: 3, type: 6, modifier: 10, rolls: [ 2, 1, 6 ], result: 19 }
94+
```
95+
7196
### `Dice.parse()`
7297

7398
**Syntax**: `Dice.parse(notation)`

dice.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,18 @@
5454
if (!rnd) {
5555
rnd = Math.random;
5656
}
57+
var rolls = [];
5758
var result = 0;
5859
for (var i = 0; i < a; i++) {
5960
var die = 0;
6061
die = Math.floor(rnd() * b) + 1;
6162
result += die;
63+
rolls.push(die);
6264
}
63-
return result;
65+
return {
66+
rolls : rolls,
67+
result : result
68+
};
6469
}
6570

6671
function rollMe (a, b, rnd) {
@@ -79,13 +84,23 @@
7984
if (typeof b === 'function') {
8085
rnd = b;
8186
}
82-
return roll(toRoll.number, toRoll.type, rnd) + toRoll.modifier;
87+
var rolled = roll(toRoll.number, toRoll.type, rnd);
88+
rolled.result += toRoll.modifier;
89+
Object.assign(toRoll, rolled);
90+
return toRoll;
8391
}
8492

85-
var Dice = rollMe;
93+
function Dice (a, b, rnd) {
94+
return rollMe(a, b, rnd).result;
95+
}
96+
97+
function detailedRoll (a, b, rnd) {
98+
return rollMe(a, b, rnd);
99+
}
86100

87101
Object.assign(Dice, {
88-
parse : parse
102+
parse : parse,
103+
detailed : detailedRoll
89104
});
90105

91106
return Dice;

test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,22 @@ describe('Dice()', function () {
4141
});
4242
});
4343

44+
describe('Dice.detailed()', function () {
45+
it('should create accurate, detailed rolls', function () {
46+
var detailed = dice.detailed('3d6 + 10', prng);
47+
console.log(detailed);
48+
assert.strictEqual(3, detailed.rolls.length);
49+
assert.strictEqual(3, detailed.number);
50+
assert.strictEqual(10, detailed.modifier);
51+
assert.strictEqual(19, detailed.result);
52+
});
53+
it('should output a valid object', function () {
54+
var detailed = dice.detailed('1d4', prng);
55+
assert.strictEqual("object", typeof detailed);
56+
assert.strictEqual('{"number":1,"type":4,"modifier":0,"rolls":[3],"result":3}', JSON.stringify(detailed));
57+
});
58+
});
59+
4460
describe('Dice.parse()', function () {
4561
const parse = dice.parse('3d6');
4662
const parsePos = dice.parse('3d6+2');

0 commit comments

Comments
 (0)