Skip to content

Commit

Permalink
Address roll of only a constant in parser
Browse files Browse the repository at this point in the history
  • Loading branch information
juusaw committed Mar 5, 2019
1 parent 9729175 commit 1107d16
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/dice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export function parseDice(inputStr: string): Dice[] | null {
const constStrs = (inputStr.match(/([\+|-]\d+)[+|-]/g) || []).map(x => x.slice(0, -1))
.concat((inputStr.match(/([\+|-]\d+)$/) || []).slice(1)) // trailing
.concat((inputStr.match(/(^\d+)[+|-]/) || []).slice(1)) // leading
.concat((inputStr.match(/^(^\d+)$/) || []).slice(1)) // lone
const dice = diceStrs.map(ds => {
const [amount, sides] = ds.split('d').map(d => parseInt(d, 10))
return { amount: Number.isNaN(amount) ? 1 : amount, sides }
Expand Down
4 changes: 3 additions & 1 deletion test/dice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ test('Parser', () => {
{ string: '3d8-10', result: [{ amount: 3, sides: 8}, { amount: -10, sides: 1 }] },
{ string: '3dd', result: null },
{ string: '1d6+-1', result: null },
{ string: 'd6', result: [{ amount: 1, sides: 6 }]}
{ string: 'd6', result: [{ amount: 1, sides: 6 }]},
{ string: '10', result: [{ amount: 10, sides: 1}]},
{ string: '-10', result: [{ amount: -10, sides: 1}]},
]
cases.forEach(({ string, result }) =>
expect(dice.parseDice(string)).toEqual(result))
Expand Down

0 comments on commit 1107d16

Please sign in to comment.