Module:Includes/doc: Difference between revisions
More actions
Created page with "{{documentation subpage}} <!-- Add categories where indicated at the bottom of this page and interwikis at Wikidata --> Lua equivalent to the javascript [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes Array.prototype.includes()] function, except fromIndex is 1-indexed instead of zero-indexed. Determines whether an array includes a certain value and returns <code>true</code> or <code>false</code>. == Syntax == <syntaxhig..." |
No edit summary |
||
| (One intermediate revision by the same user not shown) | |||
| Line 56: | Line 56: | ||
<includeonly>{{Sandbox other|| | <includeonly>{{Sandbox other|| | ||
<!-- Categories below this line; interwikis at Wikidata --> | <!-- Categories below this line; interwikis at Wikidata --> | ||
[[Category:Modules]] | |||
[[Category:Lua metamodules]] | [[Category:Lua metamodules]] | ||
}}</includeonly> | }}</includeonly> | ||
<noinclude> | |||
[[Category:Module documents]] | |||
</noinclude> | |||
Latest revision as of 03:50, 15 March 2025
| This is a documentation subpage for Module:Includes. It may contain usage information, categories and other content that is not part of the original module page. |
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