add contains() function to string library. added tests too.

git-svn-id: http://svn.openlayers.org/trunk/openlayers@859 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
euzuro
2006-07-03 19:54:00 +00:00
parent 74b3e153f8
commit 65ae34d4e7
2 changed files with 17 additions and 1 deletions

View File

@@ -561,6 +561,16 @@ String.prototype.startsWith = function(sStart){
return (this.substr(0,sStart.length) == sStart); return (this.substr(0,sStart.length) == sStart);
}; };
/**
* @param {String} str
*
* @returns Whether or not this string contains with the string passed in.
* @type Boolean
*/
String.prototype.contains = function(str){
return (this.indexOf(str) != -1);
};
/** /**
* @returns A trimmed version of the string - all leading and * @returns A trimmed version of the string - all leading and
* trailing spaces removed * trailing spaces removed

View File

@@ -11,16 +11,22 @@
} }
function test_02_Util_Strings(t) { function test_02_Util_Strings(t) {
t.plan(3); t.plan(5);
var str = " chicken pox "; var str = " chicken pox ";
t.ok(str.contains("chicken"), "contains() function correctly finds an embedded string");
t.ok(!str.contains("marsupial"), "contains() function correctly does not finds an random string");
var trimmedStr = str.trim(); var trimmedStr = str.trim();
t.eq(trimmedStr, "chicken pox", "String.trim works correctly"); t.eq(trimmedStr, "chicken pox", "String.trim works correctly");
t.eq(trimmedStr.startsWith("chicken"), true, "String.startsWith correctly finds chicken"); t.eq(trimmedStr.startsWith("chicken"), true, "String.startsWith correctly finds chicken");
t.eq(trimmedStr.startsWith("dolphin"), false, "String.startsWith correctly does not find turkey"); t.eq(trimmedStr.startsWith("dolphin"), false, "String.startsWith correctly does not find turkey");
} }
function test_03_Util_Array(t) { function test_03_Util_Array(t) {