From cd0feb6ffd0795ac375a20bb89b00d080d07d858 Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Mon, 11 Sep 2017 16:22:45 -0400 Subject: Format large pools correctly --- bin/dicebag.js | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/bin/dicebag.js b/bin/dicebag.js index 6e88ae6..be0a850 100755 --- a/bin/dicebag.js +++ b/bin/dicebag.js @@ -3,17 +3,35 @@ const { parse, roll, pool } = require('../index.js') +const rollRoller = { + roll: roll, + format: value => value +} + +const poolRoller = { + roll: pool, + format: values => { + if (values.length === 0) { + return '[]' + } else { + return '[ ' + values.reduce((acc, next) => ( + acc + ', ' + String(next) + )) + ' ]' + } + } +} + const parseArgs = () => { const args = process.argv.slice(2) const parsedArgs = { - roller: roll, + roller: rollRoller, expression: null } while (args.length > 0) { const arg = args.shift() if (arg === '-p') { - parsedArgs.roller = pool + parsedArgs.roller = poolRoller } else { parsedArgs.expression = arg } @@ -25,7 +43,8 @@ const parseArgs = () => { const rollDie = (string, roller) => { try { const die = parse(string) - console.log(roller(die)) + const roll = roller.roll(die) + console.log(roller.format(roll)) } catch (error) { console.log(error.message) } -- cgit v1.2.3