Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@airjp73/dice-notation

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@airjp73/dice-notation - npm Package Compare versions

Comparing version 1.2.0 to 1.3.0

6

dist/createDiceRoller.d.ts
import { Plugins } from './rules/types';
import calculateFinalResult from './calculateFinalResult';
import defaultRandom 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): {
declare function createDiceRoller(plugins?: Plugins, random?: typeof defaultRandom): {
tokenize: (notation: string) => import("./tokens").Token[];
calculateFinalResult: typeof calculateFinalResult;
rollDice: (tokens: import("./tokens").Token[]) => import("./rules/types").RollResults;
tallyRolls: (tokens: import("./tokens").Token[], rolls: import("./rules/types").RollResults) => (number | null)[];
tallyRolls: (tokens: import("./tokens").Token[], rolls: import("./rules/types").RollResults) => import("./tallyRolls").RollTotal[];
roll: (notation: string) => import("./roll").RollInformation;
};
export declare type DiceRoller = ReturnType<typeof createDiceRoller>;
export default createDiceRoller;
export { default as createDiceRoller } from './createDiceRoller';
export { default as withPlugins } from './withPlugins';
export { default as random } from './util/random';
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) => (number | null)[], roll: (notation: string) => import("./roll").RollInformation;
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 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;

@@ -6,2 +6,3 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.defaultPlugins = void 0;
const rollDice_1 = __importDefault(require("./rollDice"));

@@ -14,2 +15,3 @@ const tallyRolls_1 = __importDefault(require("./tallyRolls"));

