Module:TemplateHelpers: Difference between revisions
// via Wikitext Extension for VSCode Tags: Manual revert Reverted |
// via Wikitext Extension for VSCode Tag: Manual revert |
||
| Line 116: | Line 116: | ||
function p.joinValues(values, delimiter) | function p.joinValues(values, delimiter) | ||
return NormalizationText.joinValues(values, delimiter) | return NormalizationText.joinValues(values, delimiter) | ||
end | |||
-- Removes duplicate values from an array while preserving order | |||
-- @param t table The array to deduplicate | |||
-- @return table A new array with duplicates removed | |||
function p.removeDuplicates(t) | |||
-- Type checking | |||
if type(t) ~= 'table' then | |||
return {} | |||
end | |||
-- Helper function to check if a value is NaN | |||
local function isNan(v) | |||
return type(v) == 'number' and tostring(v) == '-nan' | |||
end | |||
-- Pre-allocate result table (maximum possible size is #t) | |||
local ret, exists = {}, {} | |||
-- Process each value, preserving order | |||
for i, v in ipairs(t) do | |||
if isNan(v) then | |||
-- NaNs can't be table keys, and they are also unique | |||
ret[#ret + 1] = v | |||
else | |||
if not exists[v] then | |||
ret[#ret + 1] = v | |||
exists[v] = true | |||
end | |||
end | |||
end | |||
return ret | |||
end | end | ||