Skip to content

Commit 5da278d

Browse files
author
Aaron Pettengill
committed
Improve documentation around inspecting roll details
1 parent dec2b99 commit 5da278d

File tree

1 file changed

+37
-8
lines changed

1 file changed

+37
-8
lines changed

README.md

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ A flexible and pluggable js library for parsing dice notation. It has:
1313
npm install --save @airjp73/dice-notation
1414
```
1515

16-
## Usage
16+
## Simple Usage
1717

1818
If all you want to do is parse some dice notation and get the result you can import `roll` and pass in the notation.
1919

@@ -25,16 +25,39 @@ const { result } = roll('1d6 + 3');
2525

2626
It is recommended to use an IDE or text editor that can show you the Typescript types for more in-depth documentation.
2727

28-
## Inspecting tokens
28+
## Inspecting individual rolls
2929

30-
If you want to get more data about the notation (like to do something more interactive with the ui), you can use the `tokenize` function.
30+
If you want to get the results of individual dice rolls or more info about the notation itself, you can use these functions.
31+
The `roll` function from the Simple Usage section is essentially just a wrapper for these 4 functions.
3132

3233
```js
33-
import { tokenize } from '@airjp73/dice-notation';
34-
35-
const tokens = tokenize('1d6 + 3');
34+
import {
35+
tokenize,
36+
rollDice,
37+
tallyRolls,
38+
calculateFinalResult,
39+
} from '@airjp73/dice-notation';
40+
41+
// Gets the tokens from the lexer
42+
// Example: [DiceRollToken, OperatorToken, DiceRollToken]
43+
const tokens = tokenize('2d6 + 3d4');
44+
45+
// Rolls any dice roll tokens and returns all the individual rolls.
46+
// rolls[i] contains the all rolls for the dice roll at tokens[i]
47+
// Example: [[3, 1], null, [1, 3, 2]]
48+
const rolls = rollDice(tokens);
49+
50+
// Takes the rolls and totals them
51+
// Example: [4, null, 6]
52+
const rollTotals = tallyRolls(tokens, rolls);
53+
54+
// Get the final result of the roll
55+
// Example: 10
56+
const result = calculateFinalResult(tokens, rollTotals);
3657
```
3758

59+
It's broken up this way to allow as much flexibility as possible for how you want to display the information and what kind of custom dice rules you might want.
60+
3861
## Custom dice rules
3962

4063
The default `roll` function only supports basic dice notation. If you want to support something more than that, you need a custom dice rule.
@@ -89,6 +112,12 @@ Once you've created your custom rule, you need to create new roll methods like s
89112
```js
90113
import { withPlugins, createDiceRoller } from '@airjp73/dice-notation';
91114

92-
// You can use `roll` the same as before, but now your custom rules are injected into it.
93-
const { roll } = createDiceRoller(withPlugins(myRule));
115+
// You can use the same roll functions as before, but now your custom rules are injected into it.
116+
const {
117+
roll,
118+
tokenize,
119+
rollDice,
120+
tallyRolls,
121+
calculateFinalResult,
122+
} = createDiceRoller(withPlugins(myRule));
94123
```

0 commit comments

Comments
 (0)