Editing
Module:Utils
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
-- <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
Summary:
Please note that all contributions to nUSA Wiki Archive may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
NUSA Wiki Archive:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Template used on this page:
Module:Utils/doc
(
edit
)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Module
Discussion
English
Views
Read
Edit source
View history
More
Search
Navigation
Main page
Recent changes
Random page
Tools
What links here
Related changes
Special pages
Page information