Difference between revisions of "Module:Sandbox"

From Nookipedia, the Animal Crossing wiki
(restructuring module: now should catch instances where a particular name should be lowercased)
Line 1: Line 1:
 +
-- Generates random New Horizons item name; adopted from Temtem Wiki, licensed under CC BY-NC-SA, authors listed here: https://temtem.wiki.gg/wiki/Module:Daily_tem?action=history
 
local p = {}
 
local p = {}
local getArgs = require('Module:Arguments').getArgs
+
local cargo = mw.ext.cargo
 
 
function split(str, pattern)
 
    local out = {}
 
    for m in string.gmatch(str, "[^" .. pattern .. "]+") do
 
      table.insert(out, m)
 
    end
 
    return out
 
end
 
 
 
function isEmpty(s)
 
return s == nil or s == ''
 
end
 
 
 
function search(category, substrings)
 
    for k,v in pairs(substrings) do
 
        if string.find(category, v) then
 
            return true
 
        end
 
    end
 
    return false
 
end
 
 
 
-- From Lua-User Wiki: http://lua-users.org/wiki/SciteTitleCase
 
function titlecase(str)
 
local titleName = ''
 
local buf = {}
 
local inWord = false
 
for i = 1, #str do
 
local c = string.sub(str, i, i)
 
if inWord then
 
table.insert(buf, c)
 
if string.find(c, '%s') or string.find(c, '-') then
 
inWord = false
 
end
 
else
 
table.insert(buf, string.upper(c))
 
inWord = true
 
end
 
end
 
return table.concat(buf)
 
end
 
  
 
function p.main(frame)
 
function p.main(frame)
    local args        = getArgs(frame)
+
local tables = 'nh_furniture'
local name        = args['1'] or ''
+
local count = cargo.query( tables, 'COUNT(DISTINCT en_name)=n', {'no html'})[1]['n']
    local titleCase    = titlecase(name)
+
    return p.keepLowercase(titleCase)
+
math.randomseed(os.date("%Y%m%d"))
end
 
  
function p.keepLowercase(str)
+
    local args = {
local small = {"Al", "And", "An", "As", "At", "By", "En", "De", "Di", "For", "If", "In", "N", "Nor", "O", "Of", "On", "Or", "Only", "Over", "Per", "So", "Some", "That", "Than", "The", "To", "Upon", "Vs", "Versus", "Via", "Where", "When", "With", "Without", "Yet"}
+
        limit = 1,
if search(str:gsub("^%l", string.upper), small) == true then
+
        offset = math.floor(math.random() * count),
for k,v in pairs(small) do
+
    }
        if string.find(str, "%p" .. v .. "%p") then
+
    local results = cargo.query( tables, 'en_name', args )
            str = str:gsub("%p" .. v .. "%p", string.lower)
+
local name = results[1]['name']
            elseif string.find(str, "%s" .. v .. "%s") then
+
            str = str:gsub("%s" .. v .. "%s", string.lower)
+
return name
        end
 
    end
 
end
 
return str
 
 
end
 
end
  
 
return p
 
return p

Revision as of 00:55, December 2, 2022

Module documentation (view)


Usage

This is the general sandbox page to test out Lua modules. The module should not be implemented in a template.


-- Generates random New Horizons item name; adopted from Temtem Wiki, licensed under CC BY-NC-SA, authors listed here: https://temtem.wiki.gg/wiki/Module:Daily_tem?action=history
local p = {}
local cargo = mw.ext.cargo

function p.main(frame)
	local tables = 'nh_furniture'
	local count = cargo.query( tables, 'COUNT(DISTINCT en_name)=n', {'no html'})[1]['n']
	
	math.randomseed(os.date("%Y%m%d"))

    local args = {
        limit = 1,
        offset = math.floor(math.random() * count),
    }
    local results = cargo.query( tables, 'en_name', args )
	local name = results[1]['name']
	
	return name
end

return p