Module:Utils
Jump to navigation
Jump to search
Documentation for this module may be created at Module:Utils/doc
-- <nowiki> local getArgs = require('Dev:Arguments').getArgs local tabletools = require("Dev:TableTools") local language = mw.language.getContentLanguage() local p = {} --% This function creates a wrapper function for use with #invoke. --% When a function is meant to be usable both from other Lua modules and #invoke, performance can be improved, and the [[wikipedia:Module:Arguments|Arguments module]] avoided, by having a function (with name prefixed by an underscore) for use from modules and the debug console, and a function for use from templates or pages. --@ func (function) a function that takes a table of named arguments --: (function) a function that expects a frame and will call <var>func</var> with the arguments passed by the frame or its parent frame function p.make_wrapper_function(func, postprocess) if postprocess then return function(frame) return frame:preprocess(func(getArgs(frame))) end else return function(frame) return func(getArgs(frame)) end end end function p.trim_positional_arguments(args) local positional_arguments = {} local argn = 1 local positional_argument = args[argn] while positional_argument do table.insert(positional_arguments, mw.text.trim(positional_argument)) argn = argn + 1 positional_argument = args[argn] end return positional_arguments end function p.parse_number(num, error_if_invalid) if type(num) == 'number' then return num elseif type(num) == 'string' then local number = tonumber(language:parseFormattedNumber(num)) if number then return number elseif error_if_invalid then return error(num .. " is not a valid number") end elseif error_if_invalid then return error(tostring(num) .. " is not a valid number") end end function p.format_number(number) return language:formatNum(number) end --% This function returns the first index found for a value in an array. --@ array (table) the array to search in --@ value (any) the value to look for --: (number) the first index found with the value, or nil if the value was not found function p.index_of(array, value) for i = 1, #array do if array[i] == value then return i end end end --% This function returns an array of all the indices found for a value in an array. --@ array (table) the array to search in --@ value (any) the value to look for --: (table) an array with the indices found, may be empty function p.indices_of(array, value) local indices = {} for i = 1, #array do if array[i] == value then table.insert(indices, i) end end return indices end --% This function returns the first key found for a value in a table --@ t (table) the table to search in --@ value (any) the value to look for --: the key found, nil if no key was found with the value function p.key_of(t, value) for k, v in t do if v == value then return k end end end --% This function returns an array of all the keys found for a value in a table. --@ t (table) the table to search in --@ value (any) the value to look for --: (table) an array with the keys found, may be empty function p.keys_of(t, value) local keys = {} for k, v in t do if v == value then table.insert(keys, k) end end return keys end --% This function returns whether an item is in an array. --% If the table is not an array, use p.in_table. --@ array (table) the array to search in --@ item (any) the value to look for --: (boolean) whether the item was found in the array function p.in_array(array, item) for i = 1, #array do if array[i] == item then return true end end return false end --% This function returns a set from an array. --% A set is a table with the values from the array in keys and boolean true in values. --@ array (table) the array to convert --: (table) a set function p.array_to_set(array) local set = {} for i = 1, #array do set[array[i]] = true end return set end --% This function returns a table that behaves like a set for an array. --@ array (table) the array to create a set-like table for --: (table) a table with a metatable that implements the set-like behavior function p.array_to_set_metatable(array) local set = {} setmetatable(set, { __index = function(self, index) return p.in_array(array, index) end }) return set end --% This function returns an array from a set. --@ set (table) the set --: (table) an array with all the values from the set function p.set_to_array(set) local array = {} for value in pairs(set) do table.insert(array, value) end return array end --% This function returns whether a value is in a table. --% If the table is an array, p.in_array would be faster. --@ t (table) the table to search in --@ value (any) the value to search for --: (boolean) whether the value was found in the table p.in_table = tabletools.includes --% This function returns an array with duplicates removed. --@ array (table) the original array --: (table) an array with the same items but duplicates removed p.remove_duplicates = tabletools.removeDuplicates function p.determine_old_page(frame) if os.time() - frame.args[1] > 15811200 then return true end return false end return p