Mô đun:Infobox/utilities

require('strict');
local getArgs = require ('Module:Arguments').getArgs;

------------------------------------------------------------------------------------

return true if code is one of the listed Chinese, Japanese, Korean ISO 639 codes, false else.

local function is_cjk_code (code)
local cjk =
{
['zh'] = true, ['cdo'] = true, ['cjy'] = true, ['cmn'] = true, -- Chinese language codes
['cpi'] = true, ['cpx'] = true, ['czh'] = true, ['czo'] = true,
['gan'] = true, ['hak'] = true, ['hsn'] = true, ['ltc'] = true,
['lzh'] = true, ['mnp'] = true, ['nan'] = true, ['och'] = true,
['wuu'] = true, ['yue'] = true, ['zhx'] = true,
['ja'] = true, ['jpx'] = true, ['ojp'] = true, -- Japanese language codes
['ko'] = true, ['okm'] = true, ['oko'] = true, -- Korean language codes
}

return cjk[code] or false;
end

--italics=}|}}}}}

local function set_italics (frame)
local args=getArgs(frame);
local code = args[1] or args['code'] or ; -- empty string causes 'yes' return; will handle the missing code error
local text = args[2] or args['text'] or ; -- empty string causes 'yes' return; will handle the missing text error

local is_latn = require ("Module:Unicode data").is_Latin;

if is_cjk_code (code) and not is_latn (text) then -- is_latn() is in Module:Unicode data
return 'no'; -- only case for 'no'
end
return 'yes'; -- everything else is yes
end

--------------------------------------------------------------------------------------------------

compare function for result{} table descending sort

local function comp (a, b)
return tonumber (a[1]) > tonumber (b[1]);
end


--distribution ref=, |ethnicity ref=, or |occupation ref= as appropriate
frame - calling frame required for expandTemplate()

returns sorted list on success; empty string else

local function sort_common (result, ref, frame)
for i=result, 1, -1 do
if not tonumber (result[i][1]) then -- if cannot be converted to a number
table.remove (result, i); -- delete
end
end

if 0 == result then -- if we get here and the result table is empty
return ; -- abandon returning empty string
end

table.sort (result, comp); -- sort what remains

for i, v in ipairs (result) do
result[i] = table.concat (result[i]); -- make each table in result{} a string
end

result[1] = table.concat ({result[1], ref and ref or }); -- add reference(s) from | ref= to first item in the list

return frame:expandTemplate { title = 'Unbulleted list', args = result}; -- render the unbulleted list
end

--}|} }}

local function distribution_sort (frame)
local args=getArgs(frame);

local result = { -- initialize; table will be sorted according to values in result[n][1]
{args[1], '% đô thị'},
{args[2], '% nông thôn'},
};

return sort_common (result, args[result+1], frame);
end

--}|}|}|}|}|}|}|} }}

local function ethnicity_sort (frame)
local args=getArgs(frame);

local result = { -- initialize; table will be sorted according to values in result[n][1]
{args[1], '% Da trắng'},
{args[2], '% Da đen'},
{args[3], '% Gốc Á'},
{args[4], '% Tây Ban Nha hoặc Latinh'},
{args[5], '% Bản địa'},
{args[6], '% Pacific Islander Americans'},
{args[7], '% Two or more races'},
{args[8], '% other'}, -- TODO: make other always last?
};

return sort_common (result, args[result+1], frame);
end

--}|}|} }}

local function occupation_sort (frame)
local args=getArgs(frame);

local result = { -- initialize; table will be sorted according to values in result[n][1]
{args[1], '% Blue-collar'},
{args[2], '% White-collar'},
{args[3], '% Gray-collar'},
};

return sort_common (result, args[result+1], frame)
end

----------------------------------------------------------------------

return {
distribution_sort = distribution_sort, --
ethnicity_sort = ethnicity_sort,
occupation_sort = occupation_sort,

set_italics = set_italics, --
}