Module:Includes
Lua equivalent to the javascript Array.prototype.includes() function, except fromIndex is 1-indexed instead of zero-indexed. Determines whether an array includes a certain value and returns true or false.
Syntax
includes(array, searchElement)
includes(array, searchElement, fromIndex)
array
array is the array to search. If type(array) ~= 'table' the module will return false.
value
value is the value to be tested. If value is present in the array, the module will return true. If value is missing the module will return false.
fromIndex
fromIndex is the optional 1-based index at which to start searching. If fromIndex is not present, all values in the array will be searched and the array will be treated as a table/associative array (it will be iterated over using pairs()).
If fromIndex is present and an integer, the array is assumed to be a conventional array/sequence/list (indexed with consecutive integer keys starting at 1, and interated over using ipairs()). Only the values whose index is fromIndex or higher will be searched.
In the following examples, #array represents the length of the integer-keyed portion of the array.
- If
fromIndex < 0it will count back from the end of the array, e.g. a value of-1will only search the last integer-keyed element in the array. IffromIndex <= (-1 * #array), the entire integer-keyed portion of the array will be searched. - If
fromIndex = 0it will be treated as a1and the entire integer-keyed portion of the array will be searched. - If
fromIndex > #array, the array is not searched andfalseis returned.
Usage
local includes = require('Module:Includes')
-- These will return true
includes({"a", "b", "c", "d"}, "b")
includes({"a", "b", "c", "d"}, "b", 0)
includes({"a", "b", "c", "d"}, "b", 1)
includes({"a", "b", "c", "d"}, "b", 2)
includes({"a", "b", "c", "d"}, "b", -3)
includes({"a", "b", "c", "d"}, "b", -5)
includes({[1] = "a",[100] = "b",[101] = "c"}, "b")
includes({[1] = "a",[2] = "b",[3] = "c"}, "b", 0)
includes({first = "a", second = "b", third = "c"}, "b")
--these will return false
includes("b","b") -- array is not a table
includes({"a", "b", "c", "d"}) -- value missing
includes({"a", "b", "c", "d"}, "e") -- "e" is not in array
includes({"a", "b", "c", "d"}, "b", 3) -- "b" is before position 3
includes({"a", "b", "c", "d"}, "b", 5) -- 5 is larger than #array
includes({"a", "b", "c", "d"}, "b", -2) -- "b" is not in the last two positions
includes({[1] = "a", [100] = "b", [101] = "c"}, "b", 0) -- key 100 is non-consecutive
includes({first = "a", second = "b", third = "c"}, "b", 0) -- key "second" is not an integer
-- Equivalent to JavaScript array.includes(searchElement) or
-- array.includes(searchElement, fromIndex), except fromIndex is 1 indexed
return function (array, searchElement, fromIndex)
if type(array) ~= 'table' then return false end
fromIndex = tonumber(fromIndex)
if fromIndex then
if (fromIndex < 0) then
fromIndex = #array + fromIndex + 1
end
if fromIndex < 1 then fromIndex = 1 end
for _, v in ipairs({unpack(array, fromIndex)}) do
if v == searchElement then
return true
end
end
else
for _, v in pairs(array) do
if v == searchElement then
return true
end
end
end
return false
end
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.