Module:CanonicalForms
Appearance
Documentation for this module may be created at Module:CanonicalForms/doc
-- Module:CanonicalForms
-- This module provides generic normalization functions.
-- Templates can supply their own mapping tables (arrays of mapping groups)
-- where each group is a table with:
-- • canonical: The canonical display value.
-- • synonyms: A table (array) of synonyms (case insensitive).
-- • [optional] css: Any extra property (e.g. CSS class) associated with the canonical value.
--
-- The functions in this module will process an input string against the provided mapping table
-- and return the canonical value (and optionally any extra property) if a match is found.
--
-- Usage example:
-- local mapping = {
-- { canonical = "gTLD", synonyms = {"generic", "gtld", "tld"} },
-- { canonical = "ccTLD", synonyms = {"cctld", "country", "cc"} }
-- }
-- local canonical, css = require('Module:CanonicalForms').normalize(inputString, mapping)
--
local CanonicalForms = {}
--- Normalize an input string based on a provided mapping table.
-- @param input The input string to normalize.
-- @param mappingTable An array of mapping groups.
-- @return canonical The canonical display value (if a match is found), otherwise the original input.
-- @return css (optional) The associated extra property (e.g. CSS class) if available.
function CanonicalForms.normalize(input, mappingTable)
if not input or input == "" then
return nil, nil
end
local lowerInput = input:lower()
for _, group in ipairs(mappingTable) do
if group.synonyms and type(group.synonyms) == "table" then
for _, syn in ipairs(group.synonyms) do
if lowerInput == syn:lower() then
return group.canonical, group.css
end
end
end
end
return input, nil
end
return CanonicalForms