Added arrayutil.contains function.
This commit is contained in:
parent
ca974991a5
commit
73e4392c96
@ -147,6 +147,11 @@
|
||||
<h2>Functions</h2>
|
||||
<table class="function_list">
|
||||
|
||||
<tr>
|
||||
<td class="name" nowrap><a href="#arrayutil.contains">arrayutil.contains</a> (array, item, equals)</td>
|
||||
<td class="summary">Gets if the given array contains the given item.</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td class="name" nowrap><a href="#arrayutil.index">arrayutil.index</a> (array, item, equals)</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> (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> (array, item, equals)</dt>
|
||||
<dd>
|
||||
Gets the index of the item in the given array.
|
||||
|
@ -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))
|
||||
|
||||
|
@ -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.
|
||||
|
Loading…
x
Reference in New Issue
Block a user