Skip to content

Instantly share code, notes, and snippets.

@rafaelrinaldi
Created June 25, 2009 14:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafaelrinaldi/135896 to your computer and use it in GitHub Desktop.
Save rafaelrinaldi/135896 to your computer and use it in GitHub Desktop.
Search something inside an array, using filters similars to "LIKE" filters from Oracle.
package rinaldi.array
{
import rinaldi.string.isInString;
/**
*
* Search something inside an array, using filters similars to "LIKE" filters from Oracle.
* The search is case sensitive!
*
* @param p_arr Data provider.
* @param filters Search filters.
*
* @return Filtered search.
*
* @example
* <pre>
* import rinaldi.array.filterArray;
*
* var arr : Array = ["rafael", "gabriel", "arthur", "cassio", "lucas", "29/05/2009", "10/10/2010"];
*
* // Return items that begin with "r"
* trace(filterArray(arr, "r%")); // rafael
*
* // Return items that begin with "ga"
* trace(filterArray(arr, "ga%")); // gabriel
*
* // Return items that contain "r"
* trace(filterArray(arr, "%r%")); // rafael,gabriel,arthur
*
* // Return items that contain "ss"
* trace(filterArray(arr, "%ss%")); // cassio
*
* // Return items that ends with "l"
* trace(filterArray(arr, "%l")); // rafael,gabriel
*
* // Return items that ends with "el"
* trace(filterArray(arr, "%el")); // rafael,gabriel
*
* // Return items with second character equals to "a"
* trace(filterArray(arr, "_a")); // rafael,gabriel,cassio
*
* // Return items with third character equals to "c"
* trace(filterArray(arr, "__c")); // lucas
*
* // Return items with four last characters equals to "2009"
* trace(filterArray(arr, "______2009")); // 29/05/2009
*
* // Multiple filters
* trace(filterArray(arr, "%/%", "_a", "l%")); // rafael,gabriel,cassio,lucas,29/05/2009,10/10/2010
* </pre>
*
* @see rinaldi.array
*
* @author Rafael Rinaldi.
*
* */
public function filterArray( p_arr : Array, ...filters ) : Array
{
var keyword : String, search : String, match : String, filterSymbol : String;
var start : int, end : int;
var results : Array;
results = [];
for each(var item : Object in p_arr) {
keyword = String(item);
for each(var filter : String in filters) {
filterSymbol = isInString(filter, "%") ? "%" : "_";
start = filter.indexOf(filterSymbol);
end = filter.lastIndexOf(filterSymbol);
search = filter.split(filterSymbol).join("");
if(filterSymbol == "_") {
const underscores : int = filter.split("_").length - 1;
if(underscores == 1 && search.length == 1) {
/** It's only one underscore **/
match = keyword.charAt(underscores);
if(match == search && !isInArray(results, keyword))
results[results.length] = keyword;
} else {
/** Multiple underscores **/
match = keyword.substring(underscores, underscores + search.length);
if(match == search && !isInArray(results, keyword))
results[results.length] = keyword;
}
continue;
}
if(start == end) {
/** It's only one percent **/
if(start == 0) {
/** Starts with percent **/
match = keyword.substring((keyword.length - 1) - (search.length - 1));
if(match == search && !isInArray(results, keyword))
results[results.length] = keyword;
} else {
/** Finish with percent **/
match = keyword.substring(0, end);
if(match == search && !isInArray(results, keyword))
results[results.length] = keyword;
}
} else {
/** Contain double percents **/
if(isInString(keyword, search))
if(!isInArray(results, keyword))
results[results.length] = keyword;
}
}
}
return results;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment