Module:HelperMethods: Difference between revisions

From MaRDI portal
Created page with "-- Required modules for SPARQL queries and HTML table generation local sparql = require('SPARQL') local mwHtml = require('mw.html') -- Utility function to trim and lowercase a string function trimAndLower(str) if str == nil then return nil end str = str:gsub("^%s*(.-)%s*$", "%1") return str:lower() end -- Function to convert JSON results into a Lua table function convertJsonToTable(jsonResults) local resultsTable = {} if jsonResults and jsonResults.results a..."
 
No edit summary
Line 1: Line 1:
-- Required modules for SPARQL queries and HTML table generation
-- Required modules for SPARQL queries and HTML table generation
local sparql = require('SPARQL')
local sparql = require('SPARQL')
Line 4: Line 5:


-- Utility function to trim and lowercase a string
-- Utility function to trim and lowercase a string
function trimAndLower(str)
function M.trimAndLower(str)
if str == nil then return nil end
if str == nil then return nil end
str = str:gsub("^%s*(.-)%s*$", "%1")
str = str:gsub("^%s*(.-)%s*$", "%1")
Line 11: Line 12:


-- Function to convert JSON results into a Lua table
-- Function to convert JSON results into a Lua table
function convertJsonToTable(jsonResults)
function M.convertJsonToTable(jsonResults)
     local resultsTable = {}
     local resultsTable = {}
     if jsonResults and jsonResults.results and jsonResults.results.bindings then
     if jsonResults and jsonResults.results and jsonResults.results.bindings then
Line 25: Line 26:
     return resultsTable
     return resultsTable
end
end
return M

Revision as of 10:42, 13 December 2023

Documentation for this module may be created at Module:HelperMethods/doc

-- Required modules for SPARQL queries and HTML table generation
local sparql = require('SPARQL')
local mwHtml = require('mw.html')

-- Utility function to trim and lowercase a string
function M.trimAndLower(str)
	if str == nil then return nil end
	str = str:gsub("^%s*(.-)%s*$", "%1")
	return str:lower()
end

-- Function to convert JSON results into a Lua table
function M.convertJsonToTable(jsonResults)
    local resultsTable = {}
    if jsonResults and jsonResults.results and jsonResults.results.bindings then
        local bindings = jsonResults.results.bindings
        for j = 1, #bindings do
            local row = {}
            for key, value in pairs(bindings[j]) do
                table.insert(row, value.value)
            end
            table.insert(resultsTable, row)
        end
    end
    return resultsTable
end

return M