Skip to content

Commit

Permalink
Add tsconfig and fix a typing bug
Browse files Browse the repository at this point in the history
  • Loading branch information
juusaw committed Feb 15, 2019
1 parent 7e774ac commit f5ebe90
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/dice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function validateDiceString(input: string): boolean {

function diceToDieArr({ amount, sides }: Dice): Die[] {
if (amount === 0) return []
return new Array(Math.abs(amount)).fill(amount / Math.abs(amount) * sides)
return new Array(Math.abs(amount)).fill({sides: amount / Math.abs(amount) * sides})
}

export function parseDice(inputStr: string): Dice[] | null {
Expand Down Expand Up @@ -74,7 +74,7 @@ export function calculateMin(dice: Dice[]) {
}

export function getDistribution(dice: Dice[]) {
const diceRanges = dice.flatMap(diceToDieArr).map(rangeTo)
const diceRanges = dice.flatMap(diceToDieArr).map(die => rangeTo(die.sides))
const possibleResults = cartesian(diceRanges)
return possibleResults.map(resultArr => resultArr.reduce((a, b) => a + b, 0)).sort((a, b) => a - b)
}
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function rangeTo(n) {
export function rangeTo(n: number) {
if (n === 0) return []
return new Array(Math.abs(n)).fill(undefined).map((_, i) => n / Math.abs(n) * (i + 1))
}
Expand Down
11 changes: 11 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"target": "ES2016",
"module": "commonjs",
"sourceMap": false,
"strict": true,
"esModuleInterop": true,
"inlineSourceMap": false,
"inlineSources": false
}
}

0 comments on commit f5ebe90

Please sign in to comment.