string_utils

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 spec string in this format: "Bag($N tag1 tag2)" into the parameters to generate a bag through the newCreateBag method. The components include a total value for the bag, and a tag expresion.

Source:
Parameters:
Name Type Description
spec String
Returns:

the parameters, incuding “value” and “tags”

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) 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) 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) 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 the format() 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