Skip to content

Commit

Permalink
Merge pull request #11 from n1ru4l/feat-inject-dice-roll
Browse files Browse the repository at this point in the history
feat: inject dice roll and export more types
  • Loading branch information
airjp73 authored Apr 21, 2021
2 parents 236114c + 68a01ae commit ef6e27d
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 35 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ Let's say we want to add a rule to allow us to use `d%` instead of `d100`. We ca

```js
// A convenience function for rolling a dice. You can use something else if you want
import { random } from '@airjp73/dice-notation';

const myRule = {
// Regex to pass to the parser
regex: /\d+d%/,
Expand All @@ -59,7 +57,7 @@ const myRule = {

// Takes the data returned from `tokenize` and returns an array of rolls
// this is so we can see what every individual dice roll was if we want
roll: ({ numDice }) => {
roll: ({ numDice }, random) => {
const rolls = [];
for (let i = 0; i < numDice; i++) {
rolls.push(random(1, 100));
Expand Down
34 changes: 13 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"@types/moo": "^0.5.1",
"jest": "^24.9.0",
"ts-jest": "^24.3.0",
"typescript": "^3.7.5"
"typescript": "^4.2.4"
},
"dependencies": {
"moo": "^0.5.1"
Expand Down
7 changes: 5 additions & 2 deletions src/createDiceRoller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import createRoll from './roll';
import createTokenize from './tokenize';
import simpleDieRoll from './rules/simpleDieRoll';
import constant from './rules/constant';
import defaultRandom from "./util/random"

export const defaultPlugins = {
[simpleDieRoll.typeConstant]: simpleDieRoll,
[constant.typeConstant]: constant,
};

function createDiceRoller(plugins: Plugins = defaultPlugins) {
function createDiceRoller(plugins: Plugins = defaultPlugins, random = defaultRandom) {
const tokenize = createTokenize(plugins);
const rollDice = createRollDice(plugins);
const rollDice = createRollDice(plugins, random);
const tallyRolls = createTallyRolls(plugins);

return {
Expand All @@ -26,4 +27,6 @@ function createDiceRoller(plugins: Plugins = defaultPlugins) {
};
}

export type DiceRoller = ReturnType<typeof createDiceRoller>

export default createDiceRoller;
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ export { default as createDiceRoller } from './createDiceRoller';
export { default as withPlugins } from './withPlugins';
export { default as random } from './util/random';

export type { DiceRoller } from './createDiceRoller';
export type { Random } from "./util/random";
export type { Plugins, DiceRule, Rolls, RollResults } from "./rules/types";
export type { SimpleDiceRollToken } from "./rules/simpleDieRoll";
export type { BaseToken, OpenParenToken, CloseParenToken, OperatorToken, DiceRollToken, Token } from "./tokens";

export const {
tokenize,
calculateFinalResult,
Expand Down
5 changes: 3 additions & 2 deletions src/rollDice.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Plugins, RollResults } from './rules/types';
import { CoreTokenTypes, Token } from './tokens';
import { Random } from './util/random';

function createRollDice(plugins: Plugins) {
function createRollDice(plugins: Plugins, random: Random) {
function rollDice(tokens: Token[]): RollResults {
return tokens.map(token => {
switch (token.type) {
Expand All @@ -10,7 +11,7 @@ function createRollDice(plugins: Plugins) {
case CoreTokenTypes.Operator:
return null;
case CoreTokenTypes.DiceRoll:
return plugins[token.detailType].roll(token.detail);
return plugins[token.detailType].roll(token.detail, random);
}
});
}
Expand Down
3 changes: 2 additions & 1 deletion src/rules/__test__/simpleDieRoll.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { random } from '../../index';
import simpleDieRoll from '../simpleDieRoll';

describe('simpleDieRoll', () => {
Expand All @@ -12,7 +13,7 @@ describe('simpleDieRoll', () => {

describe('roll', () => {
it('should return an array with one number for each dice rolled', () => {
const rolls = simpleDieRoll.roll({ count: 25, numSides: 6 });
const rolls = simpleDieRoll.roll({ count: 25, numSides: 6 }, random);
expect(rolls).toHaveLength(25);
rolls.forEach(val => {
expect(val).toBeLessThanOrEqual(6);
Expand Down
3 changes: 1 addition & 2 deletions src/rules/simpleDieRoll.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { DiceRule } from './types';
import random from '../util/random';

export const SIMPLE_DIE_ROLL = '_SimpleDieRoll';

Expand All @@ -15,7 +14,7 @@ const simpleDieRoll: DiceRule<SimpleDiceRollToken> = {
const [count, numSides] = raw.split('d').map(num => parseInt(num));
return { count, numSides };
},
roll: ({ count, numSides }) =>
roll: ({ count, numSides }, random) =>
new Array(count).fill(0).map(() => random(1, numSides)),
calculateValue: (token, rolls) => rolls.reduce((agg, num) => agg + num, 0),
};
Expand Down
4 changes: 3 additions & 1 deletion src/rules/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Random } from "../util/random";

export interface Plugins {
[key: string]: DiceRule<any>;
}
Expand All @@ -6,7 +8,7 @@ export interface DiceRule<T> {
regex: RegExp;
typeConstant: string;
tokenize: (raw: string) => T;
roll: (token: T) => Rolls;
roll: (token: T, random: Random) => Rolls;
calculateValue: (token: T, rolls: number[]) => number;
}

Expand Down
2 changes: 2 additions & 0 deletions src/util/random.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export type Random = (min: number, max: number) => number

function random(min: number, max: number): number {
return Math.floor(Math.random() * Math.floor(max - min + 1)) + min;
}
Expand Down
15 changes: 13 additions & 2 deletions src/withPlugins.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import { Plugins, DiceRule } from './rules/types';
import createDiceRoller, { defaultPlugins } from './createDiceRoller';
import defaultRandom, { Random } from "./util/random"

function withPlugins(...plugins: DiceRule<any>[]) {
function withPlugins(...plugins: DiceRule<any>[]): ReturnType<typeof createDiceRoller>
function withPlugins(plugins: DiceRule<any>[], random?: Random): ReturnType<typeof createDiceRoller>
function withPlugins(...args: any[]): ReturnType<typeof createDiceRoller> {
let plugins: DiceRule<any>[];
let random = defaultRandom
if (Array.isArray(args[0])) {
plugins = args[0]
random = args[1] || defaultRandom
} else {
plugins = args
}
const customPlugins: Plugins = {};
plugins.forEach(plugin => {
plugins.forEach((plugin) => {
customPlugins[plugin.typeConstant] = plugin;
});

Expand Down

0 comments on commit ef6e27d

Please sign in to comment.