- Notifications
You must be signed in to change notification settings - Fork0
🐉 Parser for classic (2d6+1), simple (2 6 1), and WoD (4d10!>6f1) dice rolls.
License
edloidas/roll-parser
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Parser for classic (2d6+1), simple (2 6 1), and WoD (4d10!>6f1) dice rolls.
Please review theAPI documentation.
npm install roll-parser
Then in the console or JS file:
constrollParser=require('roll-parser');
<scriptsrc="https://unpkg.com/roll-parser/dist/roll-parser.js"></script>
Minified version:
<scriptsrc="https://unpkg.com/roll-parser/dist/roll-parser.min.js"></script>
Then access all functions fromrollParser
object.
Install package globally:
npm install -g roll-parser
Then in the console:
$ roll-parser [options] [<rolls>]
Runroll-parser --help
for more details.
const{ parse, roll, parseAndRoll, Roll}=require('roll-parser');// `parse()` function parses any notation and returns `Roll` or `WodRoll` object//=> { dice: 6, count: 4, modifier: 1 }constparsedRoll=parse('4d6+1');// `Roll` or `WodRoll` can be stringified//=> '4d6+1'constrollNotation=parsedRoll.toString();//=> { notation: '4d6+1', value: 16, rolls: [3, 1, 6, 5] }constresult1=roll(parsedRoll);//=> { notation: '2d20-3', value: 23, rolls: [11, 15] }constresult2=roll(newRoll(20,2,-3));// Can also accept plain objects//=> { notation: '2d10>7', value: 1, rolls: [4, 8] }constresult3=roll({dice:10,count:2,success:7});// `parseAndRoll()` function can parse any notation and then roll the dice// Any invalid arguments, except `null` or `undefined`, will be parsed as default `Roll`//=> { notation: '3d10!>8f1', value: 2, rolls: [3, 10, 7, 9] }constresult4=parseAndRoll('3d10!>8f1');//=> '(3d10!>8f1) 2 [3,10,7,9]'constresultNotation=result4.toString();
Specific parsers can be used.
Classic (D&D):
const{ parseClassicRoll, rollClassic, parseAndRollClassic, Roll}=require('roll-parser');//=> { dice: 10, count: 1, modifier: 0 }constparsedRoll=parseClassicRoll('d10');//=> { notation: 'd10', value: 7, rolls: [7] }constresult1=rollClassic(parsedRoll);//=> { notation: '2d20', value: 26, rolls: [11, 15] }constresult2=rollClassic(newRoll(20,2));//=> { notation: '4d10+1', value: 22, rolls: [4, 6, 2, 9] }constresult3=rollClassic({dice:10,count:4,modifier:1});//=> { notation: '3d6', value: 15, rolls: [6, 6, 3] }constresult4=parseAndRollClassic('3d6');
WoD (World of Darkness):
const{ parseWodRoll, rollWod, parseAndRollWod, WodRoll}=require('roll-parser');//=> { dice: 10, count: 1, again: false, success: 6, fail: 0 }constparsedRoll=parseWodRoll('d10>6');// Returns notation, number of success rolls and list of all dice rolls//=> { notation: 'd10', value: 1, rolls: [7] }constresult1=rollWod(parsedRoll);//=> { notation: '4d10>6f1', value: 1, rolls: [4, 10, 5, 2] }constresult2=rollWod(newWodRoll(10,4,false,6,1));//=> { notation: '4d10!>8f1', value: 22, rolls: [1, 8, 5, 10, 10, 4] }constresult3=rollWod({dice:10,count:2,again:true,success:8,fail:1});//=> { notation: '4d10>7f4', value: 1, rolls: [6, 3, 8, 4] }constresult4=parseAndRollWod('4d10>7f4');
Simple (D&D, space-separated):
const{ parseSimpleRoll, parseAndRollSimple}=require('roll-parser');//=> { dice: 10, count: 1, modifier: 0 }constparsedRoll=parseSimpleRoll('10');//=> { notation: '4d10-1', value: 23, rolls: [3, 6, 8, 7] }constresult=parseAndRollSimple('4 10 -1');
Random number generator can be used to roll the dice.
const{ random}=require('roll-parser');//=> 84 - d100-like rollrandom(100);//=> 7 - d10-like rollrandom(10);//=> [2, 5, 2, 6] - 4d6-like roll[...Array(4)].map(()=>random(6));
Even so the parse&roll functions uses checks to convert non-standard objects toRoll
orWodRoll
, explicit conversion can be used in some cases:
const{ convert}=require('roll-parser');//=> new Roll(undefined, 4, -3)convert({count:4,modifier:-3});//=> new WodRoll(10, 6, true, undefined, 2)convert({dice:10,count:6,again:true,fail:2});
Please review thechangelog.
♥roll-parser and want to get involved?
Please, check theguide first.
About
🐉 Parser for classic (2d6+1), simple (2 6 1), and WoD (4d10!>6f1) dice rolls.