@airjp73/dice-notation
Advanced tools
Comparing version 1.3.0 to 2.0.0
@@ -1,8 +0,10 @@ | ||
import { Plugins } from './rules/types'; | ||
import type { Plugins } from './rules/types'; | ||
import calculateFinalResult from './calculateFinalResult'; | ||
import defaultRandom from "./util/random"; | ||
import type { Random } from './util/random'; | ||
export declare const defaultPlugins: { | ||
[x: string]: import("./rules/types").DiceRule<import("./rules/simpleDieRoll").SimpleDiceRollToken> | import("./rules/types").DiceRule<number>; | ||
}; | ||
declare function createDiceRoller(plugins?: Plugins, random?: typeof defaultRandom): { | ||
declare function createDiceRoller(plugins?: Plugins, { random }?: { | ||
random?: Random; | ||
}): { | ||
tokenize: (notation: string) => import("./tokens").Token[]; | ||
@@ -9,0 +11,0 @@ calculateFinalResult: typeof calculateFinalResult; |
@@ -5,6 +5,6 @@ export { default as createDiceRoller } from './createDiceRoller'; | ||
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 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 declare const tokenize: (notation: string) => import("./tokens").Token[], calculateFinalResult: typeof import("./calculateFinalResult").default, rollDice: (tokens: import("./tokens").Token[]) => import("./rules/types").RollResults, tallyRolls: (tokens: import("./tokens").Token[], rolls: import("./rules/types").RollResults) => import("./tallyRolls").RollTotal[], roll: (notation: string) => import("./roll").RollInformation; |
@@ -19,5 +19,5 @@ "use strict"; | ||
}; | ||
function createDiceRoller(plugins = exports.defaultPlugins, random = random_1.default) { | ||
function createDiceRoller(plugins = exports.defaultPlugins, { random = random_1.default } = {}) { | ||
const tokenize = tokenize_1.default(plugins); | ||
const rollDice = rollDice_1.default(plugins, random); | ||
const rollDice = rollDice_1.default(plugins, { random }); | ||
const tallyRolls = tallyRolls_1.default(plugins); | ||
@@ -24,0 +24,0 @@ return { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tokens_1 = require("./tokens"); | ||
function createRollDice(plugins, random) { | ||
function createRollDice(plugins, opts) { | ||
function rollDice(tokens) { | ||
return tokens.map(token => { | ||
return tokens.map((token) => { | ||
switch (token.type) { | ||
@@ -13,3 +13,3 @@ case tokens_1.CoreTokenTypes.CloseParen: | ||
case tokens_1.CoreTokenTypes.DiceRoll: | ||
return plugins[token.detailType].roll(token.detail, random); | ||
return plugins[token.detailType].roll(token.detail, opts); | ||
} | ||
@@ -16,0 +16,0 @@ }); |
@@ -8,6 +8,6 @@ "use strict"; | ||
typeConstant: exports.CONSTANT, | ||
tokenize: raw => parseInt(raw), | ||
tokenize: (raw) => parseInt(raw), | ||
roll: () => [], | ||
calculateValue: token => token, | ||
calculateValue: (token) => token, | ||
}; | ||
exports.default = constant; |
@@ -8,9 +8,9 @@ "use strict"; | ||
typeConstant: exports.SIMPLE_DIE_ROLL, | ||
tokenize: raw => { | ||
const [count, numSides] = raw.split('d').map(num => parseInt(num)); | ||
tokenize: (raw) => { | ||
const [count, numSides] = raw.split('d').map((num) => parseInt(num)); | ||
return { count, numSides }; | ||
}, | ||
roll: ({ count, numSides }, random) => new Array(count).fill(0).map(() => random(1, numSides)), | ||
roll: ({ count, numSides }, { random }) => new Array(count).fill(0).map(() => random(1, numSides)), | ||
calculateValue: (token, rolls) => rolls.reduce((agg, num) => agg + num, 0), | ||
}; | ||
exports.default = simpleDieRoll; |
@@ -32,3 +32,3 @@ "use strict"; | ||
}; | ||
Object.values(plugins).forEach(plugin => { | ||
Object.values(plugins).forEach((plugin) => { | ||
rules[plugin.typeConstant] = plugin.regex; | ||
@@ -40,3 +40,3 @@ }); | ||
return Array.from(lexer) | ||
.filter(token => token.type !== WHITE_SPACE) | ||
.filter((token) => token.type !== WHITE_SPACE) | ||
.map(processToken); | ||
@@ -43,0 +43,0 @@ } |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const createDiceRoller_1 = __importStar(require("./createDiceRoller")); | ||
const random_1 = __importDefault(require("./util/random")); | ||
function withPlugins(...args) { | ||
let plugins; | ||
let random = random_1.default; | ||
if (Array.isArray(args[0])) { | ||
plugins = args[0]; | ||
random = args[1] || random_1.default; | ||
} | ||
else { | ||
plugins = args; | ||
} | ||
const createDiceRoller_1 = require("./createDiceRoller"); | ||
function withPlugins(...plugins) { | ||
const customPlugins = {}; | ||
@@ -43,4 +11,4 @@ plugins.forEach((plugin) => { | ||
const allPlugins = { ...customPlugins, ...createDiceRoller_1.defaultPlugins }; | ||
return createDiceRoller_1.default(allPlugins); | ||
return allPlugins; | ||
} | ||
exports.default = withPlugins; |
@@ -8,3 +8,3 @@ import createRollDice from './rollDice'; | ||
import constant from './rules/constant'; | ||
import defaultRandom from "./util/random"; | ||
import defaultRandom from './util/random'; | ||
export const defaultPlugins = { | ||
@@ -14,5 +14,5 @@ [simpleDieRoll.typeConstant]: simpleDieRoll, | ||
}; | ||
function createDiceRoller(plugins = defaultPlugins, random = defaultRandom) { | ||
function createDiceRoller(plugins = defaultPlugins, { random = defaultRandom } = {}) { | ||
const tokenize = createTokenize(plugins); | ||
const rollDice = createRollDice(plugins, random); | ||
const rollDice = createRollDice(plugins, { random }); | ||
const tallyRolls = createTallyRolls(plugins); | ||
@@ -19,0 +19,0 @@ return { |
import { CoreTokenTypes } from './tokens'; | ||
function createRollDice(plugins, random) { | ||
function createRollDice(plugins, opts) { | ||
function rollDice(tokens) { | ||
return tokens.map(token => { | ||
return tokens.map((token) => { | ||
switch (token.type) { | ||
@@ -11,3 +11,3 @@ case CoreTokenTypes.CloseParen: | ||
case CoreTokenTypes.DiceRoll: | ||
return plugins[token.detailType].roll(token.detail, random); | ||
return plugins[token.detailType].roll(token.detail, opts); | ||
} | ||
@@ -14,0 +14,0 @@ }); |
@@ -5,6 +5,6 @@ export const CONSTANT = '_Constant'; | ||
typeConstant: CONSTANT, | ||
tokenize: raw => parseInt(raw), | ||
tokenize: (raw) => parseInt(raw), | ||
roll: () => [], | ||
calculateValue: token => token, | ||
calculateValue: (token) => token, | ||
}; | ||
export default constant; |
@@ -5,9 +5,9 @@ export const SIMPLE_DIE_ROLL = '_SimpleDieRoll'; | ||
typeConstant: SIMPLE_DIE_ROLL, | ||
tokenize: raw => { | ||
const [count, numSides] = raw.split('d').map(num => parseInt(num)); | ||
tokenize: (raw) => { | ||
const [count, numSides] = raw.split('d').map((num) => parseInt(num)); | ||
return { count, numSides }; | ||
}, | ||
roll: ({ count, numSides }, random) => new Array(count).fill(0).map(() => random(1, numSides)), | ||
roll: ({ count, numSides }, { random }) => new Array(count).fill(0).map(() => random(1, numSides)), | ||
calculateValue: (token, rolls) => rolls.reduce((agg, num) => agg + num, 0), | ||
}; | ||
export default simpleDieRoll; |
@@ -11,3 +11,3 @@ import * as moo from 'moo'; | ||
}; | ||
Object.values(plugins).forEach(plugin => { | ||
Object.values(plugins).forEach((plugin) => { | ||
rules[plugin.typeConstant] = plugin.regex; | ||
@@ -19,3 +19,3 @@ }); | ||
return Array.from(lexer) | ||
.filter(token => token.type !== WHITE_SPACE) | ||
.filter((token) => token.type !== WHITE_SPACE) | ||
.map(processToken); | ||
@@ -22,0 +22,0 @@ } |
@@ -1,13 +0,3 @@ | ||
import createDiceRoller, { defaultPlugins } from './createDiceRoller'; | ||
import defaultRandom from "./util/random"; | ||
function withPlugins(...args) { | ||
let plugins; | ||
let random = defaultRandom; | ||
if (Array.isArray(args[0])) { | ||
plugins = args[0]; | ||
random = args[1] || defaultRandom; | ||
} | ||
else { | ||
plugins = args; | ||
} | ||
import { defaultPlugins } from './createDiceRoller'; | ||
function withPlugins(...plugins) { | ||
const customPlugins = {}; | ||
@@ -19,4 +9,4 @@ plugins.forEach((plugin) => { | ||
const allPlugins = { ...customPlugins, ...defaultPlugins }; | ||
return createDiceRoller(allPlugins); | ||
return allPlugins; | ||
} | ||
export default withPlugins; |
import { Plugins, RollResults } from './rules/types'; | ||
import { Token } from './tokens'; | ||
import { Random } from './util/random'; | ||
declare function createRollDice(plugins: Plugins, random: Random): (tokens: Token[]) => RollResults; | ||
declare function createRollDice(plugins: Plugins, opts: { | ||
random: Random; | ||
}): (tokens: Token[]) => RollResults; | ||
export default createRollDice; |
@@ -1,2 +0,2 @@ | ||
import { Random } from "../util/random"; | ||
import type { Random } from '../util/random'; | ||
export interface Plugins { | ||
@@ -9,3 +9,5 @@ [key: string]: DiceRule<any>; | ||
tokenize: (raw: string) => T; | ||
roll: (token: T, random: Random) => Rolls; | ||
roll: (token: T, opts: { | ||
random: Random; | ||
}) => Rolls; | ||
calculateValue: (token: T, rolls: number[]) => number; | ||
@@ -12,0 +14,0 @@ } |
@@ -1,6 +0,3 @@ | ||
import { DiceRule } from './rules/types'; | ||
import createDiceRoller from './createDiceRoller'; | ||
import { Random } from "./util/random"; | ||
declare function withPlugins(...plugins: DiceRule<any>[]): ReturnType<typeof createDiceRoller>; | ||
declare function withPlugins(plugins: DiceRule<any>[], random?: Random): ReturnType<typeof createDiceRoller>; | ||
import type { Plugins, DiceRule } from './rules/types'; | ||
declare function withPlugins(...plugins: DiceRule<any>[]): Plugins; | ||
export default withPlugins; |
{ | ||
"name": "@airjp73/dice-notation", | ||
"version": "1.3.0", | ||
"version": "2.0.0", | ||
"description": "A js library for parsing dice notation", | ||
@@ -14,3 +14,5 @@ "module": "dist/module/index.js", | ||
"build": "npm run build:module && npm run build:main", | ||
"prepublish": "npm run build" | ||
"prepublish": "npm run build", | ||
"format": "prettier --write .", | ||
"format:check": "prettier --check ." | ||
}, | ||
@@ -35,3 +37,6 @@ "files": [ | ||
"@types/moo": "^0.5.1", | ||
"husky": "4.3.8", | ||
"jest": "^24.9.0", | ||
"lint-staged": "10.5.4", | ||
"prettier": "2.2.1", | ||
"ts-jest": "^24.3.0", | ||
@@ -42,3 +47,13 @@ "typescript": "^4.2.4" | ||
"moo": "^0.5.1" | ||
}, | ||
"lint-staged": { | ||
"**/*.{ts,json,js}": [ | ||
"prettier --write" | ||
] | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "lint-staged" | ||
} | ||
} | ||
} |
@@ -56,3 +56,3 @@ # @airjp73/dice-notation | ||
// in this case we only care about how many d100s we need to roll | ||
tokenize: raw => ({ numDice: parseInt(raw.split('d')[0]) }), | ||
tokenize: (raw) => ({ numDice: parseInt(raw.split('d')[0]) }), | ||
@@ -91,6 +91,6 @@ // Takes the data returned from `tokenize` and returns an array of rolls | ||
```js | ||
import { withPlugins } from '@airjp73/dice-notation'; | ||
import { withPlugins, createDiceRoller } from '@airjp73/dice-notation'; | ||
// You can use `roll` the same as before, but now your custom rules are injected into it. | ||
const { roll } = withPlugins(myRule); | ||
const { roll } = createDiceRoller(withPlugins(myRule)); | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
42576
8
962