diff --git a/doc/files/utils/arrayutil.html b/doc/files/utils/arrayutil.html index 2044a39..b5e5739 100644 --- a/doc/files/utils/arrayutil.html +++ b/doc/files/utils/arrayutil.html @@ -147,6 +147,11 @@

Functions

+ + + + + @@ -205,6 +210,43 @@ +
arrayutil.contains (array, item, equals)
+
+Gets if the given array contains the given item. + + +

Parameters

+ + + + + + + +

Return value:

+true if the array contains the given item. + + + +
+ + + +
arrayutil.index (array, item, equals)
Gets the index of the item in the given array. diff --git a/test/arrayutil.lua b/test/arrayutil.lua index dc8e51e..1a14673 100644 --- a/test/arrayutil.lua +++ b/test/arrayutil.lua @@ -11,6 +11,22 @@ dofile("./utils/tableutil.lua") test.start("arrayutil") +test.run("contains", function() + test.equals(false, arrayutil.contains({}, nil)) + + test.equals(true, arrayutil.contains({ "test" }, "test")) + test.equals(true, arrayutil.contains({ "a", "b", "test" }, "test")) + test.equals(true, arrayutil.contains({ "a", "b" , "c", "test", "d", "e" }, "test")) + + test.equals(true, arrayutil.contains({ "a", "b" , "c", "test", "d", "e" }, { "a", "b", "c" })) + test.equals(true, arrayutil.contains({ "a", "b" , "c", "test", "d", "e" }, { "b", "c" })) + test.equals(true, arrayutil.contains({ "a", "b" , "c", "test", "d", "e" }, { "d", "e" })) + test.equals(true, arrayutil.contains({ "a", "b" , "c", "test", "d", "e" }, { "d", "e", "a", "b" })) + + test.equals(false, arrayutil.contains({ "a", "b" , "c", "test", "d", "e" }, { "b", "d" })) + test.equals(false, arrayutil.contains({ "a", "b" , "c", "test", "d", "e" }, { "d", "e", "a", "c" })) +end) + test.run("index", function() test.equals(-1, arrayutil.index({}, nil)) diff --git a/utils/arrayutil.lua b/utils/arrayutil.lua index f739ebf..eb69d2b 100644 --- a/utils/arrayutil.lua +++ b/utils/arrayutil.lua @@ -33,6 +33,17 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. arrayutil = {} +--- Gets if the given array contains the given item. +-- +-- @param array The array to search in. +-- @param item The item to search for, can either be an item or another array. +-- @param equals Optional. The function to determine if items equal each other, +-- defaults to tableutil.equals. +-- @return true if the array contains the given item. +function arrayutil.contains(array, item, equals) + return arrayutil.index(array, item, equals) >= 0 +end + --- Gets the index of the item in the given array. -- -- @param array The array to search in.
arrayutil.contains (array, item, equals)Gets if the given array contains the given item.
arrayutil.index (array, item, equals) Gets the index of the item in the given array.