Jump to content

Module:columns/auto

From Wiktionary, the free dictionary


-- TODO needs a complete redesign
local export = {}

local columns_module = "Module:columns"
local table_module = "Module:table"

local require = require
local tostring = tostring

--[==[
Loaders for functions in other modules, which overwrite themselves with the target function when called. This ensures modules are only loaded when needed, retains the speed/convenience of locally-declared pre-loaded functions, and has no overhead after the first call, since the target functions are called directly in any subsequent calls.]==]
	local function columns_display_from(...)
		columns_display_from = require(columns_module).display_from
		return columns_display_from(...)
	end

	local function shallow_copy(...)
		shallow_copy = require(table_module).shallowCopy
		return shallow_copy(...)
	end

	local function table_len(...)
		table_len = require(table_module).length
		return table_len(...)
	end

function export.display_from(column_args, list_args)
	column_args = shallow_copy(column_args)
	local columns = column_args.columns
	if columns == nil then
		local number_of_items = table_len(list_args)
		columns = number_of_items <= 3 and 1 or
			number_of_items <= 9 and 2 or
			number_of_items <= 27 and 3 or
			number_of_items <= 81 and 4 or
			5
	end
	column_args.columns = tostring(columns)
	return columns_display_from(column_args, list_args)
end

function export.display(frame)
	return export.display_from(frame.args, frame:getParent().args)
end

return export