@dice-roller/rpg-dice-roller / dice / DieRegistry
Class: DieRegistry
Defined in: src/dice/DieRegistry.ts:36
Die factories by name.
A pool's die is a DieNode ({ name, args }), and the evaluator creates the die with registry.create(name, ...args). The grammar uses the built-in names (d6 -> standard(6), d% -> percentile(), dF.1 -> fudge(1)), so registering a factory with a built-in name changes what that notation rolls, for every roll using this registry.
Pass a registry as the dice option to a Roll or Roller. Registering a new name does not extend the notation grammar: use an AST such as ast.pool(1, ast.die("fib")) for custom names. The AST stores the name and arguments, not the factory; provide the same registration when re-rolling an imported expression that uses a custom name.
Constructors
Constructor
new DieRegistry():
DieRegistry
Defined in: src/dice/DieRegistry.ts:40
A registry with the built-in factories: standard, percentile, fudge, range, face.
Returns
DieRegistry
Accessors
names
Get Signature
get names(): readonly
string[]
Defined in: src/dice/DieRegistry.ts:56
The registered names, in alphabetical order.
Returns
readonly string[]
Methods
create()
create(
name, ...args):DieLike
Defined in: src/dice/DieRegistry.ts:120
Creates a die with the named factory, e.g. registry.create("standard", 6).
Parameters
name
string
args
...number[]
Returns
Throws
no factory is registered with the name
Throws
any error raised by the factory is propagated
get()
get(
name):DieFactory|undefined
Defined in: src/dice/DieRegistry.ts:80
The factory registered with the name, e.g. registry.get("standard")?.(6).
Parameters
name
string
Returns
DieFactory | undefined
getOrThrow()
getOrThrow(
name):DieFactory
Defined in: src/dice/DieRegistry.ts:89
Returns the factory registered with the name.
Parameters
name
string
Returns
Throws
no factory is registered with the name
has()
has(
name):boolean
Defined in: src/dice/DieRegistry.ts:100
Whether a factory is registered with the name.
Parameters
name
string
Returns
boolean
register()
register(
name,factory):this
Defined in: src/dice/DieRegistry.ts:65
Registers a factory, replacing any already registered with the same name.
Parameters
name
string
factory
Returns
this
Throws
the name isn't a string or is empty, or the factory isn't a function
remove()
remove(
name):boolean
Defined in: src/dice/DieRegistry.ts:110
Removes the factory registered with the name. Notation that uses a removed built-in (e.g. d6 after removing standard) then throws when it's rolled.
Parameters
name
string
Returns
boolean
whether a factory was registered with the name
empty()
staticempty():DieRegistry
Defined in: src/dice/DieRegistry.ts:47
A registry with no factories, not even the built-ins.
Returns
DieRegistry