20.643
Bearbeitungen
(Modul ohne e) |
(+ appendWithComma(s, a)) |
||
| (11 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt) | |||
| Zeile 39: | Zeile 39: | ||
-- mw.log("split2(" .. (s or "nil") .. ", " .. (delimiter or "nil") .. ")") | -- mw.log("split2(" .. (s or "nil") .. ", " .. (delimiter or "nil") .. ")") | ||
local result = {} | local result = {} | ||
if | if s ~= nil and s ~= "" then | ||
result = p.split(s, delimiter) | result = p.split(s, delimiter) | ||
end | end | ||
-- mw.logObject(result) | -- mw.logObject(result) | ||
return result | return result | ||
end | end | ||
| Zeile 70: | Zeile 49: | ||
-- Removes all spaces at the start and at the end of a string | -- Removes all spaces at the start and at the end of a string | ||
return s:match("^%s*(.-)%s*$") | return s:match("^%s*(.-)%s*$") | ||
end | |||
function p.nilStrip(s) | |||
-- same as p.strip(s), but removes nil, if s is empty or nil | |||
-- useful to handle missing/empty args | |||
if s == nil then | |||
return | |||
end | |||
s = p.strip(s) | |||
if s == "" then | |||
return | |||
end | |||
return s | |||
end | end | ||
| Zeile 89: | Zeile 81: | ||
-- if list has no content, table will be empty | -- if list has no content, table will be empty | ||
delimiter = delimiter or " " | delimiter = delimiter or " " | ||
s = s .. delimiter | s = (s or "") .. delimiter | ||
local result = {} | local result = {} | ||
local start = 1 | local start = 1 | ||
| Zeile 104: | Zeile 96: | ||
-- mw.logObject(result) | -- mw.logObject(result) | ||
return result | return result | ||
end | |||
function p.bracketSplitAndStrip(s) | |||
-- scheidet einen geklammerten String am Anfang und am Ende ab | |||
-- Beispiel: "( a ) b (c)" ergibt "( a )", "b", "(c)" | |||
local s_pre_bracket = "" | |||
local s_post_bracket = "" | |||
-- Klammerung am Anfang | |||
local bracket_open = string.find(s, "%(") | |||
local bracket_close = string.find(s, "%)") | |||
if bracket_open == 1 and bracket_close ~= nil then | |||
s_pre_bracket = string.sub(s, bracket_open, bracket_close) | |||
s = p.strip(string.sub(s, bracket_close+1)) | |||
end | |||
-- Klammerung am Ende | |||
bracket_open = string.find(s, "%([^%(]*$") | |||
bracket_close = string.find(s, "%)[^%)]*$") | |||
if bracket_open ~= nil and bracket_close == string.len(s) then | |||
s_post_bracket = string.sub(s, bracket_open, bracket_close) | |||
s = p.strip(string.sub(s, 1, bracket_open-1)) | |||
end | |||
return s_pre_bracket, s, s_post_bracket | |||
end | |||
function p.appendWithComma(s, a) | |||
if a ~= nil and a ~= "" then | |||
if s ~= "" then | |||
s = s .. ", " .. a | |||
else | |||
s = a | |||
end | |||
end | |||
return s | |||
end | end | ||
return p | return p | ||