const constant_1 = __importDefault(require("./rules/constant"));
const random_1 = __importDefault(require("./util/random"));
exports.defaultPlugins = {

@@ -19,5 +21,5 @@ [simpleDieRoll_1.default.typeConstant]: simpleDieRoll_1.default,

};
function createDiceRoller(plugins = exports.defaultPlugins) {
function createDiceRoller(plugins = exports.defaultPlugins, random = random_1.default) {
const tokenize = tokenize_1.default(plugins);
const rollDice = rollDice_1.default(plugins);
const rollDice = rollDice_1.default(plugins, random);
const tallyRolls = tallyRolls_1.default(plugins);

@@ -24,0 +26,0 @@ return {

@@ -7,9 +7,10 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.roll = exports.tallyRolls = exports.rollDice = exports.calculateFinalResult = exports.tokenize = exports.random = exports.withPlugins = exports.createDiceRoller = void 0;
const createDiceRoller_1 = __importDefault(require("./createDiceRoller"));
var createDiceRoller_2 = require("./createDiceRoller");
exports.createDiceRoller = createDiceRoller_2.default;
Object.defineProperty(exports, "createDiceRoller", { enumerable: true, get: function () { return __importDefault(createDiceRoller_2).default; } });
var withPlugins_1 = require("./withPlugins");
exports.withPlugins = withPlugins_1.default;
Object.defineProperty(exports, "withPlugins", { enumerable: true, get: function () { return __importDefault(withPlugins_1).default; } });
var random_1 = require("./util/random");
exports.random = random_1.default;
Object.defineProperty(exports, "random", { enumerable: true, get: function () { return __importDefault(random_1).default; } });
_a = createDiceRoller_1.default(), exports.tokenize = _a.tokenize, exports.calculateFinalResult = _a.calculateFinalResult, exports.rollDice = _a.rollDice, exports.tallyRolls = _a.tallyRolls, exports.roll = _a.roll;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPrecedence = (op) => {
exports.getPrecedence = void 0;
const getPrecedence = (op) => {
if (op === '+' || op === '-') {

@@ -11,1 +12,2 @@ return 2;

};
exports.getPrecedence = getPrecedence;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tokens_1 = require("./tokens");
function createRollDice(plugins) {
function createRollDice(plugins, random) {
function rollDice(tokens) {

@@ -13,3 +13,3 @@ return tokens.map(token => {

case tokens_1.CoreTokenTypes.DiceRoll:
return plugins[token.detailType].roll(token.detail);
return plugins[token.detailType].roll(token.detail, random);
}

@@ -16,0 +16,0 @@ });

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CONSTANT = void 0;
exports.CONSTANT = '_Constant';

@@ -4,0 +5,0 @@ const constant = {

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const random_1 = __importDefault(require("../util/random"));
exports.SIMPLE_DIE_ROLL = void 0;
exports.SIMPLE_DIE_ROLL = '_SimpleDieRoll';

@@ -15,5 +12,5 @@ const simpleDieRoll = {

},
roll: ({ count, numSides }) => new Array(count).fill(0).map(() => random_1.default(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;
"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 (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
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;

@@ -8,0 +20,0 @@ };

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.constantToken = exports.diceRollToken = exports.operatorToken = exports.closeParenToken = exports.openParenToken = exports.CoreTokenTypes = void 0;
const simpleDieRoll_1 = require("./rules/simpleDieRoll");

@@ -13,3 +14,3 @@ const constant_1 = require("./rules/constant");

// Token builders used for constructing test data
exports.openParenToken = (position, content) => ({
const openParenToken = (position, content) => ({
type: CoreTokenTypes.OpenParen,

@@ -19,3 +20,4 @@ position,

});
exports.closeParenToken = (position, content) => ({
exports.openParenToken = openParenToken;
const closeParenToken = (position, content) => ({
type: CoreTokenTypes.CloseParen,

@@ -25,3 +27,4 @@ position,

});
exports.operatorToken = (operator, position, content) => ({
exports.closeParenToken = closeParenToken;
const operatorToken = (operator, position, content) => ({
type: CoreTokenTypes.Operator,

@@ -32,3 +35,4 @@ position,

});
exports.diceRollToken = (count, numSides, position, content) => ({
exports.operatorToken = operatorToken;
const diceRollToken = (count, numSides, position, content) => ({
type: CoreTokenTypes.DiceRoll,

@@ -40,3 +44,4 @@ position,

});
exports.constantToken = (value, position, content) => ({
exports.diceRollToken = diceRollToken;
const constantToken = (value, position, content) => ({
type: CoreTokenTypes.DiceRoll,

@@ -48,1 +53,2 @@ position,

});
exports.constantToken = constantToken;
"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 (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
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"));
function withPlugins(...plugins) {
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 customPlugins = {};
plugins.forEach(plugin => {
plugins.forEach((plugin) => {
customPlugins[plugin.typeConstant] = plugin;

@@ -15,0 +40,0 @@ });

@@ -8,2 +8,3 @@ import createRollDice from './rollDice';

import constant from './rules/constant';
import defaultRandom from "./util/random";
export const defaultPlugins = {

@@ -13,5 +14,5 @@ [simpleDieRoll.typeConstant]: simpleDieRoll,

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

@@ -18,0 +19,0 @@ return {

import { CoreTokenTypes } from './tokens';
function createRollDice(plugins) {
function createRollDice(plugins, random) {
function rollDice(tokens) {

@@ -11,3 +11,3 @@ return tokens.map(token => {

case CoreTokenTypes.DiceRoll:
return plugins[token.detailType].roll(token.detail);
return plugins[token.detailType].roll(token.detail, random);
}

@@ -14,0 +14,0 @@ });

@@ -1,2 +0,1 @@

import random from '../util/random';
export const SIMPLE_DIE_ROLL = '_SimpleDieRoll';

@@ -10,5 +9,5 @@ const simpleDieRoll = {

},
roll: ({ count, numSides }) => 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;
import createDiceRoller, { defaultPlugins } from './createDiceRoller';
function withPlugins(...plugins) {
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;
}
const customPlugins = {};
plugins.forEach(plugin => {
plugins.forEach((plugin) => {
customPlugins[plugin.typeConstant] = plugin;

@@ -6,0 +16,0 @@ });

import { Plugins, RollResults } from './rules/types';
import { Token } from './tokens';
declare function createRollDice(plugins: Plugins): (tokens: Token[]) => RollResults;
import { Random } from './util/random';
declare function createRollDice(plugins: Plugins, random: Random): (tokens: Token[]) => RollResults;
export default createRollDice;

@@ -0,1 +1,2 @@

import { Random } from "../util/random";
export interface Plugins {

@@ -8,3 +9,3 @@ [key: string]: DiceRule<any>;

tokenize: (raw: string) => T;
roll: (token: T) => Rolls;
roll: (token: T, random: Random) => Rolls;
calculateValue: (token: T, rolls: number[]) => number;

@@ -11,0 +12,0 @@ }

import { Plugins, RollResults } from './rules/types';
import { Token } from './tokens';
export declare type RollTotal = number | null;
declare function createTallyRolls(plugins: Plugins): (tokens: Token[], rolls: RollResults) => (number | null)[];
declare function createTallyRolls(plugins: Plugins): (tokens: Token[], rolls: RollResults) => RollTotal[];
export default createTallyRolls;

@@ -0,2 +1,3 @@

export declare type Random = (min: number, max: number) => number;
declare function random(min: number, max: number): number;
export default random;
import { DiceRule } from './rules/types';
declare function withPlugins(...plugins: DiceRule<any>[]): {
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) => (number | null)[];
roll: (notation: string) => import("./roll").RollInformation;
};
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>;
export default withPlugins;
{
"name": "@airjp73/dice-notation",
"version": "1.2.0",
"version": "1.3.0",
"description": "A js library for parsing dice notation",

@@ -36,3 +36,3 @@ "module": "dist/module/index.js",

"ts-jest": "^24.3.0",
"typescript": "^3.7.5"
"typescript": "^4.2.4"
},

@@ -39,0 +39,0 @@ "dependencies": {

@@ -46,4 +46,2 @@ # @airjp73/dice-notation

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

@@ -63,3 +61,3 @@ // Regex to pass to the parser

// this is so we can see what every individual dice roll was if we want
roll: ({ numDice }) => {
roll: ({ numDice }, random) => {
const rolls = [];

@@ -66,0 +64,0 @@ for (let i = 0; i < numDice; i++) {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc