[checked revision] | [checked revision] |
([ST]) |
([ST] catch empty string here) |
||
(34 intermediate revisions by the same user not shown) | |||
Line 18: | Line 18: | ||
if not opts then opts = {} end |
if not opts then opts = {} end |
||
self.sep = opts.sep or ',' |
self.sep = opts.sep or ',' |
||
− | if not strs then |
+ | if not strs or (opts.alreadyCast and #strs == 0) then |
self.is_nil = true |
self.is_nil = true |
||
self.objs = {} |
self.objs = {} |
||
Line 26: | Line 26: | ||
strs = util_text.split(strs, self.sep) |
strs = util_text.split(strs, self.sep) |
||
end |
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 |
if opts.alreadyCast then |
||
self.objs = strs |
self.objs = strs |
||
Line 54: | Line 57: | ||
-- 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 |
||
+ | |||
+ | function p:iter() |
||
+ | local i = 0 |
||
+ | local function localIter() |
||
+ | i = i + 1 |
||
+ | local val = self.objs[i] |
||
+ | if val ~= nil then return i, val end |
||
+ | end |
||
+ | return localIter |
||
end |
end |
||
Line 68: | Line 81: | ||
end |
end |
||
− | function p: |
+ | function p:first() |
+ | return self.objs[1] |
||
+ | end |
||
+ | |||
+ | function p:names(opts) |
||
if self.is_nil then return end |
if self.is_nil then return end |
||
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) do |
||
− | tbl[#tbl+1] = obj:name( |
+ | tbl[#tbl+1] = obj:name(opts) |
end |
end |
||
return util_table.concat(tbl, opts.sep) |
return util_table.concat(tbl, opts.sep) |
||
end |
end |
||
− | function p: |
+ | function p:get(length, opts) |
if self.is_nil then return end |
if self.is_nil then return end |
||
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) do |
||
− | tbl[#tbl+1] = obj: |
+ | tbl[#tbl+1] = obj:get(length) |
end |
end |
||
return util_table.concat(tbl, opts.sep) |
return util_table.concat(tbl, opts.sep) |
||
end |
end |
||
− | function p: |
+ | function p:getRaw(length, opts) |
if self.is_nil then return end |
if self.is_nil then return end |
||
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) do |
||
− | tbl[#tbl+1] = obj: |
+ | tbl[#tbl+1] = obj:getRaw(length) |
+ | end |
||
+ | ret = util_table.concat(tbl, opts.sep) |
||
+ | if ret == '' then return nil end |
||
+ | return ret |
||
+ | 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) do |
||
+ | tbl[#tbl+1] = obj:link(opts) |
||
end |
end |
||
return util_table.concat(tbl, opts.sep) |
return util_table.concat(tbl, opts.sep) |
||
end |
end |
||
− | function p: |
+ | function p:flairs(opts) |
if self.is_nil then return end |
if self.is_nil then return end |
||
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) do |
||
− | tbl[#tbl+1] = obj: |
+ | 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) do |
||
+ | tbl[#tbl+1] = obj:flairlink(opts) |
||
end |
end |
||
return util_table.concat(tbl, opts.sep) |
return util_table.concat(tbl, opts.sep) |
||
Line 115: | Line 154: | ||
tbl[#tbl+1] = obj:image(opts) |
tbl[#tbl+1] = obj:image(opts) |
||
end |
end |
||
− | return util_table.concat(tbl, opts.sep) |
+ | return util_table.concat(tbl, opts.sep or '') |
end |
end |
||
− | function p: |
+ | function p:imagelinks(opts) |
if self.is_nil then return end |
if self.is_nil then return end |
||
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) do |
||
− | tbl[#tbl+1] = obj: |
+ | tbl[#tbl+1] = obj:imagelink(opts) |
end |
end |
||
return util_table.concat(tbl, opts.sep) |
return util_table.concat(tbl, opts.sep) |
||
+ | end |
||
+ | |||
+ | function p:has(len, str) |
||
+ | for _, v in ipairs(self) 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 |
end |
||
Revision as of 21:21, 14 September 2021
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:iter()
local i = 0
local function localIter()
i = i + 1
local val = self.objs[i]
if val ~= nil then return i, val end
end
return localIter
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) 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) do
tbl[#tbl+1] = obj:get(length)
end
return util_table.concat(tbl, opts.sep)
end
function p:getRaw(length, opts)
if self.is_nil then return end
if not opts then opts = {} end
local tbl = {}
for _, obj in ipairs(self) do
tbl[#tbl+1] = obj:getRaw(length)
end
ret = util_table.concat(tbl, opts.sep)
if ret == '' then return nil end
return ret
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) 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) 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) 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) 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) 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) 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