Wrote this function for a Stack Overflow answer Stack Overflow answer. It started off pretty bad but in the end I polished it a bit and now I have something I think to be pretty clean. Actually, when I look at the code, this is what I usually write when I'm programming AS3 (minus the documentation, I'm a lazy bastard, although that's slowly changing too).
The problem the code attempts to solve is finding a case-insensitive string in an array.
Soo... anything that can be improved here?
Should I handle null
and undefined
?
Does my documentation look helpful from a IDE usage perspective?
/**
* Searches through an array for a case-insensitive string match.
* Attempts to imitate Array.indexOf where it can.
* @param arr The array to search through
* @param searchingFor The string to search for
* @param fromIndex Optional, an index to start searching at.
* @returns The index of the array that a match was found at (zero-indexed), or -1 if no match was found.
*/
public static function indexOfCaseInsensitiveString(arr:Array, searchingFor:String, fromIndex:uint = 0):int {
var lowercaseSearchString:String = searchingFor.toLowerCase();
var arrayLength:uint = arr.length;//Retrieving array length is expensive, optimization
for(var index:uint = fromIndex; index < arrayLength; index++) {
var element:* = arr[index];
if(element is String && element.toLowerCase() == lowercaseSearchString) {
return index;
}
}
return -1;//It wasn't found in the array.
}
Wrote this function for a Stack Overflow answer. It started off pretty bad but in the end I polished it a bit and now I have something I think to be pretty clean. Actually, when I look at the code, this is what I usually write when I'm programming AS3 (minus the documentation, I'm a lazy bastard, although that's slowly changing too).
The problem the code attempts to solve is finding a case-insensitive string in an array.
Soo... anything that can be improved here?
Should I handle null
and undefined
?
Does my documentation look helpful from a IDE usage perspective?
/**
* Searches through an array for a case-insensitive string match.
* Attempts to imitate Array.indexOf where it can.
* @param arr The array to search through
* @param searchingFor The string to search for
* @param fromIndex Optional, an index to start searching at.
* @returns The index of the array that a match was found at (zero-indexed), or -1 if no match was found.
*/
public static function indexOfCaseInsensitiveString(arr:Array, searchingFor:String, fromIndex:uint = 0):int {
var lowercaseSearchString:String = searchingFor.toLowerCase();
var arrayLength:uint = arr.length;//Retrieving array length is expensive, optimization
for(var index:uint = fromIndex; index < arrayLength; index++) {
var element:* = arr[index];
if(element is String && element.toLowerCase() == lowercaseSearchString) {
return index;
}
}
return -1;//It wasn't found in the array.
}
Wrote this function for a Stack Overflow answer. It started off pretty bad but in the end I polished it a bit and now I have something I think to be pretty clean. Actually, when I look at the code, this is what I usually write when I'm programming AS3 (minus the documentation, I'm a lazy bastard, although that's slowly changing too).
The problem the code attempts to solve is finding a case-insensitive string in an array.
Soo... anything that can be improved here?
Should I handle null
and undefined
?
Does my documentation look helpful from a IDE usage perspective?
/**
* Searches through an array for a case-insensitive string match.
* Attempts to imitate Array.indexOf where it can.
* @param arr The array to search through
* @param searchingFor The string to search for
* @param fromIndex Optional, an index to start searching at.
* @returns The index of the array that a match was found at (zero-indexed), or -1 if no match was found.
*/
public static function indexOfCaseInsensitiveString(arr:Array, searchingFor:String, fromIndex:uint = 0):int {
var lowercaseSearchString:String = searchingFor.toLowerCase();
var arrayLength:uint = arr.length;//Retrieving array length is expensive, optimization
for(var index:uint = fromIndex; index < arrayLength; index++) {
var element:* = arr[index];
if(element is String && element.toLowerCase() == lowercaseSearchString) {
return index;
}
}
return -1;//It wasn't found in the array.
}
Wrote this function for a Stack Overflow answer. It started off pretty bad but in the end I polished it a bit and now I have something I think to be pretty clean. Actually, when I look at the code, this is what I usually write when I'm programming AS3 (minus the documentation, I'm a lazy bastard, although that's slowly changing too).
The problem the code attempts to solve is finding a case-insensitive string in an array.
Soo... anything that can be improved here?
Should I handle null
and undefined
?
Does my documentation look helpful from a IDE usage perspective?
/**
* Searches through an array for a case-insensitive string match.
* Attempts to imitate Array.indexOf where it can.
* @param arr The array to search through
* @param searchingFor The string to search for
* @param fromIndex Optional, an index to start searching at.
* @returns The index of the array that a match was found at (zero-indexed), or -1 if no match was found.
*/
public static function indexOfCaseInsensitiveString(arr:Array, searchingFor:String, fromIndex:uint = 0):int {
var lowercaseSearchString:String = searchingFor.toLowerCase();
var arrayLength:uint = arr.length;//Retrieving array length is expensive, optimization
for(var index:uint = fromIndex; index < arrayLength; index++) {
var element:* = arr[index];
if(element is String && element.toLowerCase() == lowercaseSearchString) {
return index;
}
}
return -1;//It wasn't found in the array.
}
Wrote this function for a Stack Overflow answer. It started off pretty bad but in the end I polished it a bit and now I have something I think to be pretty clean. Actually, when I look at the code, this is what I usually write when I'm programming AS3 (minus the documentation, I'm a lazy bastard, although that's slowly changing too).
Soo... anything that can be improved here?
Should I handle null
and undefined
?
Does my documentation look helpful from a IDE usage perspective?
/**
* Searches through an array for a case-insensitive string match.
* Attempts to imitate Array.indexOf where it can.
* @param arr The array to search through
* @param searchingFor The string to search for
* @param fromIndex Optional, an index to start searching at.
* @returns The index of the array that a match was found at (zero-indexed), or -1 if no match was found.
*/
public static function indexOfCaseInsensitiveString(arr:Array, searchingFor:String, fromIndex:uint = 0):int {
var lowercaseSearchString:String = searchingFor.toLowerCase();
var arrayLength:uint = arr.length;//Retrieving array length is expensive, optimization
for(var index:uint = fromIndex; index < arrayLength; index++) {
var element:* = arr[index];
if(element is String && element.toLowerCase() == lowercaseSearchString) {
return index;
}
}
return -1;//It wasn't found in the array.
}
Wrote this function for a Stack Overflow answer. It started off pretty bad but in the end I polished it a bit and now I have something I think to be pretty clean. Actually, when I look at the code, this is what I usually write when I'm programming AS3 (minus the documentation, I'm a lazy bastard, although that's slowly changing too).
The problem the code attempts to solve is finding a case-insensitive string in an array.
Soo... anything that can be improved here?
Should I handle null
and undefined
?
Does my documentation look helpful from a IDE usage perspective?
/**
* Searches through an array for a case-insensitive string match.
* Attempts to imitate Array.indexOf where it can.
* @param arr The array to search through
* @param searchingFor The string to search for
* @param fromIndex Optional, an index to start searching at.
* @returns The index of the array that a match was found at (zero-indexed), or -1 if no match was found.
*/
public static function indexOfCaseInsensitiveString(arr:Array, searchingFor:String, fromIndex:uint = 0):int {
var lowercaseSearchString:String = searchingFor.toLowerCase();
var arrayLength:uint = arr.length;//Retrieving array length is expensive, optimization
for(var index:uint = fromIndex; index < arrayLength; index++) {
var element:* = arr[index];
if(element is String && element.toLowerCase() == lowercaseSearchString) {
return index;
}
}
return -1;//It wasn't found in the array.
}