Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Utility: Difference between revisions

From Kronopolis and the Flat Plane
mNo edit summary
mNo edit summary
 
(One intermediate revision by the same user not shown)
Line 20: Line 20:
function p.dump(o)
function p.dump(o)
   if type(o) == 'table' then
   if type(o) == 'table' then
       local s = '{ '
       local s = '{ \n'
       for k,v in pairs(o) do
       for k,v in pairs(o) do
         if type(k) ~= 'number' then k = '"'..k..'"' end
         if type(k) ~= 'number' then k = '"'..k..'"' end
         s = s .. '['..k..'] = ' .. p.dump(v) .. ',\n\n'
         s = s .. '['..k..'] = ' .. p.dump(v) .. ',\n'
       end
       end
       return s .. '} '
       return s .. '} '

Latest revision as of 19:51, 18 October 2025

Usage

Import: local util = require('Module:Utility')

Functions

dump

Turn a table into a string.

dump(table)
Return type: string

Example

local table = {
    dong = 'biggest',
    description = 'schlong'
}
local str = dump(table)

Returns:

{ 
["dong "] = biggest,
["description"] = schlong,
}

firstToUpper

Capitalize first character in a string.

firstToUpper(string)
Return type: string

Example

local dong = firstToUpper("donger")

Returns:

dong = "Donger"

formatLevel

Get a formatted level based on an integer.

formatLevel(integer)
Return type: string

Example

local lv = formatLevel(0)
local lv1 = formatLevel(1)
local lv2 = formatLevel(2)

Returns:

lv = "cantrip"
lv1 = "1st"
lv2 = "2nd"


local p = {}

function p.firstToUpper(str)
    if str ~= nil then
        return (str:gsub("^%l", string.upper))
    end
end

function p.formatLevel(lv)
    local str
    if lv == 0 then str = 'Cantrip' 
    elseif lv == 1 then str = '1st' 
    elseif lv == 2 then str = '2nd' 
    elseif lv == 3 then str = '3rd' 
    else str = lv .. 'th'
    end
    return str
end

function p.dump(o)
   if type(o) == 'table' then
      local s = '{ \n'
      for k,v in pairs(o) do
         if type(k) ~= 'number' then k = '"'..k..'"' end
         s = s .. '['..k..'] = ' .. p.dump(v) .. ',\n'
      end
      return s .. '} '
   else
      return tostring(o)
   end
end


return p