Leaguepedia | League of Legends Esports Wiki
[checked revision][checked revision]
([ST] imageslinked -> imagelinks)
([ST] ditch custom ipairs metamethod)
Line 47: Line 47:
 
end
 
end
   
function p.__ipairs(tbl)
+
-- function p.__ipairs(tbl)
local function stateless_iter(tbl, i)
+
-- local function stateless_iter(tbl, i)
-- Implement your own index, value selection logic
+
-- -- Implement your own index, value selection logic
i = i + 1
+
-- i = i + 1
local val = tbl.objs[i]
+
-- local val = tbl.objs[i]
if val ~= nil then return i, val end
+
-- if val ~= nil then return i, val end
end
+
-- end
   
-- return iterator function, table, and starting point
+
-- -- return iterator function, table, and starting point
return stateless_iter, tbl, 0
+
-- return stateless_iter, tbl, 0
end
+
-- end
   
 
function p:exists()
 
function p:exists()
Line 79: Line 79:
 
if not opts then opts = {} end
 
if not opts then opts = {} end
 
local tbl = {}
 
local tbl = {}
for _, obj in ipairs(self) do
+
for _, obj in ipairs(self.objs) do
 
tbl[#tbl+1] = obj:name(opts)
 
tbl[#tbl+1] = obj:name(opts)
 
end
 
end
Line 89: Line 89:
 
if not opts then opts = {} end
 
if not opts then opts = {} end
 
local tbl = {}
 
local tbl = {}
for _, obj in ipairs(self) do
+
for _, obj in ipairs(self.objs) do
 
tbl[#tbl+1] = obj:get(length)
 
tbl[#tbl+1] = obj:get(length)
 
end
 
end
Line 99: Line 99:
 
if not opts then opts = {} end
 
if not opts then opts = {} end
 
local tbl = {}
 
local tbl = {}
for _, obj in ipairs(self) do
+
for _, obj in ipairs(self.objs) do
 
tbl[#tbl+1] = obj:link(opts)
 
tbl[#tbl+1] = obj:link(opts)
 
end
 
end
Line 109: Line 109:
 
if not opts then opts = {} end
 
if not opts then opts = {} end
 
local tbl = {}
 
local tbl = {}
for _, obj in ipairs(self) do
+
for _, obj in ipairs(self.objs) do
 
tbl[#tbl+1] = obj:flair(opts)
 
tbl[#tbl+1] = obj:flair(opts)
 
end
 
end
Line 119: Line 119:
 
if not opts then opts = {} end
 
if not opts then opts = {} end
 
local tbl = {}
 
local tbl = {}
for _, obj in ipairs(self) do
+
for _, obj in ipairs(self.objs) do
 
tbl[#tbl+1] = obj:flairlink(opts)
 
tbl[#tbl+1] = obj:flairlink(opts)
 
end
 
end
Line 129: Line 129:
 
if not opts then opts = {} end
 
if not opts then opts = {} end
 
local tbl = {}
 
local tbl = {}
for _, obj in ipairs(self) do
+
for _, obj in ipairs(self.objs) do
 
tbl[#tbl+1] = obj:image(opts)
 
tbl[#tbl+1] = obj:image(opts)
 
end
 
end
Line 139: Line 139:
 
if not opts then opts = {} end
 
if not opts then opts = {} end
 
local tbl = {}
 
local tbl = {}
for _, obj in ipairs(self) do
+
for _, obj in ipairs(self.objs) do
 
tbl[#tbl+1] = obj:imagelink(opts)
 
tbl[#tbl+1] = obj:imagelink(opts)
 
end
 
end
Line 146: Line 146:
   
 
function p:has(len, str)
 
function p:has(len, str)
for _, v in ipairs(self) do
+
for _, v in ipairs(self.objs) do
 
if v:get(len) == str then
 
if v:get(len) == str then
 
return true
 
return true

Revision as of 09:35, 21 October 2020

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

local util_args = require('Module:ArgsUtil')
local util_html = require("Module:HtmlUtil")
local util_map = require('Module:MapUtil')
local util_table = require("Module:TableUtil")
local util_text = require("Module:TextUtil")
local util_vars = require("Module:VarsUtil")
local i18n = require("Module:I18nUtil")
local lang = mw.getLanguage('en')

local LCS = require('Module:LuaClassSystem')

local p = LCS.class()
local h = {}

p.Entity = nil

function p:init(strs, opts)
	if not opts then opts = {} end
	self.sep = opts.sep or ','
	if not strs or (opts.alreadyCast and #strs == 0) then
		self.is_nil = true
		self.objs = {}
		return
	end
	if type(strs) == 'string' then
		strs = util_text.split(strs, self.sep)
	end
	
	-- so we can build a compound role from already-created objects
	-- instead of first having to cast back as string
	if opts.alreadyCast then
		self.objs = strs
	else
		self.objs = self:castEntities(strs, opts)
	end
end

function p:castEntities(strs, opts)
	if not self.Entity then
		error('Missing self.Entity, cannot cast')
	end
	local ret = {}
	for _, str in ipairs(strs) do
		ret[#ret+1] = self.Entity(str, opts)
	end
	return ret
end

-- function p.__ipairs(tbl)
-- 	local function stateless_iter(tbl, i)
-- 		-- Implement your own index, value selection logic
-- 		i = i + 1
-- 		local val = tbl.objs[i]
-- 		if val ~= nil then return i, val end
-- 	end

-- 	-- return iterator function, table, and starting point
-- 	return stateless_iter, tbl, 0
-- end

function p:exists()
	return not self.is_nil
end

function p:tostring()
	if self.is_nil then return end
	if self.objs then
		return util_table.concat(self, self.sep, tostring)
	end
	return 'Attempting to tostring something we cannot'
end

function p:first()
	return self.objs[1]
end

function p:names(opts)
	if self.is_nil then return end
	if not opts then opts = {} end
	local tbl = {}
	for _, obj in ipairs(self.objs) do
		tbl[#tbl+1] = obj:name(opts)
	end
	return util_table.concat(tbl, opts.sep)
end

function p:get(length, opts)
	if self.is_nil then return end
	if not opts then opts = {} end
	local tbl = {}
	for _, obj in ipairs(self.objs) do
		tbl[#tbl+1] = obj:get(length)
	end
	return util_table.concat(tbl, opts.sep)
end

function p:links(opts)
	if self.is_nil then return end
	if not opts then opts = {} end
	local tbl = {}
	for _, obj in ipairs(self.objs) do
		tbl[#tbl+1] = obj:link(opts)
	end
	return util_table.concat(tbl, opts.sep)
end

function p:flairs(opts)
	if self.is_nil then return end
	if not opts then opts = {} end
	local tbl = {}
	for _, obj in ipairs(self.objs) do
		tbl[#tbl+1] = obj:flair(opts)
	end
	return util_table.concat(tbl, opts.sep)
end

function p:flairlinks(opts)
	if self.is_nil then return end
	if not opts then opts = {} end
	local tbl = {}
	for _, obj in ipairs(self.objs) do
		tbl[#tbl+1] = obj:flairlink(opts)
	end
	return util_table.concat(tbl, opts.sep)
end

function p:images(opts)
	if self.is_nil then return end
	if not opts then opts = {} end
	local tbl = {}
	for _, obj in ipairs(self.objs) do
		tbl[#tbl+1] = obj:image(opts)
	end
	return util_table.concat(tbl, opts.sep or '')
end

function p:imagelinks(opts)
	if self.is_nil then return end
	if not opts then opts = {} end
	local tbl = {}
	for _, obj in ipairs(self.objs) do
		tbl[#tbl+1] = obj:imagelink(opts)
	end
	return util_table.concat(tbl, opts.sep)
end

function p:has(len, str)
	for _, v in ipairs(self.objs) do
		if v:get(len) == str then
			return true
		end
	end
	return false
end

function p:_or(entity)
	if self.is_nil then
		return entity
	end
	return self
end

return p