From 6c89acbe0de8d48521b19083c0e97849753b8ffc Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Tue, 20 Jun 2006 17:32:40 +0000 Subject: [PATCH] Scholar.inArray(needle, haystack) and Scholar.arraySearch(needle, haystack) -- versions of the PHP functions for JS --- .../content/scholar/xpcom/scholar.js | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/chrome/chromeFiles/content/scholar/xpcom/scholar.js b/chrome/chromeFiles/content/scholar/xpcom/scholar.js index 9ba05053d4..5453357214 100644 --- a/chrome/chromeFiles/content/scholar/xpcom/scholar.js +++ b/chrome/chromeFiles/content/scholar/xpcom/scholar.js @@ -23,6 +23,8 @@ var Scholar = new function(){ this.getString = getString; this.flattenArguments = flattenArguments; this.join = join; + this.inArray = inArray; + this.arraySearch = arraySearch; this.randomString = randomString; this.getRandomID = getRandomID; @@ -198,6 +200,34 @@ var Scholar = new function(){ } + /* + * PHP's in_array() for JS -- returns true if a value is contained in + * an array, false otherwise + */ + function inArray(needle, haystack){ + for (var i in haystack){ + if (haystack[i]==needle){ + return true; + } + } + return false; + } + + + /* + * PHP's array_search() for JS -- searches an array for a value and + * returns the key if found, false otherwise + */ + function arraySearch(needle, haystack){ + for (var i in haystack){ + if (haystack[i]==needle){ + return i; + } + } + return false; + } + + /** * Generate a random string of length 'len' (defaults to 8) **/