ATTENTION! The process of updating WiKi to version Eco 10.x has begun. Those wishing to participate can find out more Information on our ECO Contribution Wiki Discord.

Editing Module:Utils

From Eco - English Wiki

Your changes will be displayed to readers once an authorized user accepts them. (help)

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.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
Line 237: Line 237:
--- Get all keys from <code>tbl</code> and sort them in alphabetical order.
--- Get all keys from <code>tbl</code> and sort them in alphabetical order.
-- @param #table tbl Table to get keys from
-- @param #table tbl Table to get keys from
-- @return #table Input table keys in alphabetical order.
-- @return #table Input table keys in alphabetical order, or <code>tbl</code> exactly if it has 0 or 1 items.
-- @author User:Demian
-- @author User:Demian
-- @see getSortedValues
-- @see getSortedValues
function p.getSortedKeys(tbl)
function p.getSortedKeys(tbl)
local sorted = {}
-- Check if there is anything to even sort.
if p.tableHasAtMostOneItem(tbl) then
return tbl
else
local sorted = {}


for key in pairs(tbl) do
for key, _ in pairs(tbl) do
table.insert(sorted, key)
table.insert(sorted, key)
end
end


table.sort(sorted)
table.sort(sorted)


return sorted
return sorted
end
end
end


--- Get all values from <code>tbl</code> and sort them in alphabetical order.
--- Get all values from <code>tbl</code> and sort them in alphabetical order.
-- @param #table tbl Table to get values from
-- @param #table tbl Table to get values from
-- @return #table Input table values in alphabetical order.
-- @return #table Input table values in alphabetical order, or <code>tbl</code> exactly if it has 0 or 1 items.
-- @author User:Demian
-- @author User:Demian
-- @see getSortedKeys
-- @see getSortedKeys
function p.getSortedValues(tbl)
function p.getSortedValues(tbl)
local sorted = {}
-- Check if there is anything to even sort.
if p.tableHasAtMostOneItem(tbl) then
return tbl
else
local sorted = {}


for _, value in ipairs(tbl) do
for _, value in ipairs(tbl) do
table.insert(sorted, value)
table.insert(sorted, value)
end
end


table.sort(sorted)
table.sort(sorted)


return sorted
return sorted
end
end
end


Line 406: Line 416:
function p.isEmpty(str)
function p.isEmpty(str)
return nil == str or "" == str
return nil == str or "" == str
end
--- Try to iterate a table to see if it has more than 0 items.
--
-- Will at most iterate over the first item.
-- <code>mw.LoadData</code> prevents <code>#tbl</code> from working correctly.
-- @param #table tbl Table check
-- @return #bool <code>true</code> if table has at least 1 item.
-- @author User:Demian
function p.tableHasData(tbl)
for _, _ in ipairs(tbl) do
return true
end
return false
end
--- Try to iterate a table to see if it has less than 2 items.
--
-- Will at most iterate over the first two items.
-- <code>mw.LoadData</code> prevents <code>#tbl</code> from working correctly.
-- @param #table tbl Table check
-- @return #bool <code>true</code> if table has 0 or 1 items, otherwise <code>false</code>.
-- @author User:Demian
function p.tableHasAtMostOneItem(tbl)
local count = 0
for _, value in ipairs(tbl) do
count = count + 1
if 1 < count then
return false
end
end
return 2 > count
end
end


return p
return p
Please note that all contributions to Eco - English Wiki are considered to be released under the CC BY-NC-SA 4.0 (see Eco:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, 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. Do not submit copyrighted work without permission!

To protect the wiki against automated edit spam, we kindly ask you to solve the following hCaptcha:

Cancel Editing help (opens in new window)

Template used on this page: