I’ve been doing some formatting for my plugin, and today I wanted to add a character whitelisting, what I am formatting is a time format, it is not done (yet). Time format should look like this hh:mm basically 05:02 for example, anyways back to the issue, I’ve got a problem…
Script:
local function timeFormat(text, setting)
local chars = {"1","2","3","4","5","6","7","8","9","0",":"}
for _, char in ipairs(chars) do
if text.match(char, text)==nil then
if #text == 1 then
return "0"..text..":00"
elseif #text == 2 then
return text..":00"
elseif #text == 3 then
return text.."00"
elseif #text == 4 then
local splittedTime = text:split(":")
return splittedTime[1]..":".."0"..splittedTime[2]
else
return plugin:GetSetting(setting)
end
else
return plugin:GetSetting(setting)
end
end
end
This is the original code I made. Before adding these loops and if text.match(char, text)==nil then everything worked perfectly fine, when I started implementing the character whitelisting, things started acting up…
Problems:
When messing around with the code like adding not() or other stuff to try to make this work things happening like:
or (in this case) not even reacting to the given input, or it does format, but formats the other blacklisted characters too.
The char table and if statement is needed to blacklist the characters that aren’t in the list. But if needed, it does format properly, if the input is 5, it will format it to 05:00, etc., but sadly the same happens with letters, if the input is a it will format it to 0a:00, and what I am trying to do is, if the input contains characters that aren’t in the list, it converts to the previous and correctly formatted value.
local function timeFormat(text, setting)
local chars = {"1","2","3","4","5","6","7","8","9","0",":"}
for _, char in ipairs(chars) do
if text.match(char, text)==nil then
if char == "1" then
return "0"..text..":00"
elseif char == "2" then
return text..":00"
elseif char == "3" then
return text.."00"
elseif char == "4" then
local splittedTime = text:split(":")
return splittedTime[1]..":".."0"..splittedTime[2]
else
-- return plugin:GetSetting(setting)
end
else
-- return plugin:GetSetting(setting)
end
end
end
print(timeFormat("2", 1))
Fixed just this loop. Took out GetSetting(setting) to test.
I appreciate you putting this in a format that can be debugged easy. But, I hope you didn’t pick logic that isn’t what you’re looking for …
The fact that I can test this very easily, as I am creating a plugin, also I got the
which is all I wanted, but all I am just trying to do is whitelisting the characters
I can test format with this
as I created it just for that
If in the chars variable, no these are the whitelisted characters, also I don’t understand why did you put char instead of #text, as I format by the character count in the provided text string. And the char is the whitelisted character, and the if text.match(char, text)==nil then searches for it.