Methods
(static) article(string) → {String}
- Description:
Put an indefinite article in front of the word based on whether or not it starts with a vowel.
- Source:
Example
article('walkie talkie')
=> "a walkie talkie"
article('album')
=> "an album"
Parameters:
| Name | Type | Description |
|---|---|---|
string |
String | String to prefix with an indefinite article |
Returns:
The string with "a" or "an" in front of it.
- Type
- String
(static) bagSpecParser(spec) → {Object}
- Description:
Parses a bag specification string to extract value, repetition, encumbrance, and tags. Supports both "Bag(...)" and "Stock(...)" formats with optional pricing, count, and encumbrance modifiers.
The preamble format supports:
$<value>- monetary value (can be a number like$10or a dice expression like$2d6)<value>%- percentage chance that items will be repeated in a bage<value>orE<value>- encumbrance weight limit
- Source:
Examples
bagSpecParser("Bag($10 a b)");
=> { value: 10, repetition: 20, enc: <max value>, tags: "a b", uncountable: false }
bagSpecParser("Bag($2d6/50% a b)");
=> { value: 8, repetition: 50, enc: <max value>, tags: "a b", uncountable: false }
bagSpecParser("Stock($10/50%/e10 items)");
=> { value: 10, repetition: 0, enc: 10, tags: "items", uncountable: true }
Parameters:
| Name | Type | Description |
|---|---|---|
spec |
string | The bag specification string to parse (e.g., "Bag($10 a b)" or "Stock($2d6/50%/e10 items)"). It is required to include either "Bag(...)" or "Stock(...)" format, with a value and tags. |
Returns:
An object containing:
- {number} value - The monetary value (defaults to 20)
- {number} repetition - The count/repetition number (defaults to 20). For stock, this is always 0.
- {number} enc - The encumbrance limit (defaults to Infinity)
- {string} tags - The tags representing item categories
- {boolean} uncountable - True if the spec is a Stock, false if Bag
- Type
- Object
(static) format(template, values+) → {String}
- Description:
Format a string with parameters. There are many ways to supply values to this method:
- Source:
Example
format('This {0} a {1}.', ['is', 'test']);
=> "This is a test."
format('This {0} a {1}.', 'is', 'test');
=> "This is a test."
format('This {verb} a {noun}.', {verb: 'is', noun: 'test'})
=> "This is a test."
Parameters:
| Name | Type | Description |
|---|---|---|
template |
String | template string |
values+ |
Object | An array, a set of values, or an object with key/value pairs that will be substituted into the template. |
Returns:
the formatted string
- Type
- String
(static) parseDelimiters(string, startCharopt, endCharopt)
- Description:
Parses a string for delimiters, returning a boolean indicating if they were found, the string without the delimiters, and the value within the delimiters.
- Source:
Example
parseDelimiters("Bag{$10 living}", "{", "}")
=> [true, "Bag", "$10 living"]
parseDelimiters("Bag($10 living)")
=> [true, "Bag", "$10 living"]
parseDelimiters("Bag")
=> [false, "Bag", null]
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
string |
String | the string to parse |
||
startChar |
String |
<optional> |
"("
|
the starting delimiter character |
endChar |
String |
<optional> |
")"
|
the ending delimiter character |
(static) pluralize(name, countopt) → {String}
- Description:
Pluralizes a string (usually a noun), if the count is greater than one. If it's a single item, an indefinite article will be added (see example below for cases where it should not be added, "uncountables"). The string should note the method of pluralizing the string in curly braces if it is not a simple noun that is pluralized using "s", "es" or "aries". For example:
- Source:
Example
pluralize('shoe', 3)
=> "3 shoes"
pluralize('status', 2)
=> "2 statuses"
pluralize('bag{s} of flour', 1)
=> "a bag of flour"
pluralize('bag{s} of flour', 2)
=> "2 bags of flour"
// Note suppression of the indefinite article!
pluralize('{|suits of }makeshift metal armor')
=> "makeshift metal armor"
pluralize('{|suits of }makeshift metal armor', 4)
=> "4 suits of makeshift metal armor"
let item = new Item('quarry');
pluralize(item, 3)
=> "3 quarries"
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
name |
String | Item | A string name following the rules described above, or an Item with a name property |
||
count |
Number |
<optional> |
1
|
The number of these items |
Returns:
the correct singular or plural name
- Type
- String
(static) sentenceCase(string) → {String}
- Description:
Convert a string to sentence case (only the first letter capitalized).
- Source:
Example
sentenceCase('antwerp benedict');
=> "Antwerp benedict"
sentenceCase('antwerp-Benedict');
=> "Antwerp-benedict"
sentenceCase('bead to a small mouth');
=> "Bead to a small mouth"
Parameters:
| Name | Type | Description |
|---|---|---|
string |
String |
Returns:
in sentence case
- Type
- String
(static) startLowercase(string) → {String}
- Description:
Converts the first character of a string to lowercase. The rest of the string is unchanged.
- Source:
Example
startLowercase('Atomic Frontier');
=> "atomic Frontier"
Parameters:
| Name | Type | Description |
|---|---|---|
string |
* |
Returns:
the string with a lowercase first character
- Type
- String
(static) titleCase(string) → {String}
- Description:
Convert string to title case. There's a long list of rules for this kind of capitalization, see:
To Title Case 2.1 - http://individed.com/code/to-title-case/
Copyright 2008-2013 David Gouch. Licensed under the MIT License.
- Source:
Example
titleCase('antwerp benedict');
=> "Antwerp Benedict"
titleCase('antwerp-Benedict');
=> "Antwerp-Benedict"
titleCase('bead to a small mouth');
=> "Bead to a Small Mouth"
Parameters:
| Name | Type | Description |
|---|---|---|
string |
String | string to title case |
Returns:
in title case
- Type
- String
(static) toList(array, funcopt, joinopt, separatoropt) → {String}
- Description:
Format the elements of an array into a list phrase.
- Source:
Example
toList(['Apples', 'Bananas', 'Oranges'], (value) => '*'+value);
=> "*Apples, *Bananas, and *Oranges"
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
array |
Array | The array to format |
||
func |
function |
<optional> |
identity
|
An optional function to format the elements of the array in the returned string. |
join |
String |
<optional> |
and
|
the word to join the last word in the list. |
separator |
String |
<optional> |
,
|
the delimiter to separate items in the list. |
Returns:
the array formatted as a list.
- Type
- String
(inner) hasDelimiters(string, startCharopt, endCharopt)
- Description:
Checks if a string has the specified delimiters. Defaults to the use of parentheses.
- Source:
Examples
hasDelimiters("Bag($10 living)")
=> true
hasDelimiters("Bag")
=> false
hasDelimiters("Bag($10 living)")
=> true
hasDelimiters("Bag")
=> false
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
string |
String | the string to check |
||
startChar |
String |
<optional> |
"("
|
the starting delimiter character |
endChar |
String |
<optional> |
")"
|
the ending delimiter character |
(inner) resolve(value) → {String}
- Description:
Combines randomization with formatting. First, randomizes the first argument using the
random()function. Then formats the resulting string using the rest of the arguments passed in to the method, as described by theformat()function.resolve(["Mr. {name}", "Mrs. {name}"], {name: "Smith"}); => "Mrs. Smith"
- Source:
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
value |
String | Array | function | A string with optional variants, or an array from which to select an element, or a function that returns a value. |
|
...args |
<optional> |
zero or more objects to use in formatting the final string |
Returns:
the randomly selected, formatted string
- Type
- String