Class IString
Create a new ilib string instance. This string inherits from and extends the Javascript String class. It can be used almost anywhere that a normal Javascript string is used, though in some instances you will need to call the #toString method when a built-in Javascript string is needed. The formatting methods are methods that are not in the intrinsic String class and are most useful when localizing strings in an app or web site in combination with the ResBundle class.
This class is named IString ("ilib string") so as not to conflict with the
built-in Javascript String class.
Defined in: ilib-full-dyn.js.
Constructor Attributes | Constructor Name and Description |
---|---|
IString(string)
|
Method Attributes | Method Name and Description |
---|---|
charAt(index)
Same as String.charAt()
|
|
charCodeAt(index)
Same as String.charCodeAt().
|
|
Return an iterator that will step through all of the characters
in the string one at a time, taking
care to step through the surrogate pairs in UTF-16 encoding
properly.
|
|
codePointAt(index)
Return the code point at the given index when the string is viewed
as an array of code points.
|
|
Return the number of code points in this string.
|
|
concat(strings)
Same as String.concat()
|
|
forEach(callback)
Call the callback with each character in the string one at
a time, taking care to step through the surrogate pairs in
the UTF-16 encoding properly.
|
|
forEachCodePoint(callback)
Call the callback with each numeric code point in the string one at
a time, taking care to step through the surrogate pairs in
the UTF-16 encoding properly.
|
|
format(params)
Format this string instance as a message, replacing the parameters with
the given values.
|
|
formatChoice(argIndex, params)
Format a string as one of a choice of strings dependent on the value of
a particular argument index.
|
|
<static> |
IString.fromCodePoint(codepoint)
Convert a UCS-4 code point to a Javascript string.
|
Return the locale to use when processing choice formats.
|
|
indexOf(searchValue, start)
Same as String.indexOf()
|
|
iterator()
Return an iterator that will step through all of the characters
in the string one at a time and return their code points, taking
care to step through the surrogate pairs in UTF-16 encoding
properly.
|
|
lastIndexOf(searchValue, start)
Same as String.lastIndexOf()
|
|
<static> |
IString.loadPlurals(sync, locale, loadParams, onLoad)
Load the plural the definitions of plurals for the locale.
|
match(regexp)
Same as String.match()
|
|
replace(searchValue, newValue)
Same as String.replace()
|
|
search(regexp)
Same as String.search()
|
|
setLocale(locale, sync, loadParams, onLoad)
Set the locale to use when processing choice formats.
|
|
slice(start, end)
Same as String.slice()
|
|
split(separator, limit)
Same as String.split()
|
|
substr(start, length)
Same as String.substr()
|
|
substring(from, to)
Same as String.substring()
|
|
<static> |
IString.toCodePoint(str, index)
Convert the character or the surrogate pair at the given
index into the intrinsic Javascript string to a Unicode
UCS-4 code point.
|
Same as String.toLowerCase().
|
|
toString()
Same as String.toString()
|
|
Same as String.toUpperCase().
|
|
valueOf()
Same as String.valueOf()
|
- Parameters:
- {string|IString=} string
- initialize this instance with this string
- Parameters:
- {number} index
- the index of the character being sought
- Returns:
- {IString} the character at the given index
- Parameters:
- {number} index
- the index of the character being sought
- Returns:
- {number} the character code of the character at the given index in the string
The standard Javascript String's charAt() method only returns information about a particular 16-bit character in the UTF-16 encoding scheme. If the index is pointing to a low- or high-surrogate character, it will return that surrogate character rather than the surrogate pair which represents a character in the supplementary planes.
The iterator instance returned has two methods, hasNext() which returns true if the iterator has more characters to iterate through, and next() which returns the next character.
- Returns:
- {Object} an iterator that iterates through all the characters in the string
- Parameters:
- {number} index
- index of the code point
- Returns:
- {number} code point of the character at the given index into the string
- Returns:
- {number} the number of code points in this string
- Parameters:
- {string} strings
- strings to concatenate to the current one
- Returns:
- {IString} a concatenation of the given strings
The standard Javascript String's charAt() method only returns a particular 16-bit character in the UTF-16 encoding scheme. If the index to charAt() is pointing to a low- or high-surrogate character, it will return the surrogate character rather than the the character in the supplementary planes that the two surrogates together encode. This function will call the callback with the full character, making sure to join two surrogates into one character in the supplementary planes where necessary.
- Parameters:
- {function(string)} callback
- a callback function to call with each full character in the current string
The standard Javascript String's charCodeAt() method only returns information about a particular 16-bit character in the UTF-16 encoding scheme. If the index to charCodeAt() is pointing to a low- or high-surrogate character, it will return the code point of the surrogate character rather than the code point of the character in the supplementary planes that the two surrogates together encode. This function will call the callback with the full code point of each character, making sure to join two surrogates into one code point in the supplementary planes.
- Parameters:
- {function(string)} callback
- a callback function to call with each code point in the current string
The string can contain any text that a regular Javascript string can contain. Replacement parameters have the syntax:
{name}Where "name" can be any string surrounded by curly brackets. The value of "name" is taken from the parameters argument.
Example:
var str = new IString("There are {num} objects."); console.log(str.format({ num: 12 });Would give the output:
There are 12 objects.If a property is missing from the parameter block, the replacement parameter substring is left untouched in the string, and a different set of parameters may be applied a second time. This way, different parts of the code may format different parts of the message that they happen to know about.
Example:
var str = new IString("There are {num} objects in the {container}."); console.log(str.format({ num: 12 });Would give the output:
There are 12 objects in the {container}.The result can then be formatted again with a different parameter block that specifies a value for the container property.
- Parameters:
- params
- a Javascript object containing values for the replacement parameters in the current string
- Returns:
- a new IString instance with as many replacement parameters filled out as possible with real values.
The syntax of the choice string is as follows. The string contains a series of choices separated by a vertical bar character "|". Each choice has a value or range of values to match followed by a hash character "#" followed by the string to use if the variable matches the criteria.
Example string:
var num = 2; var str = new IString("0#There are no objects.|1#There is one object.|2#There are {number} objects."); console.log(str.formatChoice(num, { number: num }));Gives the output:
"There are 2 objects."The strings to format may contain replacement variables that will be formatted using the format() method above and the params argument as a source of values to use while formatting those variables.
If the criterion for a particular choice is empty, that choice will be used as the default one for use when none of the other choice's criteria match.
Example string:
var num = 22; var str = new IString("0#There are no objects.|1#There is one object.|#There are {number} objects."); console.log(str.formatChoice(num, { number: num }));Gives the output:
"There are 22 objects."If multiple choice patterns can match a given argument index, the first one encountered in the string will be used. If no choice patterns match the argument index, then the default choice will be used. If there is no default choice defined, then this method will return an empty string.
Special Syntax
For any choice format string, all of the patterns in the string should be of a single type: numeric, boolean, or string/regexp. The type of the patterns is determined by the type of the argument index parameter.
If the argument index is numeric, then some special syntax can be used in the patterns to match numeric ranges.
- >x - match any number that is greater than x
- >=x - match any number that is greater than or equal to x
- <x - match any number that is less than x
- <=x - match any number that is less than or equal to x
- start-end - match any number in the range [start,end)
- zero - match any number in the class "zero". (See below for a description of number classes.)
- one - match any number in the class "one"
- two - match any number in the class "two"
- few - match any number in the class "few"
- many - match any number in the class "many"
The definition of what numbers are included in a class is locale-dependent. They are defined in the data file plurals.json. If your string is in a different locale than the default for ilib, you should call the setLocale() method of the string instance before calling this method.
Other Pattern Types
If the argument index is a boolean, the string values "true" and "false" may appear as the choice patterns.
If the argument index is of type string, then the choice patterns may contain regular expressions, or static strings as degenerate regexps.
- Parameters:
- {*} argIndex
- The index into the choice array of the current parameter
- {Object} params
- The hash of parameter values that replace the replacement variables in the string
- Throws:
- "syntax error in choice format pattern: " if there is a syntax error
- Returns:
- {string} the formatted string
Similarly, that means if you use String.charCodeAt() you will only retrieve a 16-bit value, which may possibly be a single surrogate character that is part of a surrogate pair representing a character in the supplementary plane. It will not give you a code point. Use IString.codePointAt() to access code points in a string, or use an iterator to walk through the code points in a string.
- Parameters:
- {number} codepoint
- UCS-4 code point to convert to a character
- Returns:
- {string} a string containing the character represented by the codepoint
- Returns:
- {string} localespec to use when processing choice formats with this string
- Parameters:
- {string} searchValue
- string to search for
- {number} start
- index into the string to start searching, or undefined to search the entire string
- Returns:
- {number} index into the string of the string being sought, or -1 if the string is not found
The standard Javascript String's charCodeAt() method only returns information about a particular 16-bit character in the UTF-16 encoding scheme. If the index is pointing to a low- or high-surrogate character, it will return a code point of the surrogate character rather than the code point of the character in the supplementary planes that the two surrogates together encode.
The iterator instance returned has two methods, hasNext() which returns true if the iterator has more code points to iterate through, and next() which returns the next code point as a number.
- Returns:
- {Object} an iterator that iterates through all the code points in the string
- Parameters:
- {string} searchValue
- string to search for
- {number} start
- index into the string to start searching, or undefined to search the entire string
- Returns:
- {number} index into the string of the string being sought, or -1 if the string is not found
- Parameters:
- {boolean=} sync
- {Locale|string=} locale
- {Object=} loadParams
- {function(*)=} onLoad
- Parameters:
- {string} regexp
- the regular expression to match
- Returns:
- {Array.<string>} an array of matches
- Parameters:
- {string} searchValue
- a regular expression to search for
- {string} newValue
- the string to replace the matches with
- Returns:
- {IString} a new string with all the matches replaced with the new value
- Parameters:
- {string} regexp
- the regular expression to search for
- Returns:
- {number} position of the match, or -1 for no match
- Parameters:
- {Locale|string} locale
- locale to use when processing choice formats with this string
- {boolean=} sync
- [optional] whether to load the locale data synchronously or not
- {Object=} loadParams
- [optional] parameters to pass to the loader function
- {function(*)=} onLoad
- [optional] function to call when the loading is done
- Parameters:
- {number} start
- first character to include in the string
- {number} end
- include all characters up to, but not including the end character
- Returns:
- {IString} a slice of the current string
- Parameters:
- {string} separator
- regular expression to match to find separations between the parts of the text
- {number} limit
- maximum number of items in the final output array. Any items beyond that limit will be ignored.
- Returns:
- {Array.<string>} the parts of the current string split by the separator
- Parameters:
- {number} start
- the index of the character that should begin the returned substring
- {number} length
- the number of characters to return after the start character.
- Returns:
- {IString} the requested substring
- Parameters:
- {number} from
- the index of the character that should begin the returned substring
- {number} to
- the index where to stop the extraction. If omitted, extracts the rest of the string
- Returns:
- {IString} the requested substring
- Parameters:
- {string} str
- string to get the code point from
- {number} index
- index into the string
- Returns:
- {number} code point of the character at the given index into the string
- Returns:
- {IString} a string with the first character lower-cased
- Returns:
- {string} this instance as regular Javascript string
- Returns:
- {IString} a string with the first character upper-cased
- Returns:
- {string} this instance as a regular Javascript string