new Format(formatString, definitions, settingsopt)
The format according to which text will be generated.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
formatString |
string | A string which may contain 'nonterminals' - text which will be replaced with a specified or randomly chosen substring. By default, any sections of the text enclosed in parentheses will be replaced; this can be changed in the setings argument. |
|
definitions |
object | An object assosciating nonterminals as keys with the values they will be replaced with. If the value is a WeightedRandom, its .choose method will be called and the result inserted instead; if it is a Format its .expand method will be called and the result inserted. |
|
settings |
formatSettings |
<optional> |
An object containing the settings used by this Format.
Default value: |
Methods
expand(definitionsArgopt, settingsopt)
Generate text according to the format. Uses the definitions passed to the constructor if there were any, or those passed as an argument if none were passed to the constructor.
Each section of the formatString that is enclosed in parentheses is replaced according to the following rules:
- If it's not a key in the definitions passed to the constructor or this method, an error is thrown.
- If its definition is a string, it's replaced with that string.
- If its definition is also a Format, it's replaced with the returned value of a call to its .expand method on the definitions object currently in use (note if it has its own definitions they will take precedence).
- If it's a WeightedRandom, it's replaced with the returned value of a call to its .choose method.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
definitionsArg |
object |
<optional> |
Definitions to be used if no definitions were passed to the constructor |
settings |
formatSettings |
<optional> |
An object containing the settings used by this Format. Settings passed to this method will take precedence over those set in the constructor. |