Added arrayutil.contains function.

This commit is contained in:
Robert Zenz 2015-04-26 17:29:02 +02:00
parent ca974991a5
commit 73e4392c96
3 changed files with 69 additions and 0 deletions

View File

@ -147,6 +147,11 @@
<h2>Functions</h2> <h2>Functions</h2>
<table class="function_list"> <table class="function_list">
<tr>
<td class="name" nowrap><a href="#arrayutil.contains">arrayutil.contains</a>&nbsp;(array, item, equals)</td>
<td class="summary">Gets if the given array contains the given item.</td>
</tr>
<tr> <tr>
<td class="name" nowrap><a href="#arrayutil.index">arrayutil.index</a>&nbsp;(array, item, equals)</td> <td class="name" nowrap><a href="#arrayutil.index">arrayutil.index</a>&nbsp;(array, item, equals)</td>
<td class="summary">Gets the index of the item in the given array.</td> <td class="summary">Gets the index of the item in the given array.</td>
@ -205,6 +210,43 @@
<dt><a name="arrayutil.contains"></a><strong>arrayutil.contains</strong>&nbsp;(array, item, equals)</dt>
<dd>
Gets if the given array contains the given item.
<h3>Parameters</h3>
<ul>
<li>
array: The array to search in.
</li>
<li>
item: The item to search for, can either be an item or another array.
</li>
<li>
equals: Optional. The function to determine if items equal each other, defaults to tableutil.equals.
</li>
</ul>
<h3>Return value:</h3>
true if the array contains the given item.
</dd>
<dt><a name="arrayutil.index"></a><strong>arrayutil.index</strong>&nbsp;(array, item, equals)</dt> <dt><a name="arrayutil.index"></a><strong>arrayutil.index</strong>&nbsp;(array, item, equals)</dt>
<dd> <dd>
Gets the index of the item in the given array. Gets the index of the item in the given array.

View File

@ -11,6 +11,22 @@ dofile("./utils/tableutil.lua")
test.start("arrayutil") 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.run("index", function()
test.equals(-1, arrayutil.index({}, nil)) test.equals(-1, arrayutil.index({}, nil))

View File

@ -33,6 +33,17 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
arrayutil = {} 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. --- Gets the index of the item in the given array.
-- --
-- @param array The array to search in. -- @param array The array to search in.