Locale-Sensitive JavaScript Method
str.endsWith(searchString);
str.endsWith(searchString, length);
Internationalization (I18n) Method Overview
This method determines if the string ends with the searchString parameter, returning true if found, else false if the string isn't found.
To shorten the string that will be searched, use the length parameter.
For example:
var str = "Hello Goodbye Hello";
var result1 = str.endsWith("Hello");
var result2 = str.endsWith("Hello", 5);
Method returns:
result1: true
result2: true
Click here (MDN) for additional details.
Note: This method is new, part of the ECMAScript 6 proposal, and so may not be supported by all browsers. Until approved, it is also subject to change.
I18n Issues
Whether or not calling endsWith is an i18n issue is dependent on how it is being used in the application. If the string that is to be
searched is to be displayed to the user, then this may be a problem because:
- The string being searched may need to be translated.
- The searchString parameter may need to be translated.
- In addition to the string possibly needing translation, the order of the words may be different and may "end with" a different string.
- The length parameter is the number of 16-bit values and does not take into account characters outside the Basic Multilingual Plane (BMP) which
require two 16-bit values.
Suggested Replacement
Use the endsWith method when the strings are programmatic strings that do not require translation.
If the string is to be displayed to the user, then you may need to first retrieve the string from a resource file based on locale, and then do the same thing
with searchString; retrieving it from the locale-sensitive resource file.
If you are using the length parameter, then you may need to use a 3rd party library (such as punycode.js) if the string that is being accessed
could have characters greater than 16-bits (i.e. outside the Basic Multilingual Plane of Unicode characters).
Globalyzer will detect this method and report it as an I18n issue. If you have determined that the call is being handled correctly, you can
use Globalyzer's Ignore Comment
functionality to ensure that it isn't picked up in a subsequent scan.
Locale-Sensitive JavaScript Methods
|